Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Leunen committed Aug 16, 2015
0 parents commit 5c95fb8
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .eslintrc
@@ -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": [
]
}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.DS_Store
node_modules/
npm-debug.log

lib/
21 changes: 21 additions & 0 deletions LICENSE.md
@@ -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.
33 changes: 33 additions & 0 deletions README.md
@@ -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.
31 changes: 31 additions & 0 deletions package.json
@@ -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"
]
}
83 changes: 83 additions & 0 deletions src/index.js
@@ -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);
}
}
}
});
};

0 comments on commit 5c95fb8

Please sign in to comment.