Last active
February 26, 2016 13:44
-
-
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'`
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
/** | |
* 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