webpack: UglifyJsPlugin options.exclude does not work?

When using the UglifyJsPlugin within my webpack build I get a large list of errors from my bower_components dependencies. It looks to me (from here: https://github.com/webpack/webpack/blob/master/lib/optimize/UglifyJsPlugin.js#L43) that I should be able to use something like this

new webpack.optimize.UglifyJsPlugin({
  exclude: [
    /bower_components\//
  ]
})

which continues to throw out a massive list of errors, leading me to believe they are still being parsed.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 6
  • Comments: 17 (6 by maintainers)

Most upvoted comments

It’s working as expected, which is processing your generated chunks rather than source files. If you want to ignore only a few modules, extract them into a separate chunk and exclude that one from UglifyJsPlugin.

@Sawtaytoes

new webpack.optimize.UglifyJsPlugin({
	exclude: 'chunkToNotUglify',
}),

My solution was to use the uglify-loader and use exclude: /\.test\.js/i. This is not ideal for actually uglifying all the code because it works at the module level, but I’m just interested in my code working uglified under test. My actual distributable still uses UglifyJsPlugin so this workaround worked well for me.

Thanks! I was able to run this against my chunk name, and it did work. All I had to do was to put the UglifyJS plugin declaration in there twice. Once for the main bundle files excluding this particular one and another declaration for this particular one with mangle: false. I appreciate the help 👍.

I’ve realized that in my case the reason that this is happening is that by the time the code gets to my UglifyJsPlugin it has already been concatenated into a single file (by the name of formly.min.js). I need to figure out a way to uglify before it concats…

I actually mentioned this issue in the pull-request that introduced this exclude feature for the UglifyJsPlugin. Currently you can only exclude generated webpack chunks from being parsed, which is kind of pointless IMO since the general use-case is to avoid parsing required libraries, not your own generated code.

Hopefully the UglifyJsPlugin exclude feature can be updated to work as would be expected. Although at this point it might need to be added as an additional configuration API since some projects may now be dependent on exclude working as it currently does.