-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |