README
Babel Module Name Mapper Plugin
This plugin adds a Jest-like moduleNameMapper option as a babel plugin.
It mostly adds a <pkgDir>
string token that will resolve to the dirname of the closest package.json
in order to support monorepos, (which is lacking for now in both Jest/Webpack).
It also supports the classic <rootDir>
string token.
Usage
This plugin allows you to drop relative path usage for prefixed paths.
// Instead of:
import fooHelper from './../../../helpers/foo.js';
// You can write:
import fooHelper from 'src/helpers/example.js';
// No more relative path hell 🎉
Quickstart
yarn add --dev babel-plugin-module-name-mapper
- Add the plugin to your
.babel.config.js
(or.babelrc
)
module.exports = {
presets: [['@babel/preset-env']],
plugins: [
[
'module-name-mapper',
{
moduleNameMapper: {
'^src/(.*)': '<pkgDir>/src/$1'
}
}
]
]
};
Other examples
module.exports = {
presets: [['@babel/preset-env']],
plugins: [
[
'module-name-mapper',
{
moduleNameMapper: {
'^src/(.*)': '<pkgDir>/src/$1',
'^image![a-zA-Z0-9$_-]+