babel-plugin-module-alias
Advanced tools
Comparing version 0.2.0 to 1.0.0
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
7
1
64
6222
7
63
- Removedbabel-core@^5.8.22
- Removedglob@^5.0.14
- Removedlodash@^3.10.1
- Removedacorn@5.7.4(transitive)
- Removedalign-text@0.1.4(transitive)
- Removedalter@0.2.0(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedast-traverse@0.1.1(transitive)
- Removedast-types@0.8.120.9.6(transitive)
- Removedbabel-core@5.8.38(transitive)
- Removedbabel-plugin-constant-folding@1.0.1(transitive)
- Removedbabel-plugin-dead-code-elimination@1.0.2(transitive)
- Removedbabel-plugin-eval@1.0.1(transitive)
- Removedbabel-plugin-inline-environment-variables@1.0.1(transitive)
- Removedbabel-plugin-jscript@1.0.4(transitive)
- Removedbabel-plugin-member-expression-literals@1.0.1(transitive)
- Removedbabel-plugin-property-literals@1.0.1(transitive)
- Removedbabel-plugin-proto-to-assign@1.0.4(transitive)
- Removedbabel-plugin-react-constant-elements@1.0.3(transitive)
- Removedbabel-plugin-react-display-name@1.0.3(transitive)
- Removedbabel-plugin-remove-console@1.0.1(transitive)
- Removedbabel-plugin-remove-debugger@1.0.1(transitive)
- Removedbabel-plugin-runtime@1.0.7(transitive)
- Removedbabel-plugin-undeclared-variables-check@1.0.2(transitive)
- Removedbabel-plugin-undefined-to-void@1.1.6(transitive)
- Removedbabylon@5.8.38(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbluebird@2.11.0(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbreakable@1.0.0(transitive)
- Removedcamelcase@1.2.1(transitive)
- Removedcenter-align@0.1.3(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcliui@2.1.0(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcommoner@0.10.8(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedconvert-source-map@1.9.0(transitive)
- Removedcore-js@1.2.7(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removeddefined@1.0.1(transitive)
- Removeddefs@1.1.1(transitive)
- Removeddetect-indent@3.0.1(transitive)
- Removeddetective@4.7.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@2.7.33.1.3(transitive)
- Removedesprima-fb@15001.1001.0-dev-harmony-fb(transitive)
- Removedesutils@2.0.3(transitive)
- Removedfs-readdir-recursive@0.1.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedglob@5.0.15(transitive)
- Removedglobals@6.4.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhome-or-tmp@1.0.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinvert-kv@1.0.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-core-module@2.16.1(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedis-integer@1.0.7(transitive)
- Removedjs-tokens@1.0.1(transitive)
- Removedjsesc@0.5.0(transitive)
- Removedjson5@0.4.0(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@1.0.4(transitive)
- Removedlcid@1.0.0(transitive)
- Removedleven@1.0.2(transitive)
- Removedlodash@3.10.1(transitive)
- Removedlongest@1.0.1(transitive)
- Removedminimatch@2.0.10(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedms@2.0.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedos-locale@1.4.0(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedoutput-file-sync@1.1.2(transitive)
- Removedpath-exists@1.0.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedprivate@0.1.8(transitive)
- Removedq@1.5.1(transitive)
- Removedrecast@0.10.330.11.23(transitive)
- Removedregenerate@1.4.2(transitive)
- Removedregenerator@0.8.40(transitive)
- Removedregexpu@1.3.0(transitive)
- Removedregjsgen@0.2.0(transitive)
- Removedregjsparser@0.1.5(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedrepeating@1.1.3(transitive)
- Removedresolve@1.22.10(transitive)
- Removedright-align@0.1.3(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedsimple-fmt@0.1.0(transitive)
- Removedsimple-is@0.2.0(transitive)
- Removedslash@1.0.0(transitive)
- Removedsource-map@0.1.320.5.7(transitive)
- Removedsource-map-support@0.2.10(transitive)
- Removedstable@0.1.8(transitive)
- Removedstringmap@0.2.2(transitive)
- Removedstringset@0.2.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedthrough@2.3.8(transitive)
- Removedto-fast-properties@1.0.3(transitive)
- Removedtrim-right@1.0.1(transitive)
- Removedtry-resolve@1.0.1(transitive)
- Removedtryor@0.1.2(transitive)
- Removeduser-home@1.1.1(transitive)
- Removedwindow-size@0.1.4(transitive)
- Removedwordwrap@0.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedy18n@3.2.2(transitive)
- Removedyargs@3.27.0(transitive)