TerserWebpackPlugin在webpack5已经包含了terser-webpack-plugin
,webpack4才需要单独安装。
webpack4需要执行:
npm install terser-webpack-plugin --save-dev
webpack5不需要执行上面代码。
optimization.minimizer
配置方法有两种:
第一种:
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
},
}),
],
},
};
第二种:
module.exports = {
optimization: {
minimizer: [
(compiler) => {
const TerserPlugin = require('terser-webpack-plugin');
new TerserPlugin({
/* your config */
}).apply(compiler);
},
],
},
};
具体查看:optimization.minimizer。