vue2 + vue-cli4
webpack4会自带terser-webpack-plugin插件。
vue.config.js文件
module.exports = {
// ...
chainWebpack(config) {
// ...
// 清除console
config.optimization.minimizer('terser').tap(options => {
options[0].terserOptions.compress.drop_console = true
return options
})
//...
}
//...
}
vue3 + vite
vite.config.ts文件
export default defineConfig({
// ...
esbuild: {
// ...
pure: ['console.log'], // 安全
// drop: ['console'], // esbuild官方说有风险
// ...
},
// ...
})
Esbuild#drop:For example, you can mark console.log as pure using --pure:console.log. This will cause these API calls to be removed safely when minification is enabled.