Webpack 4 - How to configure minimize?

前端 未结 6 1914
挽巷
挽巷 2020-12-07 11:34

Webpack 4 comes with the following statement:

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

<
相关标签:
6条回答
  • 2020-12-07 11:56

    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.

    0 讨论(0)
  • 2020-12-07 11:57

    You can try this

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

    webpack.config.js

    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    
    module.exports = {
      optimization: {
        minimizer: [new UglifyJsPlugin()],
      },
    };
    

    webpack documentation

    0 讨论(0)
  • 2020-12-07 12:02

    Without adding uglifyjs-webpack-plugin, you can just add this to the end of your webpack.prod.config.js file:

     optimization: {
       minimize: false
     }
    
    0 讨论(0)
  • 2020-12-07 12:06

    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

    0 讨论(0)
  • 2020-12-07 12:13

    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
          })
        ]
      }
    };
    
    0 讨论(0)
  • 2020-12-07 12:18

    Just run:

    yarn add uglifyjs-webpack-plugin --dev
    

    Reference: Alfonso Pérez answer

    0 讨论(0)
提交回复
热议问题