A Babel plugin for requiring files relative to the project root.
In: /path/to/project/containers/Register/Register.js
const BaseAuthForm = require('~/components/BaseAuthForm');
or
import BaseAuthForm from '~/components/BaseAuthForm';
Out
var BaseAuthForm = require('../../components/BaseAuthForm');
$ npm install babel-plugin-require-root-rewrite --save-dev
.babelrc
{
"plugins": [
["babel-plugin-require-root-rewrite", {
"pattern": "^~/",
"basePath": "lib",
"overrides": [
"api",
"server"
]
}]
]
}
$ babel --plugins babel-plugin-require-root-rewrite script.js
require('babel-core').transform('code', {
plugins: ['babel-plugin-require-root-rewrite']
});