Ai
0 Star 0 Fork 0

嗷大张/webpack5-study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
unconsole-loader.js 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
aodazhang 提交于 2023-01-05 15:57 +08:00 . 代码更新
const parser = require('@babel/parser')
const traverse = require('@babel/traverse').default
const generate = require('@babel/generator').default
module.exports = function (source) {
/************************** parse 解析阶段 **************************/
/**
* 调用 parser.parse(input, options) 根据源代码生成对应 AST
* - input:源代码字符串
*/
const ast = parser.parse(source, { sourceType: 'unambiguous' })
/************************** transform 转换阶段 **************************/
/**
* 调用 traverse(parent, opts) 遍历各个节点,当命中指定类型节点时触发 callback
* - parent:parser 生成的 AST
*/
traverse(ast, {
// 遍历到 debugger 关键字会调用该函数
DebuggerStatement(path, state) {
path.remove() // 移除该节点
},
// 遍历到调用表达式会调用该函数
CallExpression(path, state) {
// 1.获取 CallExpression 下的 callee 路径
const calleePath = path.get('callee')
// 2.检测 callee 路径是否部分匹配 console
if (calleePath?.matchesPattern('console', true)) {
// 2-1.获取当前路径父节点 ExpressionStatement 下的前缀注释
const leadingComments = path.parentPath.node.leadingComments
// 2-2.是否存在关键字注释
let hasToken = false
if (leadingComments?.length) {
for (let i = 0; i < leadingComments.length; i++) {
const commentNode = leadingComments[i]
// 识别 行注释 和 块注释
if (!['CommentLine', 'CommentBlock'].includes(commentNode.type)) {
continue
}
// 是否存在关键字注释
if (/\bunconsole-disable-next-line\b/g.test(commentNode.value)) {
hasToken = true
break
}
}
}
// 2-3.不存在关键字注释则移除该节点
!hasToken && path.remove()
}
}
})
/************************** generate 生成阶段 **************************/
/**
* 调用 generate(ast) 根据 AST 生成对应目标代码
*/
const { code } = generate(ast)
return code
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aodazhang/webpack5-study.git
git@gitee.com:aodazhang/webpack5-study.git
aodazhang
webpack5-study
webpack5-study
master

搜索帮助