今天 review 代码,发现要重构的项目中,存在很多无用的 console.log(xxx) 的代码。
所以,就写了这篇文章,分享给大家。
现在思路就是,拿到代码中所有的 console.log(xxx),并对其进行删除,提供两种方案:
方案一
webpack 的 loader 本质上其实就是一个函数,我们可以在这个函数内部,根据正则匹配出我们想删除的字符串,对其进行替换。
自定义 loaders/ignore-console-log-loader.js 代码很简单,如下:
const reg = /(console.log\()(.*)(\))/g
module.exports = function (sourceCode) {
return sourceCode.replace(reg, '')
}
使用姿势,在webpack.config.js 配置文件中添加一下自定义的 loader :
module: {
rules: [
{
test: /\.js$/,
loader: ['ignore-console-log-loader'],
},
]
},
resolveLoader: {
// 指定自定义 loader 的位置目录
modules: ['node_modules', path.resolve(__dirname, 'loaders')]
},
使用前后源代码打包对比:
const name = '爱情'
console.log(name)
使用前(正常打包)<