webpack 4.x 移除 console.log 解决方案
- 只删除
console.log,但保留其他控制台(推荐✅)
pure_funcs: ['console.log']
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
// webpack.config.js
module.exports = {
// ...
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: {
pure_funcs: [
'console.log',
// 'console.error',
// 'console.warn',
// ...
],
},
// Make sure symbols under `pure_funcs`,
// are also under `mangle.reserved` to avoid mangling.
mangle: {
reserved: [
'console.log',
// 'console.error',
// 'console.warn',
// ...
],
},
},
}),
],
},
// ...
}
- 删除所有控制台,包括(console.log、console.error、console.warn、...等)(不推荐??♂️)
drop_console: 真,
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
// webpack.config.js
module.exports = {
// ...
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: {
drop_console: true,
},
},
}),
],
},
// ...
}
所有控制台
Chrome Google,版本 88.0.4324.192(官方构建)(x86_64)
参考
https://github.com/mishoo/UglifyJS#compress-options