Skip to content

Commit

Permalink
add babel-plugin-czq-import.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenquan committed Aug 16, 2018
1 parent 1653b65 commit 3f1fbf5
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions babel-plugin-czq-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//实现模块的按需加载

//import {Button} from 'antd';

//babel-plugin-import

//import {Button, ALert} from 'antd';

//import Button from 'antd/lib/button'

//import Alert from 'antd/lib/alert';
const babel = require('babel-core');//babel核心解析库
const t = require('babel-types');//babel类型转化库
let code = `import {Button, ALert} from 'antd'`;
let importPlugin = {
visitor: {
ImportDeclaration(path){
let {node} = path;
//console.log(node);
let source = node.source.value;
let specifiers = node.specifiers;
if(!t.isImportDefaultSpecifier(specifiers[0])){
specifiers = specifiers.map(specifier=>{
return t.importDeclaration(
[t.importDefaultSpecifier(specifier.local)],
t.stringLiteral(`${source}/lib/${specifier.local.name.toLowerCase()}`)
)
});
path.replaceWithMultiple(specifiers);
}
}
}
}
let r = babel.transform(code, {
plugins: [
importPlugin
]
})

module.exports = importPlugin;

0 comments on commit 3f1fbf5

Please sign in to comment.