问题: 项目到了发布阶段平时很多测试用的console.log,应该如何解决
总不能一个一个去代码里面找来删除,毕竟可能以后测试的时候会用到.
解决: 使用插件 babel-plugin-transform-remove-console
Installation
npm install babel-plugin-transform-remove-console --save-dev
使用:
在babel.config.js中加一行
module
.exports
= {
presets
: [
'@vue/cli-plugin-babel/preset'
],
plugins
: [
[
'component',
{
libraryName
: 'element-ui',
styleLibraryName
: 'theme-chalk'
}
],
"transform-remove-console"
]
}
注意:
该插件无论在发布阶段还是开发阶段,都会自行屏蔽所有的控制台打印,所以最好的做法是 通过判断当前处于什么模式来决定是否移除打印语句
const prodPlugins
= []
if(process
.env
.NODE_ENV=='production') {
prodPlugins
.push('transform-remove-console')
}
module
.exports
= {
presets
: [
'@vue/cli-plugin-babel/preset'
],
plugins
: [
[
'component',
{
libraryName
: 'element-ui',
styleLibraryName
: 'theme-chalk'
}
],
...prodPlugins
]
}