Use aliases to resolve imports. Similar to webpack's resolve-alias.
Inspired by babel-plugin-module-resolver
!important!
Affects only import
statements, not require
calls.
Webpack has convenient feature resolve-alias. You can avoid the double dots in your import declarations animals/Cat
instead of ../../../animals/Cat
.
Using webpack for building packages is ok, but when it comes to running tests, you need some untrivial hacks to make this aliasing feature work. Why don't we just delegate it to Babel? So do we with this plugin.
You can just set up the aliases in the plugin setting in .babelrc
and they'll be used by all the Babel invokers, let's say the builder and the test runner.
- There is no
root
option, hence no multiple roots too. The root is always only one and it's the root of the project - Only
import
statements are handled.require
calls remain unchanged.
{
plugins: [
['import-resolve-alias', {
alias: {
'brilliant-lib': './app/lib', // the dot means path is relative to the project root
'brilliant': './app/src',
'one': 'another'
}
}]
]
}
Let us having the config from the previous section and project root is /home/fred/proj/
.
import brilliant-lib/xyz
becomesimport /home/fred/proj/app/lib/xyz
import brilliant-lib/xyz
becomesimport /home/fred/proj/app/lib/xyz
import one
becomesimport another
Generally the table from webpack documentation is the reference.