Forked from hjzheng/plugin-babel-import-chunkname.js
Created
January 10, 2020 05:44
-
-
Save NanissaDev/f6c7a6af9d858ade0f239a6bd5c59a1b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function addComments(arg, name) { | |
| // 当参数前的注释不存在的情况, 加入 webpackChunkName 注释 | |
| if (!arg.leadingComments) { | |
| arg.leadingComments = [ | |
| { | |
| type: 'CommentBlock', | |
| value: ` webpackChunkName: '${name}' `, | |
| }, | |
| ] | |
| } | |
| } | |
| module.exports = function(babel) { | |
| const { types: t } = babel | |
| return { | |
| name: 'ast-transform', // not required | |
| visitor: { | |
| CallExpression: function(path) { | |
| const { node } = path | |
| if (t.isImport(node.callee)) { | |
| const arg0 = node.arguments[0] | |
| const name = arg0.value | |
| addComments(arg0, name.replace(/.*\/(.*)$/, '$1')) | |
| } | |
| }, | |
| }, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment