Webpack 4 - How to configure minimize?

2019-03-07 23:01发布

问题:

Webpack 4 comes with the following statement:

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?

回答1:

It's not possible to modify the default configuration.

You can use the optimization.minimizer setting to instantiate your own UglifyJsPlugin, however. Using 4.0 we used this example to get source maps even when mode is set to 'production' for example (no longer necessary as of 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
      })
    ]
  }
};


回答2:

Just run:

yarn add uglifyjs-webpack-plugin --dev

Reference: Alfonso Pérez answer



回答3:

You should check p option: https://webpack.js.org/guides/production/#cli-alternatives : this flag tells Webpack to optimize your build for production environment. You can use it with the new "production" mode for a smaller build.