Webpack 4 - How to configure minimize?

Webpack 4 comes with the following statement:

Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

Fair enough, but I cannot find any information about configuring the UglifyJsPlugin instance running under the hood, for example to change the cache directory. Can this be done?

82308 次浏览

您应该选中 p选项: https://webpack.js.org/guides/production/#cli-alternatives: 这个标志告诉 Webpack 为生产环境优化构建。您可以将它与新的“生产”mode一起用于较小的构建。

不可能修改默认配置。

但是,您可以使用 optimization.minimizer设置来实例化自己的 UglifyJsPlugin。使用4.0,我们使用这个例子来获取源地图,即使当 mode被设置为 'production'时(在4.1.1时不再需要) :

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');


module.exports = {
optimization: {
minimizer: [
// we specify a custom UglifyJsPlugin here to get source maps in production
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: false,
ecma: 6,
mangle: true
},
sourceMap: true
})
]
}
};

快跑:

yarn add uglifyjs-webpack-plugin --dev

参考文献: 阿方索 · 佩雷斯答案

For those coming behind me, realized this misleading error was not related to my correct webpack config, but actually, the offline-plugin was out of date and causing this issue. It needed to be upgraded. See github issue: https://github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/234#issuecomment-369134047

你可以试试这个

npm install uglifyjs-webpack-plugin --save-dev

Webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');


module.exports = {
optimization: {
minimizer: [new UglifyJsPlugin()],
},
};

webpack documentation

不需要添加 uglifyjs-webpack-plugin,只需要将它添加到 webpack.prod.config.js文件的末尾:

 optimization: {
minimize: false
}