Expand multiple members import to specific module import, mainly for performance concerns.
import { A, B, C as D } from '../components';import A from '../components/A/A';
import B from '../components/B/B';
import D from '../components/C/C';$ npm install babel-plugin-import-expander --save-dev{
"plugins": [
["import-expander", {
"rules": {
"condition": "^(\\.|\\/).*\\/components$",
"template": "{source}/{name}/{name}"
}
}]
]
}-
rulesOptions can be one object or a list of objects, each object has two properties:
-
conditionOne or multiple string format regular expressions, if the source of ImportDeclaration matches any of them, it will be replaced by the following
template. -
templateUsed to replace hit source with a simple variable placeholder presentation. There are mainly two variable placeholders:
{source}represents the source of ImportDeclaration, and{name}represents imported name.
-
require('babel-core').transform('code', {
plugins: ['import-expander', {
rules: {
condition: '^(\\.|\\/).*\\/components$',
template: '{source}/{name}/{name}'
}
}],
});