1.安装
npm install babel-plugin-transform-remove-console --save-dev
2.在babel.config.js的plugins中配置"transform-remove-console"
plugins: [
[
'component',
{
libraryName: 'element-ui',
styleLibraryName: 'theme-chalk'
}
],
'transform-remove-console'
]
3.重新启动项目【我是重新启动了vscode】,执行npm run build打包后的文件中,console.*被移除
如果只在发布阶段移除所有的console,需要对配置做如下修改
// 这是项目发布阶段需要用到的babel插件
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
]
}
附:https://www.npmjs.com/package/babel-plugin-transform-remove-console