Last active
October 14, 2022 06:02
-
-
Save hjzheng/969c47661f4636d15a0234b3ce1edc72 to your computer and use it in GitHub Desktop.
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
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
Doesn't seem to work with
await import()
. Do you have any clue why?