Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NanissaDev/f6c7a6af9d858ade0f239a6bd5c59a1b to your computer and use it in GitHub Desktop.
Save NanissaDev/f6c7a6af9d858ade0f239a6bd5c59a1b to your computer and use it in GitHub Desktop.
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