Skip to content

Instantly share code, notes, and snippets.

@jonniedarko
Last active February 26, 2016 13:44
Show Gist options
  • Save jonniedarko/8365f37c1f46c2051392 to your computer and use it in GitHub Desktop.
Save jonniedarko/8365f37c1f46c2051392 to your computer and use it in GitHub Desktop.
Allows us to reference files relative to our JS_DIR wthing our imports using '~' e.g. `'~/main.js'` would import the file `JS_DIR + '/main.js'`
/**
* Allows us to referece files relative to our JS_DIR wthing our imports uing '~'
*
* e.g. '~/main.js' would import the file JS_DIR +'/main.js'
*/
var Path = require('path')
var JS_DIR = '/app/js';
/*
// For referece : Babel 5 version
module.exports = function (babel) {
// get the working directory
var cwd = process.cwd();
return new babel.Transformer("babel-plugin-project-relative-import", {
ImportDeclaration: function(node, parent) {
// probably always true, but let's be safe
if (!babel.types.isLiteral(node.source)) {
return node;
}
var ref = node.source.value;
// ensure a value, make sure it's not home relative e.g. ~/foo
if (!ref || ref[0] !== '~') {
return node;
}
node.source.value = cwd + JS_DIR + node.source.value.slice(1);
return node;
}
});
};
*/
module.exports = function () {
var cwd = process.cwd();
return {
visitor: {
ImportDeclaration: function(path, state) {
var importPath = path.node.source.value;
if (importPath[0]=== '~') {
path.node.source.value = cwd + JS_DIR + importPath.slice(1);
}
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment