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
      })
    ]
  }
};
Answer from Beau on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 58754030 › webpack-optimize-uglifyjsplugin-has-been-removed
npm - webpack.optimize.UglifyJsPlugin has been removed - Stack Overflow
November 8, 2019 - // The development configuration is different and lives in a separate file. module.exports = { mode: 'production', 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 }) ] }, // Don't attempt to continue if there are any errors.
Top answer
1 of 14
169

webpack.config.js:

const webpack = require("webpack");

module.exports = {
  entry: {
    "bundle": "./entry.js",
    "bundle.min": "./entry.js",
  },
  devtool: "source-map",
  output: {
    path: "./dist",
    filename: "[name].js"
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin({
      include: /\.min\.js$/,
      minimize: true
    })
  ]
};

Since Webpack 4, webpack.optimize.UglifyJsPlugin has been deprecated and its use results in error:

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

As the manual explains, the plugin can be replaced with minimize option. Custom configuration can be provided to the plugin by specifying UglifyJsPlugin instance:

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

module.exports = {
  // ...
  optimization: {
    minimize: true,
    minimizer: [new UglifyJsPlugin({
      include: /\.min\.js$/
    })]
  }
};

This does the job for a simple setup. A more effective solution is to use Gulp together with Webpack and do the same thing in one pass.

2 of 14
166

You can use a single config file, and include the UglifyJS plugin conditionally using an environment variable:

const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');

const PROD = JSON.parse(process.env.PROD_ENV || '0');

module.exports = {

  entry: './entry.js',
  devtool: 'source-map',
  output: {
    path: './dist',
    filename: PROD ? 'bundle.min.js' : 'bundle.js'
  },
  optimization: {
    minimize: PROD,
    minimizer: [
      new TerserPlugin({ parallel: true })
  ]
};

and then just set this variable when you want to minify it:

$ PROD_ENV=1 webpack

Edit:

As mentioned in the comments, NODE_ENV is generally used (by convention) to state whether a particular environment is a production or a development environment. To check it, you can also set const PROD = (process.env.NODE_ENV === 'production'), and continue normally.

Find elsewhere
🌐
GitHub
github.com › gyulavoros › kotlin-todomvc › issues › 1
Error: webpack.optimize.UglifyJsPlugin has been removed · Issue #1 · gyulavoros/kotlin-todomvc
March 25, 2018 - > Task :frontend:webpack-run webpack-dev-server exited with exit code 1, see /Users/mcpringle/GitHub/other/kotlin-todomvc/frontend/build/logs/webpack-dev-server.log /Users/mcpringle/GitHub/other/kotlin-todomvc/frontend/build/node_modules/webpack/lib/webpack.js:162 throw new RemovedPluginError(errorMessage); ^ Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.
🌐
GitHub
gist.github.com › gricard › e8057f7de1029f9036a990af95c62ba8
Just some notes about my attempt to upgrade to webpack 4 · GitHub
$ npm run buildjs ... Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead. Oh, that too, eh?
🌐
webpack
v4.webpack.js.org › plugins › uglifyjs-webpack-plugin
UglifyjsWebpackPlugin | webpack
The uglifyOptions.output.comments option specifies whether the comment will be preserved, i.e. it is possible to preserve some comments (e.g. annotations) while extracting others or even preserving comments that have been extracted. Enable/disable extracting comments. ... module.exports = { optimization: { minimizer: [ new UglifyJsPlugin({ extractComments: true, }), ], }, };
🌐
GitHub
github.com › webpack › webpack › issues › 283
Why does the UglifyJsPlugin affect other loaders? · Issue #283 · webpack/webpack
Btw. This was changed in webpack. UglifyJs now only minimized js. A LoaderOptionsPlugin allows to switch on minimizing for loaders. ... I wholeheartedly agree with the issue, so it's great if this has been solved, but I haven't seen it for the stable (1.x.x) version.
🌐
GitHub
github.com › webpack › webpack › issues › 6409
TypeError: webpack.optimize.UglifyJsPlugin is not a constructor · Issue #6409 · webpack/webpack
If the current behavior is a bug, please provide the steps to reproduce. const webpack = require('webpack'); new webpack.optimize.UglifyJsPlugin()
🌐
Davidwanderer
davidwanderer.github.io › Blog › 2020 › 03 › 19 › Vue打包报错Error-webpack-optimize-UglifyJsPlugin-has-been-removed-please-use-config-optimization-minimize-instead
Vue打包报错Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead. | Stay hungry, Stay foolish.
April 15, 2021 - 使用命令npm run build进行生产构建时,报错Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.,详细的错误信息如下: · 查资料发现webpack4已经不支持在plugins里面操作UglifyJsPlugin,需要将UglifyJsPl...
🌐
webpack
webpack.js.org › configuration › optimization
Optimization | webpack
Tell webpack to minimize the bundle using the TerserPlugin or the plugin(s) specified in optimization.minimizer.
🌐
GitHub
github.com › NekR › offline-plugin › issues › 351
Issues with Webpack 4 · Issue #351 · NekR/offline-plugin
February 25, 2018 - webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead. If I remove offline-plugin, then this error isn't there anymore. 👍9 · Sign up for free to join this conversation on GitHub.
🌐
GitHub
github.com › webpack › webpack › issues › 4713
When using `--optimize-minimize`, webpack ignores uglifyPlugin's `mangle: false` option. · Issue #4713 · webpack/webpack
April 15, 2017 - When using --optimize-minimize, webpack ignores uglifyPlugin's mangle: false option. If the current behavior is a bug, please provide the steps to reproduce. let uglifyPlugin = new UglifyJSPlugin({ mangle: false, // not working output: { comments: ...