Module resolver plugin for Babel
npm install babel-plugin-module-resolvebabel-plugin-module-resolve> A Babel plugin to import similar to webpack alias.
> Implemented based on plugin enhanced-resolve.
Simplify the require import System.import System.import path.
``js
// simple @utils ->
import t from '@utils';
// instead
import t from '../../utils/index';
const t = require('@utils');
// instead
const t = require('../../utils/index');
`
Install the plugin
`
npm install --save-dev babel-plugin-module-resolve
``
or
`
yarn add --dev babel-plugin-module-resolve
``
Specify the plugin in your .babelrc with the custom root or alias. Here's an example:
`json`
{
"plugins": [
[
"module-resolve",
{
"roots": ["./src"],
"alias": {
"@/": ["./"],
"assets": ["./assets"]
}
}
]
]
}
Also supports the use of paths configuration in jsconfig and tsconfig.
`json`
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@/": ["./"]
}
}
}
#### roots
Parsed root directory
* Type: arraycwd | compilerOptions.baseUrl
* Default:
#### alias
Alias configuration
* Type: object{ @: resolve('src') } | compilerOptions.paths
* Default:
#### extensions
Parse file types
* Type: array['.js', '.jsx', '.es', '.es6', '.mjs', '.ts', '.tsx']`
* Default: