第一种方式
安装插件 npm install babel-plugin-transform-remove-console --save-dev
在 babel.config.js 文件中添加配置 transform-remove-console
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
plugins: ['transform-remove-console'],
sourceType: 'unambiguous'
}
只在生产模式下删除console.log
let pluginsT = []
if (process.env.NODE_ENV === "production") {
plugins.push("transform-remove-console");
}
module.exports = {
...
plugins:['..',...pluginsT ]
...
}
完整代码
let plugins = [[
'import',
{
libraryName: 'vant',
libraryDirectory: 'es',
style: (name) => `${name}/style/less`
},
'vant'
]];
if (process.env.NODE_ENV === "production") {
plugins.push("transform-remove-console");
}
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
plugins: plugins,
// common + es6 共存
sourceType: 'unambiguous'
}
第二种方式
找到 build / webpack.prod.conf.js
在build下面的webpack.prod.conf.js
里面找到这里添加上
drop_console: true,
pure_funcs: ['console.log']
完整代码
plugins: [
...
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false,
drop_console: true,
pure_funcs: ['console.log']
}
},
sourceMap: config.build.productionSourceMap,
parallel: true
})
...
网友评论