即在项目 build 阶段把所有的 console 移除掉
1,安装插件
npm install babel-plugin-transform-remove-console --save-dev
2,声明插件,项目中 babel.config.js 文件
"plugins": [
"transform-remove-console"
]
3,重新运行 build
npm run build
但是,babel.config.js 文件是全局共享的,不管是项目开发阶段(serve)还是项目发布阶段(build),这个配置文件都会生效。
只在项目发布阶段移除所有的 console
项目中 babel.config.js 文件
// 先定义一个空数组,然后判断当前项目是处于开发阶段还是处于发布阶段
// 这是项目在发布阶段需要用到的babel插件
const prodPlugins = []
if (process.env.NODE_ENV === 'production') {
prodPlugins.push("transform-remove-console")
}
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset',
["@babel/preset-env", { "modules": false }]
],
plugins: [
[
"component",
{
libraryName: "element-ui",
styleLibraryName: "theme-chalk"
},
],
...prodPlugins
]
}
最后重新运行build