webpack-contrib / uglifyjs-webpack-plugin

[deprecated] UglifyJS Plugin
MIT License
1.38k stars 179 forks source link

Apply Uglify multiple times to the same code #364

Closed joecorkerton closed 6 years ago

joecorkerton commented 6 years ago

Is it possible to apply uglify twice to the same code? I need to do the webpack equivalent of

uglifyjs elm.js --compress 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters=true,keep_fargs=false,unsafe_comps=true,unsafe=true,passes=2' --output=elm.js && uglifyjs elm.js --mangle --output=elm.js

Why?

It's necessary to run Uglify twice if you use the pure_funcs flag, because if you enable both --compress and --mangle at the same time, the pure_funcs argument will have no effect; Uglify will mangle the names first and then not recognize them when it encounters those functions later.

alexander-akait commented 6 years ago

@joecorkerton create two instance of plugin and put second instance after first

joecorkerton commented 6 years ago

@evilebottnawi and if I include: with the same string it will figure out to apply the second uglify to the already transformed code? Intuitively it seems like that it would not figure that out

Otherwise what should I do instead of the second include?

alexander-akait commented 6 years ago

@joecorkerton just use:

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

module.exports = {
  //...
  optimization: {
    minimizer: [new UglifyJsPlugin(), new UglifyJsPlugin()]
  }
};

You can define own options, also you can exclude/include chunks what should be do not uglify twice

joecorkerton commented 6 years ago

@evilebottnawi Thanks for your help so far.

I have been playing around with different config options and it seems like I'm not able to disable mangle now. I set mangle to false and the output is still minified, with the same filesize. This seems to be the same issue as #234 . Is there a solution for that? Reading through the issue didn't help much

alexander-akait commented 6 years ago

@joecorkerton please create minimum reproducible test repo and describe what you have and what you expected, i can't understand you

kzc commented 6 years ago

uglifyjs elm.js --compress 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters=true,keep_fargs=false,unsafe_comps=true,unsafe=true,passes=2' --output=elm.js && uglifyjs elm.js --mangle --output=elm.js

Elm is doing that to work around a bug in uglify-js.

See https://github.com/parcel-bundler/parcel/issues/2062#issuecomment-424763767 for the correct way to do it in both Terser and Uglify.

minimizer: [new UglifyJsPlugin(), new UglifyJsPlugin()]

That's inefficient - that would parse and output the code twice.

Instead just set the compress option passes to 2 or greater.