安装terser-webpack-plugin 插件
terser-webpack-plugin 插件官网: https://www.npmjs.com/package/terser-webpack-plugin
首先,你需要安装 terser-webpack-plugin:
npm install terser-webpack-plugin --save-dev
然后将插件添加到你的 webpack 配置文件中。例如:
webpack.config.js:
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
configureWebpack: config => {
config.optimization = {
minimize: true,
minimizer: [new TerserPlugin({
terserOptions: {
mangle: {
safari10: true,
},
compress: {
// 是否在UglifyJS删除没有用到的代码时输出警告信息,默认为输出,可以设置为false关闭这些作用不大的警告
warnings: false,
// 是否删除代码中所有的console语句,默认为不删除,开启后,会删除所有的console语句
drop_console: true,
pure_funcs: ['console.log'] // 移除console
}
}
})],
}
}
上述包含一些常用的配置
- 删除代码中所有的
console
语句——drop_console: true
;