Dependency-free, lightweight babel plugin that allows using shortcuts in import
and require
declarations.
npm install babel-plugin-shortcut-import --save-dev
In .babelrc
write:
{
"plugins": [
["babel-plugin-shortcut-import"]
]
}
Then you can define your own shortcuts for directiories. For example:
{
"plugins": [
["babel-plugin-shortcut-import", [
{
"pathPrefix": "#",
"pathSuffix": "src/"
},
{
"pathPrefix": "~",
"pathSuffix": "src/shared/"
}
]]
]
}
Now every shortcutted import like:
import Test from '~test.js';
Will be translated by babel to:
import Test from './shared/test.js';
Translated path is relative, so it may be different depending on the location of the source file.
It works for require
function also.
Inspired by babel-plugin-root-import