Babel plugin to add the opportunity to use import
and require
with root based paths.
// Usually
import SomeExample from '../../../some/example.js';
const OtherExample = require('../../../other/example.js');
// With Babel-Root-Importer
import SomeExample from '~/some/example.js';
const OtherExample = require('~/other/example.js');
npm install babel-root-import --save-dev
Add a .babelrc
file and write:
{
"plugins": [
["babel-root-import"]
]
}
or pass the plugin with the plugins-flag on CLI
babel-node myfile.js --plugins babel-root-import
If you want a custom root because for example all your files are in the src/js folder you can define this in your .babelrc
file
{
"plugins": [
["babel-root-import", {
"rootPathSuffix": "src/js"
}]
]
}
If you don't like the ~
syntax you can just use your own symbol (for example a @
symbol or \
)
{
"plugins": [
["babel-root-import", {
"rootPathPrefix": "@"
}]
]
}
// Now you can use the plugin like:
import foo from '@/my-file';
You can supply an array of the above. The plugin will try each prefix/suffix pair in the order they are defined.
{
"plugins": [
["babel-root-import", [{
"rootPathPrefix": "~", // `~` is the default so you can remove this if you want
"rootPathSuffix": "src/js"
}, {
"rootPathPrefix": "@",
"rootPathSuffix": "other-src/js"
}, {
"rootPathPrefix": "#",
"rootPathSuffix": "../../src/in/parent" // since we suport relative paths you can also go into a parent directory
}]]
]
}
// Now you can use the plugin like:
import foo from '~/my-file';
const bar = require('@/my-file');
If you use eslint-plugin-import to validate imports it may be necessary to instruct ESLint to ignore root imports. Add the following to your .eslintrc file, replacing ~
with your chosen prefix.
{
"rules": {
"import/no-unresolved": [2, { "ignore": ["^[~]"] }]
},
}
If you use Facebook's Flow for type-checking it is necessary to instruct it on how to map your chosen prefix to the root directory. Add the following to your .flowconfig file, replacing ~
with your chosen prefix.
[options]
module.name_mapper='^~\/\(.*\)$' -> '<PROJECT_ROOT>/\1'
Webpack delivers a similar feature, if you just want to prevent end-less import strings you can also define aliases
in the resolve
module, at the moment it doesn't support custom/different symbols and multiple/custom suffixes.
READ MORE
- Compile new version and release again
- Improve support for relative paths (e.g. referencing parent folders via ../) (thanks to @Hizoul)
- Support paths (thanks to @sivael)
- Use relative paths instead of absolute ones (thanks to @nescalante)
- Almost everything changed, thanks to @sheepsteak, @gingur, @olalonde
- Fix custom suffix in path, missing
/
in generated paths
- Support Windows-Filesystem
- Add possibility to configure a custom rootPath-Symbol (instead of
~
you can use whatever you like)
- Add possibility config the custom root path
- Updated plugin to new babel6 API
- Splitted tests and functions into two scopes with single tests
- Removed the "extra-root" param for the .babelrc since this is no yet supported in babel6
Breaking Change to Babel 5
- Updated to Babel 6
- Added integration tests
- Added / updated tests
- Implemented ESlint