Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tommy Leunen
committed
Aug 16, 2015
0 parents
commit 5c95fb8
Showing
6 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
}, | ||
"rules": { | ||
"brace-style": [2, "stroustrup", { "allowSingleLine": true }], | ||
"camelcase": [2, {"properties": "never"}], | ||
"comma-style": [2, "last"], | ||
"indent": 2, | ||
"no-underscore-dangle": 0, | ||
"quotes": [2, "single", "avoid-escape"], | ||
"semi": [2, "always"], | ||
"space-before-function-paren": [2, "never"] | ||
}, | ||
"plugins": [ | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
node_modules/ | ||
npm-debug.log | ||
|
||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Tommy Leunen <tommy.leunen@gmail.com> (tommyleunen.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Module alias plugin for Babel | ||
|
||
A [babel](http://babeljs.io) plugin to easily map directories as different directories when your required a module. | ||
Useful when you don't want to write relative path yourself. | ||
|
||
|
||
## Usage | ||
|
||
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'. | ||
|
||
To do so, first install babel and the plugin | ||
``` | ||
$ npm install --save babel babel-plugin-module-alias | ||
``` | ||
|
||
Then, the recommended way of using it is by using the file `.babelrc` to setup the configuration for Babel. | ||
``` | ||
{ | ||
"plugins": [ | ||
"babel-plugin-module-alias" | ||
], | ||
"extra": { | ||
"module-alias": [ | ||
{ "src": "./src/utils", "expose": "utils" } | ||
] | ||
} | ||
} | ||
``` | ||
_Note:_ the section `extra` is a custom section commonly used by plugins to take options. There's currently no better way in Babel to pass options to plugins. | ||
|
||
## License | ||
|
||
MIT, see [LICENSE.md](/LICENSE.md) for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "babel-plugin-module-alias", | ||
"version": "0.0.0", | ||
"main": "lib/index.js", | ||
"description": "Babel plugin to rewrite the path in require() and ES6 import", | ||
"repository": "tleunen/babel-plugin-module-alias", | ||
"author": { | ||
"name": "Tommy Leunen", | ||
"email": "tommy.leunen@gmail.com", | ||
"url": "http://tommyleunen.com" | ||
}, | ||
"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": [ | ||
"babel", | ||
"babel-plugin", | ||
"module", | ||
"alias", | ||
"rewrite", | ||
"rename" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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; | ||
} | ||
|
||
|
||
export default function({ Plugin, types: t }) { | ||
function transformRequireCall(context, call) { | ||
if( | ||
!t.isIdentifier(call.callee, {name: 'require'}) && | ||
!( | ||
t.isMemberExpression(call.callee) && | ||
t.isIdentifier(call.callee.object, {name: 'require'}) | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
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)]); | ||
} | ||
} | ||
} | ||
|
||
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) | ||
); | ||
} | ||
} | ||
} | ||
|
||
return new Plugin(pluginName, { | ||
visitor: { | ||
CallExpression: { | ||
exit(node, parent, scope) { | ||
return transformRequireCall(this, node); | ||
} | ||
}, | ||
ImportDeclaration: { | ||
exit(node) { | ||
return transformImportCall(this, node); | ||
} | ||
} | ||
} | ||
}); | ||
}; |