├── .editorconfig ├── .eslintrc.js ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── lib ├── index.js └── util.js ├── package-lock.json ├── package.json └── test ├── cases ├── .babelrc ├── nested-dep │ ├── index.js │ ├── node_modules │ │ ├── a │ │ │ ├── index.js │ │ │ └── package.json │ │ ├── b │ │ │ ├── index.js │ │ │ ├── node_modules │ │ │ │ └── a │ │ │ │ │ ├── index.js │ │ │ │ │ └── package.json │ │ │ └── package.json │ │ ├── c │ │ │ ├── index.js │ │ │ └── package.json │ │ ├── d │ │ │ ├── index.js │ │ │ └── package.json │ │ └── e │ │ │ ├── index.js │ │ │ └── package.json │ ├── package.json │ └── webpack.config.js ├── nested │ ├── app.js │ ├── index.js │ ├── node_modules │ │ ├── a │ │ │ ├── index.js │ │ │ └── package.json │ │ └── b │ │ │ ├── index.js │ │ │ ├── node_modules │ │ │ ├── @dd │ │ │ │ └── a │ │ │ │ │ ├── index.js │ │ │ │ │ └── package.json │ │ │ └── a │ │ │ │ ├── index.js │ │ │ │ └── package.json │ │ │ └── package.json │ ├── package.json │ └── webpack.config.js └── normal │ ├── app.js │ ├── index.js │ ├── node_modules │ ├── a │ │ ├── index.js │ │ └── package.json │ └── b │ │ ├── index.js │ │ └── package.json │ ├── package.json │ └── webpack.config.js ├── integration ├── nested.js └── normal.js └── unit ├── index.js └── util.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | charset = utf-8 6 | trim_trailing_whitespace = false 7 | insert_final_newline = true 8 | indent_style = space 9 | indent_size = 2 10 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | node: true 4 | }, 5 | parserOptions: { 6 | sourceType: 'module' 7 | }, 8 | extends: 'standard' 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | app.js 2 | # Logs 3 | logs 4 | *.log 5 | npm-debug.log* 6 | yarn-debug.log* 7 | yarn-error.log* 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | *.pid.lock 14 | 15 | # Directory for instrumented libs generated by jscoverage/JSCover 16 | lib-cov 17 | 18 | # Coverage directory used by tools like istanbul 19 | coverage 20 | 21 | # nyc test coverage 22 | .nyc_output 23 | 24 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 25 | .grunt 26 | 27 | # Bower dependency directory (https://bower.io/) 28 | bower_components 29 | 30 | # node-waf configuration 31 | .lock-wscript 32 | 33 | # Compiled binary addons (http://nodejs.org/api/addons.html) 34 | build/Release 35 | 36 | # Dependency directories 37 | node_modules/* 38 | jspm_packages/ 39 | 40 | # Typescript v1 declaration files 41 | typings/ 42 | 43 | # Optional npm cache directory 44 | .npm 45 | 46 | # Optional eslint cache 47 | .eslintcache 48 | 49 | # Optional REPL history 50 | .node_repl_history 51 | 52 | # Output of 'npm pack' 53 | *.tgz 54 | 55 | # Yarn Integrity file 56 | .yarn-integrity 57 | 58 | # dotenv environment variables file 59 | .env 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | cache: 4 | directories: 5 | - node_modules 6 | node_js: 7 | - "6" 8 | branches: 9 | only: 10 | - master 11 | script: 12 | - npm test 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 doly mood 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # webpack-transform-modules-plugin [![Build Status](https://travis-ci.org/dolymood/webpack-transform-modules-plugin.svg?branch=master)](https://travis-ci.org/dolymood/webpack-transform-modules-plugin?branch=master) [![codecov.io](http://codecov.io/github/dolymood/webpack-transform-modules-plugin/coverage.svg?branch=master)](http://codecov.io/github/dolymood/webpack-transform-modules-plugin?branch=master) 2 | 3 | A webpack plugin for [babel-plugin-transform-modules](https://github.com/dolymood/babel-plugin-transform-modules). It is used to handle `babel-plugin-transform-modules` 'transform-modules' options in node_modules. 4 | 5 | Note: This plugin only works with `babel-loader` and `vue-loader`. 6 | 7 | ### Install 8 | 9 | ```shell 10 | npm i webpack-transform-modules-plugin --save-dev 11 | ``` 12 | 13 | ### Usage 14 | 15 | ```js 16 | var TransformModulesPlugin = require('webpack-transform-modules-plugin') 17 | 18 | module.exports = { 19 | // ... 20 | module: { 21 | rules: [ 22 | // ... 23 | { 24 | test: /\.js$/, 25 | loader: 'babel-loader' 26 | // this plugin will be include the packages "transformModules" config 27 | // eg: {"transformModules": {"a": {...}}} // in package.json 28 | // the current rule's options plugins will be like: 29 | /* 30 | `[require('babel-plugin-transform-modules'), { 31 | 'a': { 32 | ... 33 | } 34 | }]` 35 | */ 36 | } 37 | // ... 38 | ] 39 | }, 40 | plugins: [ 41 | new TransformModulesPlugin() 42 | ] 43 | } 44 | ``` 45 | 46 | #### Options 47 | 48 | ```js 49 | new TransformModulesPlugin({ 50 | transformModules: { 51 | a: { 52 | transform: 'a/${member}' 53 | }, 54 | b: null 55 | } 56 | }) 57 | ``` 58 | 59 | In this demo, this plugin will load `a` and `b` packages "transformModules" config in each package.json and it will load sub packages "transformModules" configs too. 60 | 61 | * `transformModules {Object}` default `undefined`, application init `babel-plugin-transform-modules` 'transform-modules' options, if it is `undefined` then this plugin will get `transformModules` value in `package.json` as the init config. 62 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var util = require('./util') 2 | 3 | function TransformModulesPlugin (options) { 4 | if (!options) { 5 | options = {} 6 | } 7 | this.key = 'transformModules' 8 | this.initTransformModules = options.transformModules || null 9 | } 10 | 11 | TransformModulesPlugin.prototype.apply = function (compiler) { 12 | var that = this 13 | var called = false 14 | var runGetter = function (compiler, callback) { 15 | if (called) return callback() 16 | called = true 17 | /* istanbul ignore if */ 18 | if (compiler.compiler) { 19 | // fix https://github.com/webpack/webpack/pull/5739 20 | compiler = compiler.compiler 21 | } 22 | var transformModules = that._collectTransformModules(compiler) 23 | if (Object.keys(transformModules).length) { 24 | var rules = compiler.options.module.rules 25 | rules && rules.forEach(iterRule) 26 | } 27 | callback() 28 | function iterRule(rule, i) { 29 | if (doRule(rule, 'loader')) { 30 | } else if (doRule(rule, 'use')) { 31 | } else if (rule.oneOf) { 32 | if (!Array.isArray(rule.oneOf)) { 33 | rule.oneOf = [rule.oneOf] 34 | } 35 | rule.oneOf.forEach(iterRule) 36 | } 37 | } 38 | function doRule (rule, key) { 39 | var conf = rule[key] 40 | if (conf) { 41 | if (Array.isArray(conf)) { 42 | conf.forEach(function (loaderConf, i) { 43 | if (typeof loaderConf === 'string') { 44 | loaderConf = conf[i] = { 45 | loader: loaderConf 46 | } 47 | } 48 | updateLoaderConf(loaderConf) 49 | }) 50 | } else { 51 | if (key === 'use') { 52 | if (typeof conf === 'string') { 53 | conf = rule.use = { 54 | loader: conf 55 | } 56 | } 57 | updateLoaderConf(conf) 58 | } else { 59 | updateLoaderConf(rule) 60 | } 61 | } 62 | return true 63 | } 64 | return false 65 | } 66 | function updateLoaderConf (rule) { 67 | if (util.loaderNameMatches(rule, 'babel-loader')) { 68 | updateRule(rule) 69 | } else if (util.loaderNameMatches(rule, 'vue-loader')) { 70 | updateVueLoaderOptions(rule) 71 | } 72 | } 73 | function updateRule (conf) { 74 | if (!conf.options) { 75 | conf.options = {} 76 | } 77 | if (!conf.options.plugins) { 78 | conf.options.plugins = [] 79 | } 80 | if (!Array.isArray(conf.options.plugins)) { 81 | conf.options.plugins = [conf.options.plugins] 82 | } 83 | var name = 'babel-plugin-transform-modules' 84 | var plugins = conf.options.plugins 85 | var added = plugins.some(function (plugin) { 86 | return plugin === name || plugin[0] === name 87 | }) 88 | !added && plugins.push([ 89 | name, 90 | transformModules 91 | ]) 92 | } 93 | function updateVueLoaderOptions (conf) { 94 | if (!conf.options) { 95 | conf.options = {} 96 | } 97 | if (!conf.options.loaders) { 98 | conf.options.loaders = {} 99 | } 100 | var jsConf = conf.options.loaders.js 101 | if (!jsConf) { 102 | jsConf = conf.options.loaders.js = [] 103 | } else if (typeof jsConf === 'string') { 104 | if (jsConf === 'babel-loader') { 105 | // reset 106 | jsConf = conf.options.loaders.js = [] 107 | } else { 108 | jsConf = conf.options.loaders.js = [ 109 | { 110 | loader: jsConf 111 | } 112 | ] 113 | } 114 | } 115 | // if babel-loader already exists, 116 | // push babel-plugin-transform-modules into the plugins of existing babel-loader 117 | if (typeof jsConf === 'object' && jsConf.loader === 'babel-loader') { 118 | jsConf.plugins = jsConf.plugins || [] 119 | jsConf.plugins.push(['babel-plugin-transform-modules', transformModules]) 120 | } else { 121 | jsConf.push({ 122 | loader: 'babel-loader', 123 | options: { 124 | plugins: [ 125 | ['babel-plugin-transform-modules', transformModules] 126 | ] 127 | } 128 | }) 129 | } 130 | } 131 | } 132 | if (compiler.hooks) { 133 | compiler.hooks.beforeRun.tapAsync('TransformModulesPlugin', runGetter) 134 | compiler.hooks.watchRun.tapAsync('TransformModulesPlugin', runGetter) 135 | } else { 136 | compiler.plugin(['before-run', 'watch-run'], runGetter) 137 | } 138 | } 139 | 140 | TransformModulesPlugin.prototype._collectTransformModules = function (compiler) { 141 | var transformModules = {} 142 | var walkQueue = [] 143 | var walked = {} 144 | var context = compiler.options.context 145 | var initTransformModules = this.initTransformModules || {} 146 | walkQueue.push({ 147 | name: '.', 148 | context: context 149 | }) 150 | util.setTransformModules(transformModules, initTransformModules, context) 151 | util.collectTransformModules(walkQueue, walked, transformModules, context, this.key) 152 | return transformModules 153 | } 154 | 155 | module.exports = TransformModulesPlugin 156 | -------------------------------------------------------------------------------- /lib/util.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var load = require('load-pkg-config') 3 | 4 | module.exports = { 5 | getPkg: getPkg, 6 | getConfig: getConfig, 7 | collectTransformModules: collectTransformModules, 8 | loaderNameMatches: loaderNameMatches, 9 | setTransformModules: setTransformModules 10 | } 11 | 12 | function loaderNameMatches (rule, loaderName) { 13 | return rule && rule.loader && typeof rule.loader === 'string' && 14 | ( 15 | rule.loader === loaderName || 16 | rule.loader.indexOf(path.sep + loaderName + path.sep) !== -1 || 17 | rule.loader.indexOf('@' + loaderName + path.sep) !== -1 18 | ) 19 | } 20 | 21 | function collectTransformModules (walkQueue, walked, transformModules, context, key) { 22 | while(walkQueue.length) { 23 | var depInfo = walkQueue.shift() 24 | var pkg = getPkg(depInfo.name, depInfo.context) 25 | if (!pkg || walked[pkg.modulePath]) { 26 | continue 27 | } 28 | walked[pkg.modulePath] = true 29 | var subPkgTransformModules = pkg[key] 30 | setTransformModules(transformModules, pkg[key] || {}, pkg.modulePath) 31 | var subDep = getDeepWalkDep(pkg, key) 32 | walkQueue = walkQueue.concat(subDep) 33 | } 34 | } 35 | 36 | var rootCwd = process.cwd() 37 | function getPkg (name, cwd) { 38 | if (!cwd) { 39 | cwd = rootCwd 40 | } 41 | var pkg = load(name, cwd) 42 | return pkg || null 43 | } 44 | 45 | function getConfig (name, key, cwd) { 46 | var pkg = getPkg(name, cwd) 47 | if (pkg) { 48 | return pkg[key] 49 | } 50 | return null 51 | } 52 | 53 | function getDeepWalkDep (pkg, key) { 54 | var deepWalkDep = [] 55 | Object.keys(pkg.dependencies || {}).forEach(function (k) { 56 | deepWalkDep.push({ 57 | name: k, 58 | context: pkg.modulePath 59 | }) 60 | }) 61 | return deepWalkDep 62 | } 63 | 64 | function setTransformModules(transformModules, optMap, fromContext) { 65 | var transNames = Object.keys(optMap) 66 | transNames.forEach(name => { 67 | if (!transformModules[name]) { 68 | optMap[name] && (transformModules[name] = optMap[name]) 69 | return 70 | } 71 | if (JSON.stringify(transformModules[name]) !== JSON.stringify(optMap[name])) { 72 | console.warn('[webpack-transform-modules-plugin] There are multiple node modules configured "transformModules" with module: "' + name + '", ' + 'in:' + fromContext) 73 | } 74 | }) 75 | } 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-transform-modules-plugin", 3 | "version": "0.4.4", 4 | "description": "webpack transform modules plugin", 5 | "main": "lib/index.js", 6 | "files": [ 7 | "lib" 8 | ], 9 | "scripts": { 10 | "test": "npm run test:unit && npm run codecov && npm run test:integration", 11 | "test:unit": "nyc mocha -t 1000 --recursive ./test/unit", 12 | "test:integration": "mocha -t 5000 ./test/integration/*.js", 13 | "codecov": "codecov", 14 | "lint": "eslint lib/*" 15 | }, 16 | "nyc": { 17 | "sourceMap": false, 18 | "include": [ 19 | "lib" 20 | ], 21 | "reporter": [ 22 | "lcov" 23 | ] 24 | }, 25 | "dependencies": { 26 | "babel-plugin-transform-modules": "^0.1.1", 27 | "load-pkg-config": "^1.0.1" 28 | }, 29 | "devDependencies": { 30 | "babel": "^6.23.0", 31 | "babel-core": "^6.26.0", 32 | "babel-loader": "^7.1.2", 33 | "babel-preset-es2015": "^6.24.1", 34 | "chai": "^4.1.2", 35 | "codecov": "^3.0.0", 36 | "eslint": "^4.16.0", 37 | "eslint-config-standard": "^11.0.0-beta.0", 38 | "eslint-plugin-import": "^2.7.0", 39 | "eslint-plugin-node": "^5.1.1", 40 | "eslint-plugin-promise": "^3.6.0", 41 | "eslint-plugin-standard": "^3.0.1", 42 | "json-loader": "^0.5.7", 43 | "mocha": "^5.0.0", 44 | "nyc": "^11.4.1", 45 | "webpack": "^3.6.0" 46 | }, 47 | "peerDependencies": { 48 | "webpack": ">= 3", 49 | "babel-plugin-transform-modules": ">= 0.1.1" 50 | }, 51 | "repository": { 52 | "type": "git", 53 | "url": "git+https://github.com/dolymood/webpack-transform-modules-plugin.git" 54 | }, 55 | "author": "dolymood", 56 | "license": "MIT", 57 | "bugs": { 58 | "url": "https://github.com/dolymood/webpack-transform-modules-plugin/issues" 59 | }, 60 | "homepage": "https://github.com/dolymood/webpack-transform-modules-plugin#readme" 61 | } 62 | -------------------------------------------------------------------------------- /test/cases/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /test/cases/nested-dep/index.js: -------------------------------------------------------------------------------- 1 | import { index as a } from 'a' 2 | import b from 'b' 3 | import { index as c } from 'c' 4 | import { index as d } from 'd' 5 | import pkg from './package.json' 6 | 7 | export { 8 | a, 9 | b, 10 | c, 11 | d, 12 | pkg 13 | } 14 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/a/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested a' -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "a", 3 | "version": "0.0.1", 4 | "main": "index.js" 5 | } -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/b/index.js: -------------------------------------------------------------------------------- 1 | import { index as d } from 'd' 2 | import { index as e } from 'e' 3 | import { index as a } from 'a' 4 | export default `nested b, ${d}, ${e}, ${a}` 5 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/b/node_modules/a/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested a@0.0.2' 2 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/b/node_modules/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "a", 3 | "version": "0.0.2", 4 | "main": "index.js", 5 | "transformModules": { 6 | "a": { 7 | "transform": "a/xx${member}" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/b/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "b", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "dependencies": { 6 | "d": "0.0.1", 7 | "e": "0.0.1", 8 | "a": "0.0.2" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/c/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested c' 2 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/c/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "c", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "transformModules": { 6 | "c": { 7 | "transform": "c/${member}" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/d/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested d' 2 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/d/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "d", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "transformModules": { 6 | "d": { 7 | "transform": "d/${member}" 8 | }, 9 | "a": { 10 | "transform": "a/${member}", 11 | "kebabCase": true 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/e/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested e' 2 | -------------------------------------------------------------------------------- /test/cases/nested-dep/node_modules/e/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "e", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "transformModules": { 6 | "e": { 7 | "transform": "e/${member}" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/cases/nested-dep/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nested", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "dependencies": { 6 | "a": "0.0.1", 7 | "b": "0.0.1", 8 | "c": "0.0.1", 9 | "d": "0.0.1" 10 | }, 11 | "transformModules": { 12 | "a": { 13 | "transform": "a/${member}" 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/cases/nested-dep/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var TransformModulesPlugin = require('../../../lib/index') 3 | 4 | module.exports = { 5 | context: __dirname, 6 | entry: { 7 | app: path.resolve(__dirname, './index') 8 | }, 9 | output: { 10 | path: path.resolve(__dirname, './'), 11 | library: 'normal', 12 | libraryTarget: 'umd' 13 | }, 14 | module: { 15 | rules: [ 16 | { 17 | test: /\.js$/, 18 | use: { 19 | loader: 'babel-loader' 20 | } 21 | }, 22 | { 23 | test: /\.json$/, 24 | loader: 'json-loader' 25 | } 26 | ] 27 | }, 28 | plugins: [ 29 | new TransformModulesPlugin() 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /test/cases/nested/app.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(); 4 | else if(typeof define === 'function' && define.amd) 5 | define([], factory); 6 | else if(typeof exports === 'object') 7 | exports["normal"] = factory(); 8 | else 9 | root["normal"] = factory(); 10 | })(typeof self !== 'undefined' ? self : this, function() { 11 | return /******/ (function(modules) { // webpackBootstrap 12 | /******/ // The module cache 13 | /******/ var installedModules = {}; 14 | /******/ 15 | /******/ // The require function 16 | /******/ function __webpack_require__(moduleId) { 17 | /******/ 18 | /******/ // Check if module is in cache 19 | /******/ if(installedModules[moduleId]) { 20 | /******/ return installedModules[moduleId].exports; 21 | /******/ } 22 | /******/ // Create a new module (and put it into the cache) 23 | /******/ var module = installedModules[moduleId] = { 24 | /******/ i: moduleId, 25 | /******/ l: false, 26 | /******/ exports: {} 27 | /******/ }; 28 | /******/ 29 | /******/ // Execute the module function 30 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 31 | /******/ 32 | /******/ // Flag the module as loaded 33 | /******/ module.l = true; 34 | /******/ 35 | /******/ // Return the exports of the module 36 | /******/ return module.exports; 37 | /******/ } 38 | /******/ 39 | /******/ 40 | /******/ // expose the modules object (__webpack_modules__) 41 | /******/ __webpack_require__.m = modules; 42 | /******/ 43 | /******/ // expose the module cache 44 | /******/ __webpack_require__.c = installedModules; 45 | /******/ 46 | /******/ // define getter function for harmony exports 47 | /******/ __webpack_require__.d = function(exports, name, getter) { 48 | /******/ if(!__webpack_require__.o(exports, name)) { 49 | /******/ Object.defineProperty(exports, name, { 50 | /******/ configurable: false, 51 | /******/ enumerable: true, 52 | /******/ get: getter 53 | /******/ }); 54 | /******/ } 55 | /******/ }; 56 | /******/ 57 | /******/ // getDefaultExport function for compatibility with non-harmony modules 58 | /******/ __webpack_require__.n = function(module) { 59 | /******/ var getter = module && module.__esModule ? 60 | /******/ function getDefault() { return module['default']; } : 61 | /******/ function getModuleExports() { return module; }; 62 | /******/ __webpack_require__.d(getter, 'a', getter); 63 | /******/ return getter; 64 | /******/ }; 65 | /******/ 66 | /******/ // Object.prototype.hasOwnProperty.call 67 | /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; 68 | /******/ 69 | /******/ // __webpack_public_path__ 70 | /******/ __webpack_require__.p = ""; 71 | /******/ 72 | /******/ // Load entry module and return exports 73 | /******/ return __webpack_require__(__webpack_require__.s = 0); 74 | /******/ }) 75 | /************************************************************************/ 76 | /******/ ([ 77 | /* 0 */ 78 | /***/ (function(module, exports, __webpack_require__) { 79 | 80 | "use strict"; 81 | 82 | 83 | Object.defineProperty(exports, "__esModule", { 84 | value: true 85 | }); 86 | exports.pkg = exports.b = exports.a = undefined; 87 | 88 | var _index = __webpack_require__(1); 89 | 90 | var _index2 = _interopRequireDefault(_index); 91 | 92 | var _b = __webpack_require__(2); 93 | 94 | var _b2 = _interopRequireDefault(_b); 95 | 96 | var _package = __webpack_require__(5); 97 | 98 | var _package2 = _interopRequireDefault(_package); 99 | 100 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 101 | 102 | exports.a = _index2.default; 103 | exports.b = _b2.default; 104 | exports.pkg = _package2.default; 105 | 106 | /***/ }), 107 | /* 1 */ 108 | /***/ (function(module, exports, __webpack_require__) { 109 | 110 | "use strict"; 111 | 112 | 113 | Object.defineProperty(exports, "__esModule", { 114 | value: true 115 | }); 116 | exports.default = 'nested a'; 117 | 118 | /***/ }), 119 | /* 2 */ 120 | /***/ (function(module, exports, __webpack_require__) { 121 | 122 | "use strict"; 123 | 124 | 125 | Object.defineProperty(exports, "__esModule", { 126 | value: true 127 | }); 128 | 129 | var _index = __webpack_require__(3); 130 | 131 | var _index2 = _interopRequireDefault(_index); 132 | 133 | var _index3 = __webpack_require__(4); 134 | 135 | var _index4 = _interopRequireDefault(_index3); 136 | 137 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 138 | 139 | exports.default = 'nested b, ' + _index2.default + ', ' + _index4.default; 140 | 141 | /***/ }), 142 | /* 3 */ 143 | /***/ (function(module, exports, __webpack_require__) { 144 | 145 | "use strict"; 146 | 147 | 148 | Object.defineProperty(exports, "__esModule", { 149 | value: true 150 | }); 151 | exports.default = 'nested inner a'; 152 | 153 | /***/ }), 154 | /* 4 */ 155 | /***/ (function(module, exports, __webpack_require__) { 156 | 157 | "use strict"; 158 | 159 | 160 | Object.defineProperty(exports, "__esModule", { 161 | value: true 162 | }); 163 | exports.default = '@dd nested inner a'; 164 | 165 | /***/ }), 166 | /* 5 */ 167 | /***/ (function(module, exports) { 168 | 169 | module.exports = {"name":"nested","version":"0.0.1","main":"index.js","dependencies":{"a":"0.0.1","b":"0.0.1"}} 170 | 171 | /***/ }) 172 | /******/ ]); 173 | }); -------------------------------------------------------------------------------- /test/cases/nested/index.js: -------------------------------------------------------------------------------- 1 | import { index as a } from 'a' 2 | import b from 'b' 3 | import pkg from './package.json' 4 | 5 | export { 6 | a, 7 | b, 8 | pkg 9 | } 10 | -------------------------------------------------------------------------------- /test/cases/nested/node_modules/a/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested a' -------------------------------------------------------------------------------- /test/cases/nested/node_modules/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "a", 3 | "version": "0.0.1", 4 | "main": "index.js" 5 | } -------------------------------------------------------------------------------- /test/cases/nested/node_modules/b/index.js: -------------------------------------------------------------------------------- 1 | import { index as a } from 'a' 2 | import { index as ddA } from '@dd/a' 3 | 4 | export default `nested b, ${a}, ${ddA}` 5 | -------------------------------------------------------------------------------- /test/cases/nested/node_modules/b/node_modules/@dd/a/index.js: -------------------------------------------------------------------------------- 1 | export default '@dd nested inner a' 2 | -------------------------------------------------------------------------------- /test/cases/nested/node_modules/b/node_modules/@dd/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@dd/a", 3 | "version": "0.0.1", 4 | "main": "index.js" 5 | } 6 | -------------------------------------------------------------------------------- /test/cases/nested/node_modules/b/node_modules/a/index.js: -------------------------------------------------------------------------------- 1 | export default 'nested inner a' -------------------------------------------------------------------------------- /test/cases/nested/node_modules/b/node_modules/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "a", 3 | "version": "0.0.2", 4 | "main": "index.js" 5 | } -------------------------------------------------------------------------------- /test/cases/nested/node_modules/b/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "b", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "dependencies": { 6 | "a": "0.0.2", 7 | "@dd/a": "0.0.1" 8 | }, 9 | "transformModules": { 10 | "a": { 11 | "transform": "a/${member}" 12 | }, 13 | "@dd/a": { 14 | "transform": "@dd/a/${member}" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test/cases/nested/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nested", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "dependencies": { 6 | "a": "0.0.1", 7 | "b": "0.0.1" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/cases/nested/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var TransformModulesPlugin = require('../../../lib/index') 3 | 4 | module.exports = { 5 | context: __dirname, 6 | entry: { 7 | app: path.resolve(__dirname, './index') 8 | }, 9 | output: { 10 | path: path.resolve(__dirname, './'), 11 | library: 'normal', 12 | libraryTarget: 'umd' 13 | }, 14 | module: { 15 | rules: [ 16 | { 17 | test: /\.js$/, 18 | use: { 19 | loader: 'babel-loader' 20 | } 21 | }, 22 | { 23 | test: /\.json$/, 24 | loader: 'json-loader' 25 | } 26 | ] 27 | }, 28 | plugins: [ 29 | new TransformModulesPlugin({ 30 | transformModules: { 31 | a: { 32 | transform: 'a/${member}' 33 | }, 34 | b: null 35 | } 36 | }) 37 | ] 38 | } 39 | -------------------------------------------------------------------------------- /test/cases/normal/app.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(); 4 | else if(typeof define === 'function' && define.amd) 5 | define([], factory); 6 | else if(typeof exports === 'object') 7 | exports["normal"] = factory(); 8 | else 9 | root["normal"] = factory(); 10 | })(typeof self !== 'undefined' ? self : this, function() { 11 | return /******/ (function(modules) { // webpackBootstrap 12 | /******/ // The module cache 13 | /******/ var installedModules = {}; 14 | /******/ 15 | /******/ // The require function 16 | /******/ function __webpack_require__(moduleId) { 17 | /******/ 18 | /******/ // Check if module is in cache 19 | /******/ if(installedModules[moduleId]) { 20 | /******/ return installedModules[moduleId].exports; 21 | /******/ } 22 | /******/ // Create a new module (and put it into the cache) 23 | /******/ var module = installedModules[moduleId] = { 24 | /******/ i: moduleId, 25 | /******/ l: false, 26 | /******/ exports: {} 27 | /******/ }; 28 | /******/ 29 | /******/ // Execute the module function 30 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 31 | /******/ 32 | /******/ // Flag the module as loaded 33 | /******/ module.l = true; 34 | /******/ 35 | /******/ // Return the exports of the module 36 | /******/ return module.exports; 37 | /******/ } 38 | /******/ 39 | /******/ 40 | /******/ // expose the modules object (__webpack_modules__) 41 | /******/ __webpack_require__.m = modules; 42 | /******/ 43 | /******/ // expose the module cache 44 | /******/ __webpack_require__.c = installedModules; 45 | /******/ 46 | /******/ // define getter function for harmony exports 47 | /******/ __webpack_require__.d = function(exports, name, getter) { 48 | /******/ if(!__webpack_require__.o(exports, name)) { 49 | /******/ Object.defineProperty(exports, name, { 50 | /******/ configurable: false, 51 | /******/ enumerable: true, 52 | /******/ get: getter 53 | /******/ }); 54 | /******/ } 55 | /******/ }; 56 | /******/ 57 | /******/ // getDefaultExport function for compatibility with non-harmony modules 58 | /******/ __webpack_require__.n = function(module) { 59 | /******/ var getter = module && module.__esModule ? 60 | /******/ function getDefault() { return module['default']; } : 61 | /******/ function getModuleExports() { return module; }; 62 | /******/ __webpack_require__.d(getter, 'a', getter); 63 | /******/ return getter; 64 | /******/ }; 65 | /******/ 66 | /******/ // Object.prototype.hasOwnProperty.call 67 | /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; 68 | /******/ 69 | /******/ // __webpack_public_path__ 70 | /******/ __webpack_require__.p = ""; 71 | /******/ 72 | /******/ // Load entry module and return exports 73 | /******/ return __webpack_require__(__webpack_require__.s = 1); 74 | /******/ }) 75 | /************************************************************************/ 76 | /******/ ([ 77 | /* 0 */ 78 | /***/ (function(module, exports, __webpack_require__) { 79 | 80 | "use strict"; 81 | 82 | 83 | Object.defineProperty(exports, "__esModule", { 84 | value: true 85 | }); 86 | exports.default = 'a'; 87 | 88 | /***/ }), 89 | /* 1 */ 90 | /***/ (function(module, exports, __webpack_require__) { 91 | 92 | "use strict"; 93 | 94 | 95 | Object.defineProperty(exports, "__esModule", { 96 | value: true 97 | }); 98 | exports.b = exports.a = undefined; 99 | 100 | var _index = __webpack_require__(0); 101 | 102 | var _index2 = _interopRequireDefault(_index); 103 | 104 | var _index3 = __webpack_require__(2); 105 | 106 | var _index4 = _interopRequireDefault(_index3); 107 | 108 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 109 | 110 | exports.a = _index2.default; 111 | exports.b = _index4.default; 112 | 113 | /***/ }), 114 | /* 2 */ 115 | /***/ (function(module, exports, __webpack_require__) { 116 | 117 | "use strict"; 118 | 119 | 120 | Object.defineProperty(exports, "__esModule", { 121 | value: true 122 | }); 123 | 124 | var _index = __webpack_require__(0); 125 | 126 | var _index2 = _interopRequireDefault(_index); 127 | 128 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 129 | 130 | exports.default = 'b' + _index2.default; 131 | 132 | /***/ }) 133 | /******/ ]); 134 | }); -------------------------------------------------------------------------------- /test/cases/normal/index.js: -------------------------------------------------------------------------------- 1 | import { index as a } from 'a' 2 | import { index as b } from 'b' 3 | 4 | export { 5 | a, 6 | b 7 | } 8 | -------------------------------------------------------------------------------- /test/cases/normal/node_modules/a/index.js: -------------------------------------------------------------------------------- 1 | export default 'a' -------------------------------------------------------------------------------- /test/cases/normal/node_modules/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "a", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "transformModules": { 6 | "a": { 7 | "transform": "a/${member}" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/cases/normal/node_modules/b/index.js: -------------------------------------------------------------------------------- 1 | import { index } from 'a' 2 | 3 | export default `b${index}` 4 | -------------------------------------------------------------------------------- /test/cases/normal/node_modules/b/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "b", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "dependencies": { 6 | "a": "0.0.1" 7 | }, 8 | "transformModules": { 9 | "b": { 10 | "transform": "b/${member}" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/cases/normal/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "normal", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "dependencies": { 6 | "a": "0.0.1", 7 | "b": "0.0.1" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/cases/normal/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var TransformModulesPlugin = require('../../../lib/index') 3 | 4 | module.exports = { 5 | context: __dirname, 6 | entry: { 7 | app: path.resolve(__dirname, './index') 8 | }, 9 | output: { 10 | path: path.resolve(__dirname, './'), 11 | library: 'normal', 12 | libraryTarget: 'umd' 13 | }, 14 | module: { 15 | rules: [ 16 | { 17 | test: /\.js$/, 18 | loader: 'babel-loader' 19 | } 20 | ] 21 | }, 22 | plugins: [ 23 | new TransformModulesPlugin() 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /test/integration/nested.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var webpackConfig = require('../cases/nested/webpack.config') 4 | var webpackConfig2 = require('../cases/nested-dep/webpack.config') 5 | var expect = require('chai').expect 6 | 7 | describe('nested case', function () { 8 | var warns = [] 9 | var originConsoleWarn = null 10 | beforeEach(function() { 11 | console.warn = function (str) { 12 | warns.push(str) 13 | } 14 | }) 15 | afterEach(function() { 16 | console.warn = originConsoleWarn 17 | warns = [] 18 | originConsoleWarn = null 19 | }) 20 | it('plugin options', function (done) { 21 | webpack(webpackConfig, function (err, stats) { 22 | var compiler = stats.compilation.compiler 23 | var options = compiler.options 24 | var rule = options.module.rules[0] 25 | expect(rule.use.options.plugins.length) 26 | .to.equal(1) 27 | var ret = require('../cases/nested/app.js') 28 | expect(ret.a).to.equal('nested a') 29 | expect(ret.b).to.equal('nested b, nested inner a, @dd nested inner a') 30 | expect(ret.pkg.name).to.equal('nested') 31 | done() 32 | }) 33 | }) 34 | it('nested dep', function (done) { 35 | webpack(webpackConfig2, function (err, stats) { 36 | var compiler = stats.compilation.compiler 37 | var options = compiler.options 38 | var rule = options.module.rules[0] 39 | expect(rule.use.options.plugins.length) 40 | .to.equal(1) 41 | var ret = require('../cases/nested-dep/app.js') 42 | expect(ret.a).to.equal('nested a') 43 | expect(ret.b).to.equal('nested b, nested d, nested e, nested a@0.0.2') 44 | expect(ret.c).to.equal('nested c') 45 | expect(ret.d).to.equal('nested d') 46 | expect(ret.pkg.name).to.equal('nested') 47 | expect(warns.length).to.equal(2) 48 | expect(warns[0]).to.include('[webpack-transform-modules-plugin] There are multiple node modules configured "transformModules" with module: "a"') 49 | expect(warns[0]).to.include('/webpack-transform-modules-plugin/test/cases/nested-dep/node_modules/d') 50 | expect(warns[1]).to.include('[webpack-transform-modules-plugin] There are multiple node modules configured "transformModules" with module: "a"') 51 | expect(warns[1]).to.include('/webpack-transform-modules-plugin/test/cases/nested-dep/node_modules/b/node_modules/a') 52 | done() 53 | }) 54 | }) 55 | }) 56 | -------------------------------------------------------------------------------- /test/integration/normal.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var webpackConfig = require('../cases/normal/webpack.config') 4 | var expect = require('chai').expect 5 | 6 | describe('normal case', function () { 7 | it('webpack options should be correct', function (done) { 8 | webpack(webpackConfig, function (err, stats) { 9 | var compiler = stats.compilation.compiler 10 | var options = compiler.options 11 | var rule = options.module.rules[0] 12 | expect(rule.options.plugins.length) 13 | .to.equal(1) 14 | var ret = require('../cases/normal/app.js') 15 | expect(ret.a).to.equal('a') 16 | expect(ret.b).to.equal('ba') 17 | done() 18 | }) 19 | }) 20 | }) 21 | -------------------------------------------------------------------------------- /test/unit/index.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var expect = require('chai').expect 3 | var TransformModulesPlugin = require('../../lib/index') 4 | 5 | describe('TransformModulesPlugin', function () { 6 | it('new', function () { 7 | var p = new TransformModulesPlugin() 8 | expect(p.key).to.equal('transformModules') 9 | expect(p.initTransformModules).to.be.null 10 | p = new TransformModulesPlugin({ 11 | transformModules: { 12 | a: { 13 | transform: 'a/${member}' 14 | }, 15 | b: { 16 | transform: 'b/${member}' 17 | } 18 | } 19 | }) 20 | expect(Object.keys(p.initTransformModules).length).to.equal(2) 21 | expect(p.initTransformModules.b.transform).to.equal('b/${member}') 22 | }) 23 | it('#apply()', function () { 24 | var p = new TransformModulesPlugin() 25 | var context = path.resolve(__dirname, '../cases/normal') 26 | var compiler = { 27 | _plugins: [], 28 | options: { 29 | context: context, 30 | module: { 31 | rules: [ 32 | { 33 | test: /\.js$/, 34 | loader: 'babel-loader' 35 | }, 36 | { 37 | test: /\.ejs$/, 38 | use: 'babel-loader' 39 | }, 40 | { 41 | test: /\.mjs$/, 42 | use: [ 43 | 'babel-loader', 44 | { 45 | loader: 'babel-loader', 46 | options: { 47 | plugins: ['a-plugin'] 48 | } 49 | }, 50 | { 51 | loader: 'm-loader', 52 | options: { 53 | plugins: ['m-plugin'] 54 | } 55 | } 56 | ] 57 | }, 58 | { 59 | test: /\.vue$/, 60 | loader: 'vue-loader' 61 | }, 62 | { 63 | test: /\.vue$/, 64 | use: [ 65 | 'vue-loader', 66 | { 67 | loader: 'vue-loader', 68 | options: { 69 | loaders: {} 70 | } 71 | }, 72 | { 73 | loader: 'vue-loader', 74 | options: { 75 | loaders: { 76 | js: 'babel-loader' 77 | } 78 | } 79 | }, 80 | { 81 | loader: 'vue-loader', 82 | options: { 83 | loaders: { 84 | js: 'myjs-loader' 85 | } 86 | } 87 | } 88 | ] 89 | }, 90 | { 91 | oneOf: [ 92 | { 93 | use: 'babel-loader', 94 | xx: 'xx' 95 | }, 96 | { 97 | loader: 'babel-loader', 98 | yy: 'yy' 99 | }, 100 | { 101 | loader: 'xx-loader' 102 | } 103 | ] 104 | } 105 | ] 106 | } 107 | }, 108 | plugin: function (name, fn) { 109 | if (Array.isArray(name)) { 110 | return name.forEach(function (n) { 111 | compiler._plugins.push({ 112 | name: n, 113 | cb: fn 114 | }) 115 | }) 116 | } 117 | compiler._plugins.push({ 118 | name: name, 119 | cb: fn 120 | }) 121 | } 122 | } 123 | p.apply(compiler) 124 | compiler._plugins[0].cb(compiler, function () { 125 | var jsPlugins = compiler.options.module.rules[0].options.plugins 126 | var ejsPlugins = compiler.options.module.rules[1].use.options.plugins 127 | var mjsPlugins = compiler.options.module.rules[2].use[0].options.plugins 128 | var mjsPlugins2 = compiler.options.module.rules[2].use[1].options.plugins 129 | var mjsPlugins3 = compiler.options.module.rules[2].use[2].options.plugins 130 | expect(jsPlugins.length) 131 | .to.equal(1) 132 | expect(ejsPlugins.length) 133 | .to.equal(1) 134 | expect(mjsPlugins.length) 135 | .to.equal(1) 136 | expect(mjsPlugins2.length) 137 | .to.equal(2) 138 | expect(mjsPlugins3.length) 139 | .to.equal(1) 140 | expect(jsPlugins[0][1].a) 141 | .to.be.an('object') 142 | expect(jsPlugins[0][1].b) 143 | .to.be.an('object') 144 | expect(mjsPlugins2[0]) 145 | .to.equal('a-plugin') 146 | expect(mjsPlugins3[0]) 147 | .to.equal('m-plugin') 148 | 149 | var vueLoader = compiler.options.module.rules[3] 150 | expectVueLoader(vueLoader) 151 | var vueUseLoader = compiler.options.module.rules[4].use 152 | var vueUseConf = vueUseLoader[0] 153 | var vueUseConf2 = vueUseLoader[1] 154 | var vueUseConf3 = vueUseLoader[2] 155 | var vueUseConf4 = vueUseLoader[3] 156 | expectVueLoader(vueUseConf) 157 | expectVueLoader(vueUseConf2) 158 | expectVueLoader(vueUseConf3) 159 | expectVueLoader(vueUseConf4, 1) 160 | 161 | var oneOf = compiler.options.module.rules[5].oneOf 162 | expect(oneOf[0].use.options.plugins[0][0]) 163 | .to.equal('babel-plugin-transform-modules') 164 | expect(oneOf[1].options.plugins[0][0]) 165 | .to.equal('babel-plugin-transform-modules') 166 | expect(oneOf[2].options) 167 | .to.be.undefined 168 | 169 | function expectVueLoader (vueLoader, index) { 170 | if (!index) { 171 | index = 0 172 | } 173 | expect(vueLoader.options.loaders.js[index].loader) 174 | .to.equal('babel-loader') 175 | expect(vueLoader.options.loaders.js[index].options.plugins[0][0]) 176 | .to.equal('babel-plugin-transform-modules') 177 | expect(vueLoader.options.loaders.js[index].options.plugins[0][1]) 178 | .to.be.an('object') 179 | } 180 | }) 181 | }) 182 | }) 183 | -------------------------------------------------------------------------------- /test/unit/util.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var expect = require('chai').expect 3 | var util = require('../../lib/util') 4 | 5 | describe('util', function () { 6 | it('#getConfig()', function () { 7 | var normalPath = path.resolve(__dirname, '../cases/normal/node_modules/a') 8 | var config = util.getConfig('.', 'transformModules', normalPath) 9 | expect(config.a) 10 | .not.to.be.null 11 | expect(config.b) 12 | .to.be.undefined 13 | normalPath = path.resolve(__dirname, '../cases/normal/node_modules/b') 14 | config = util.getConfig('.', 'transformModules', normalPath) 15 | expect(config.b) 16 | .not.to.be.null 17 | config = util.getConfig('b', 'transformModules', normalPath) 18 | expect(config.b) 19 | .not.to.be.null 20 | expect(config.a) 21 | .to.be.undefined 22 | }) 23 | it('#collectTransformModules()', function () { 24 | var normalPath = path.resolve(__dirname, '../cases/normal') 25 | var appPkg = util.getPkg('.', normalPath) 26 | var initTransformModules = appPkg['transformModules'] 27 | if (!initTransformModules) { 28 | initTransformModules = {} 29 | Object.keys(appPkg.dependencies || {}).forEach(function (k) { 30 | initTransformModules[k] = null 31 | }) 32 | } 33 | var transformModules = {} 34 | util.collectTransformModules(initTransformModules, transformModules, normalPath, 'transformModules') 35 | expect(transformModules.a) 36 | .not.to.be.null 37 | expect(transformModules.b) 38 | .not.to.be.null 39 | 40 | var nestedPath = path.resolve(__dirname, '../cases/nested') 41 | transformModules = {} 42 | util.collectTransformModules({ 43 | "a": { 44 | "transform": "a/${member}" 45 | }, 46 | "b": null 47 | }, transformModules, nestedPath, 'transformModules') 48 | expect(transformModules.a) 49 | .not.to.be.null 50 | expect(transformModules.b) 51 | .to.be.undefined 52 | expect(transformModules['@dd/a']) 53 | .not.to.be.null 54 | }) 55 | it('#loaderNameMatches()', function () { 56 | expect(util.loaderNameMatches({ 57 | loader: 'babel-loader' 58 | }, 'babel-loader')).to.be.true 59 | expect(util.loaderNameMatches({ 60 | loader: '/xx/babel-loader/i' 61 | }, 'babel-loader')).to.be.true 62 | expect(util.loaderNameMatches({ 63 | loader: '@babel-loader/i' 64 | }, 'babel-loader')).to.be.true 65 | expect(util.loaderNameMatches({ 66 | loader: '@babedl-loader/i' 67 | }, 'babel-loader')).to.be.false 68 | }) 69 | }) 70 | --------------------------------------------------------------------------------