webpack优化,使用terser-webpack-plugin插件,删除代码中所有的console语句

安装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

猜你喜欢

转载自blog.csdn.net/weixin_39550080/article/details/129318652