forked from luckyxutao/2020-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin-import.js
30 lines (29 loc) · 959 Bytes
/
plugin-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
let babel = require('@babel/core');
let t = require('babel-types');
const code = "import {Button,Alert} from 'antd'";
module.exports = {
visitor: {
ImportDeclaration(path) {
let { node } = path;
let source = node.source.value;
if (!/antd$/.test(source)) {
return;
}
let specifiers = node.specifiers;
if (!t.isImportDefaultSpecifier(specifiers[0])) {
specifiers = specifiers.map((v, i) => {
return t.importDeclaration(
[t.importDefaultSpecifier(v.local)],
t.stringLiteral(`${source}/lib/${v.local.name.toLowerCase()}`)
)
})
path.replaceWithMultiple(specifiers);
}
}
}
}
// const ast = babel.transform(code, {
// plugins: [importPlugin]
// });
// const newCode = ast.code;
// console.log(newCode)