babel-plugin-module-alias
Advanced tools
Comparing version
108
lib/index.js
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var path = require('path'); | ||
var glob = require('glob'); | ||
var _ = require('lodash'); | ||
var pluginName = 'module-alias'; | ||
var filesMap = {}; | ||
function mapModule(context, module) { | ||
if (!_.keys(filesMap).length) { | ||
_.each(context.state.opts.extra[pluginName] || [], function (moduleMapData) { | ||
filesMap[moduleMapData.expose] = filesMap[moduleMapData.expose] || { | ||
src: moduleMapData.src, | ||
files: [] | ||
}; | ||
var src = path.join(moduleMapData.src, '**', '*'); | ||
_.merge(filesMap[moduleMapData.expose].files, glob.sync(src)); | ||
}); | ||
} | ||
var moduleSplit = module.split('/'); | ||
if (moduleSplit.length < 2 || !filesMap.hasOwnProperty(moduleSplit[0])) { | ||
return; | ||
} | ||
var currentFile = context.state.opts.filename; | ||
moduleSplit[0] = filesMap[moduleSplit[0]].src; | ||
var moduleMapped = path.relative(path.dirname(currentFile), path.normalize(moduleSplit.join('/'))); | ||
if (moduleMapped[0] != '.') moduleMapped = './' + moduleMapped; | ||
return moduleMapped; | ||
} | ||
exports['default'] = function (_ref) { | ||
var Plugin = _ref.Plugin; | ||
exports.default = function (_ref) { | ||
var t = _ref.types; | ||
function transformRequireCall(context, call) { | ||
if (!t.isIdentifier(call.callee, { name: 'require' }) && !(t.isMemberExpression(call.callee) && t.isIdentifier(call.callee.object, { name: 'require' }))) { | ||
return; | ||
function transformRequireCall(nodePath, state, filesMap) { | ||
if (!t.isIdentifier(nodePath.node.callee, { name: 'require' }) && !(t.isMemberExpression(nodePath.node.callee) && t.isIdentifier(nodePath.node.callee.object, { name: 'require' }))) { | ||
return null; | ||
} | ||
var moduleArg = call.arguments[0]; | ||
if (moduleArg && moduleArg.type === 'Literal') { | ||
var module = mapModule(context, moduleArg.value); | ||
if (module) { | ||
return t.callExpression(call.callee, [t.literal(module)]); | ||
var moduleArg = nodePath.node.arguments[0]; | ||
if (moduleArg && moduleArg.type === 'StringLiteral') { | ||
var modulePath = mapModule(moduleArg.value, state, filesMap); | ||
if (modulePath) { | ||
nodePath.replaceWith(t.callExpression(nodePath.node.callee, [t.stringLiteral(modulePath)])); | ||
} | ||
@@ -57,8 +24,8 @@ } | ||
function transformImportCall(context, call) { | ||
var moduleArg = call.source; | ||
if (moduleArg && moduleArg.type === 'Literal') { | ||
var module = mapModule(context, moduleArg.value); | ||
if (module) { | ||
return t.importDeclaration(call.specifiers, t.literal(module)); | ||
function transformImportCall(nodePath, state, filesMap) { | ||
var moduleArg = nodePath.node.source; | ||
if (moduleArg && moduleArg.type === 'StringLiteral') { | ||
var modulePath = mapModule(moduleArg.value, state, filesMap); | ||
if (modulePath) { | ||
nodePath.replaceWith(t.importDeclaration(nodePath.node.specifiers, t.stringLiteral(modulePath))); | ||
} | ||
@@ -68,19 +35,46 @@ } | ||
return new Plugin(pluginName, { | ||
return { | ||
visitor: { | ||
CallExpression: { | ||
exit: function exit(node, parent, scope) { | ||
return transformRequireCall(this, node); | ||
exit: function exit(nodePath, state) { | ||
return transformRequireCall(nodePath, state, createFilesMap(state)); | ||
} | ||
}, | ||
ImportDeclaration: { | ||
exit: function exit(node) { | ||
return transformImportCall(this, node); | ||
exit: function exit(nodePath, state) { | ||
return transformImportCall(nodePath, state, createFilesMap(state)); | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
}; | ||
; | ||
module.exports = exports['default']; | ||
var path = require('path'); | ||
function createFilesMap(state) { | ||
var result = {}; | ||
if (!Array.isArray(state.opts)) { | ||
state.opts = [state.opts]; | ||
} | ||
state.opts.forEach(function (moduleMapData) { | ||
result[moduleMapData.expose] = moduleMapData.src; | ||
}); | ||
return result; | ||
} | ||
function mapModule(modulePath, state, filesMap) { | ||
var moduleSplit = modulePath.split('/'); | ||
if (!filesMap.hasOwnProperty(moduleSplit[0])) { | ||
return null; | ||
} | ||
var currentFile = state.file.opts.filename; | ||
moduleSplit[0] = filesMap[moduleSplit[0]]; | ||
var moduleMapped = path.relative(path.dirname(currentFile), path.normalize(moduleSplit.join('/'))); | ||
if (moduleMapped[0] !== '.') moduleMapped = './' + moduleMapped; | ||
return moduleMapped; | ||
} |
{ | ||
"name": "babel-plugin-module-alias", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"main": "lib/index.js", | ||
@@ -13,12 +13,2 @@ "description": "Babel plugin to rewrite the path in require() and ES6 import", | ||
"license": "MIT", | ||
"dependencies": { | ||
"babel-core": "^5.8.22", | ||
"glob": "^5.0.14", | ||
"lodash": "^3.10.1" | ||
}, | ||
"scripts": { | ||
"build": "babel-plugin build", | ||
"push": "babel-plugin publish", | ||
"test": "babel-plugin test" | ||
}, | ||
"keywords": [ | ||
@@ -31,3 +21,19 @@ "babel", | ||
"rename" | ||
] | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-cli": "^6.0.14", | ||
"babel-core": "^6.0.14", | ||
"babel-eslint": "^4.1.3", | ||
"babel-preset-es2015": "^6.0.14", | ||
"eslint": "^1.7.3", | ||
"eslint-config-airbnb": "^0.1.0", | ||
"mocha": "^2.3.3" | ||
}, | ||
"scripts": { | ||
"build": "babel-plugin build", | ||
"push": "babel-plugin publish", | ||
"test": "mocha --compilers js:babel-core/register", | ||
"posttest": "eslint src test" | ||
} | ||
} |
@@ -9,3 +9,8 @@ # Module alias plugin for Babel | ||
Instead of writing `var m = require('../../../../utils/myUtils')` or `import m from '../../../../myUtils'`. You could just use `var m = require('utils/myUtils')` or the equivalent ES6 import `import m from 'utils/myUtils'`. | ||
Instead of writing `var m = require('../../../../utils/myUtils')` or `import m from '../../../../myUtils'`. This plugin will allow you to set an alias to access your plugin. | ||
```js | ||
var myUtils = require('utils/myUtils'); | ||
// or | ||
import myUtils from 'utils/myUtils'; | ||
``` | ||
@@ -18,3 +23,3 @@ To do so, first install babel and the plugin | ||
Then, the recommended way of using it is by using the file `.babelrc` to setup the configuration for Babel. | ||
``` | ||
```json | ||
{ | ||
@@ -34,3 +39,3 @@ "plugins": [ | ||
``` | ||
```json | ||
{ | ||
@@ -37,0 +42,0 @@ "plugins": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
-100%7
16.67%1
-50%64
8.47%6222
-1.75%7
Infinity%63
-10%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed