Skip to content

TypeError: webpack.optimize.UglifyJsPlugin is not a constructor #6409

Closed

Description

@fregante

This is in [email protected]

Do you want to request a feature or report a bug?

bug

What is the current behavior?

TypeError: webpack.optimize.UglifyJsPlugin is not a constructor

If the current behavior is a bug, please provide the steps to reproduce.

const webpack = require('webpack');
new webpack.optimize.UglifyJsPlugin()

Also:

$ npm i [email protected]
$ node -e 'console.log(require("webpack").optimize)'

{ AggressiveMergingPlugin: [Getter],
  AggressiveSplittingPlugin: [Getter],
  ChunkModuleIdRangePlugin: [Getter],
  LimitChunkCountPlugin: [Getter],
  MinChunkSizePlugin: [Getter],
  ModuleConcatenationPlugin: [Getter],
  OccurrenceOrderPlugin: [Getter],
  RuntimeChunkPlugin: [Getter],
  SideEffectsFlagPlugin: [Getter],
  SplitChunksPlugin: [Getter] }

What is the expected behavior?

webpack.optimize.UglifyJsPlugin === function

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

❯ node v8.6.0
❯ npm 5.6.0
❯ webpack 4.0.0-beta.0

Activity

  1. michael-ciniawsky commented on Jan 29, 2018

    @michael-ciniawsky
    Contributor

    CommonChunksPlugin (removed) && UglifyJsPlugin aren't exposed as plugins anymore since [email protected], use config.optimization.splitChunks && config.optimization.minimize instead please

  2. fregante commented on Jan 29, 2018

    @fregante
    ContributorAuthor

    Any docs available for config.optimization.minimize? Google brings up zero results.

    Also it'd be nice to include a friendly message about this in webpack itself because plenty of people will be hitting that error.

  3. swarajgiri commented on Jan 30, 2018

    @swarajgiri

    @bfred-it - https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693

    optimization: {
        minimize: true,
        runtimeChunk: true,
        splitChunks: {
            chunks: "async",
            minSize: 1000,
            minChunks: 2,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            name: true,
            cacheGroups: {
                default: {
                    minChunks: 1,
                    priority: -20,
                    reuseExistingChunk: true,
                },
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10
                }
            }
        }
    }
    
  4. fregante commented on Jan 30, 2018

    @fregante
    ContributorAuthor

    Thanks! Is it possible to configure the minification as well?

  5. ooflorent commented on Jan 30, 2018

    @ooflorent
    Contributor

    @bfred-it yes. Set optimization.minimize to false, add uglifyjs-webpack-plugin to your project deps. Use this one instead of webpack’s builtin.

  6. NekR commented on Jan 30, 2018

    @NekR

    Now it's even more confusing. What exactly minimize option is doing then?

  7. ooflorent commented on Jan 30, 2018

    @ooflorent
    Contributor

    When mode is production or not defined, minimize is enabled. This option automatically adds Uglify plugin. If you want to add your own setup, you should set minimize to false to avoid double minification.

  8. NekR commented on Jan 30, 2018

    @NekR
  9. fregante commented on Jan 30, 2018

    @fregante
    ContributorAuthor

    If you set minimize: false you won’t have double minification.

    What I don’t understand is why not make minimize accept a uglifyjs-webpack-plugin config object.

  10. ooflorent commented on Jan 30, 2018

    @ooflorent
    Contributor

    Because minimize tells webpack to use optimization.minimizer to create the minification plugin. Currently it creates uglify’s one but one could set it to closure or babili.

  11. ooflorent commented on Jan 30, 2018

    @ooflorent
    Contributor

    But sure, minimize could also accept an object and pass it to minimizer. What do you think about this @sokra?

  12. alexander-akait commented on Jan 30, 2018

    @alexander-akait
    Member

    @ooflorent should accept, otherwise, how to configure uglify plugin?

  13. benthemonkey commented on Jan 30, 2018

    @benthemonkey
    Contributor

    I could be mistaken, but if you look at https://github.com/webpack/webpack/blob/next/lib/WebpackOptionsApply.js#L306, you'll see that if optimization.minimize = true, then webpack just takes the array of plugins from optimization.minimizer (e.g. optimization.minimizer = [new UglifyJSPlugin(your options)]) and applies it to the compiler. So I think that's how you should configure the uglify plugin.

  14. backflip commented on Feb 7, 2018

    @backflip

    @benthemonkey, there seem to be issues with the config validation when doing this. Example:

    optimization: {
      minimizer: [
        new UglifyJSPlugin({
          mangle: {
            keep_fnames: true,
          },
        }),
      ],
    },

    This triggers the following error for me:

    options['mangle'] should NOT have additional properties
    

    UPDATE: Never mind, happens as well for

    plugins: [
      new UglifyJSPlugin({
        mangle: {
          keep_fnames: true,
        },
      }),
    ],
    
  15. alexander-akait commented on Feb 7, 2018

    @alexander-akait
    Member

    @backflip ?

    optimization: {
      minimizer: {
        mangle: {
          keep_fnames: true,
        },
      },
    },
  16. backflip commented on Feb 7, 2018

    @backflip

    I was using an outdated structure, the config needs to be wrapped with uglifyOptions:

    optimization: {
      minimizer: [
        new UglifyJSPlugin({
          uglifyOptions: {
            mangle: {
              keep_fnames: true,
            },
          },
        }),
      ],
    },

    Sorry for spamming.

  17. Vnthf commented on Feb 26, 2018

    @Vnthf

    @backflip But treeshaking not work anymore if I use minimizer option. I think unused option not work in webpack4. right?

  18. ooflorent commented on Apr 4, 2018

    @ooflorent
    Contributor

    webpack.optimize.UglifyJsPlugin is gone in webpack 4.

    Now you must load the plugin:

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

    And register it:

    optimization.minimizer = [new UglifyJSPlugin(…)]
  19. grig0ry commented on Apr 13, 2018

    @grig0ry

    @ooflorent If my configuration is in another package, @evilebottnawi variant is more convenient. There is no need to add uglifyjs-webpack-plugin in dependencies.

  20. D19cannon commented on Jul 23, 2018

    @D19cannon

    Hi guys, Sorry for coming back to this topic, but I have tried a billion ways now to get the UglyJsPlugin running. All I get i the same error over and over again. Any thoughts what I might be doing wrong here?

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

    module.exports = {
    mode: "production",
    optimization: {
    minimizer: [
    new UglifyJSPlugin({
    sourceMap: true,
    ecma: 5,
    warnings: false,
    ie8: false,
    safari10: false,
    dead_code: true,
    comments: false,
    uglifyOptions: {
    compress: {
    inline: false
    }
    }
    })
    ]
    },
    ...
    };

  21. alexander-akait commented on Jul 23, 2018

    @alexander-akait
    Member

    @D19cannon what error you are got?

  22. D19cannon commented on Jul 23, 2018

    @D19cannon

    @evilebottnawi ReferenceError: UglifyJSPlugin is not defined

    Error doesn't make any sense to me. As you can see I have the require() and with an extension in VScode I can see that 733.3k has been imported.

    Any ideas?

  23. alexander-akait commented on Jul 23, 2018

    @alexander-akait
    Member

    You need add const UglifyJsPlugin = require('uglifyjs-webpack-plugin') in your config file

  24. D19cannon commented on Jul 23, 2018

    @D19cannon

    @evilebottnawi Yes I understand, but as I mentioned in my first message. It is there, but I still receive this error.

  25. D19cannon commented on Jul 23, 2018

    @D19cannon

    screen shot 2018-07-23 at 14 06 17

  26. alexander-akait commented on Jul 23, 2018

    @alexander-akait
    Member

    @D19cannon seriously? You use const UglifyJsPlugin = require('uglifyjs-webpack-plugin') in top, but in minimizer option you use UglifyJSPlugin. Look on s in plugin name (first example with lowercase, second in UPPERCASE)

  27. D19cannon commented on Jul 23, 2018

    @D19cannon

    My bad. I copied that part from an example from somewhere. Also, when i searched for it VScode didn't distinct the uppercase from the lowercase, but instead recognises the words as its whole.

    Thank you :)

  28. danchristian commented on Aug 9, 2018

    @danchristian

    Tripped up on this too, thanks!

  29. phoeson commented on Jan 14, 2019

    @phoeson

    And first of all, you need to install the package
    npm install uglifyjs-webpack-plugin --save-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions