很多时候测试的时候,我们为了方便找bug,会在代码之间穿插着console.log(),来在页面控制台进行打印,寻找问题的出处。但项目在打包的时候有时会全去掉,但那样又会遗漏几个。这样如果让用户看到的话,感觉不太好,假如包含重要信息,岂不是更不好。但要是真的去掉了,以后再一个一个加,去找问题出处,可能有些麻烦,这样的话,webpack打包时的一段配置就会帮你解决这个问题。
作者:风后奇门_
链接:juejin.cn/post/697281…
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
const { defineConfig } = require('@vue/cli-service');
module.exports = defineConfig({
...,
chainWebpack: config => {
if (process.env.NODE_ENV === 'production') { // 生产
// 删除console、debugger、注释
const terser = config.optimization.minimizer('terser');
terser.tap(args => {
const { terserOptions } = args[0];
Object.assign(
terserOptions,
{
compress: {
...terserOptions.compress,
drop_console: true,
drop_debugger: true
},
format: {
comments: /@license/i
}
}
);
return args;
});
}
}
});