NekR / offline-plugin

Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
MIT License
4.52k stars 295 forks source link

Minify option auto-detection not working on webpack 4 #379

Closed edmorley closed 6 years ago

edmorley commented 6 years ago

Hi :-)

The options docs say:

  • minify: boolean. If set to true or false, the ServiceWorker's output will be minified or not accordingly. If set to something else, the ServiceWorker output will be minified if you are using webpack.optimize.UglifyJsPlugin in your configuration.
    Default: null

The check for that uses plugins to determine whether the user has activated minification: https://github.com/NekR/offline-plugin/blob/v5.0.2/src/service-worker.js#L143-L146

However in webpack 4, UglifyJsPlugin is typically not added to plugins, but instead used via the new optimization.minimizers option (the plugin is added there by default, but people can override) which is toggled via the optimization.minimize bool: https://github.com/webpack/webpack/blob/v4.6.0/lib/WebpackOptionsDefaulter.js#L270-L289 https://github.com/webpack/webpack/blob/v4.6.0/lib/WebpackOptionsApply.js#L334-L338

To allow the minify option auto-detection to work under both webpack 2-3 and webpack 4, I guess the conditional needs to be a combination of the existing plugins check and also optimization.minimize?

NekR commented 6 years ago

Hi @edmorley!

I think the definition of ServiceWorker.minify is quite right. It exactly does what it says.

However in webpack 4, UglifyJsPlugin is typically not added to plugins, but instead used via the new optimization.minimizers option (the plugin is added there by default, but people can override) which is toggled via the optimization.minimize bool:

The optimization.minimize supposed to add Uglify plugin to plugins list, unless you're using some custom minifier.

To allow the minify option auto-detection to work under both webpack 2-3 and webpack 4, I guess the conditional needs to be a combination of the existing plugins check and also optimization.minimize?

That would be too complicated, I think. I would prefer it to be as is, but if it doesn't work and isn't able to catch the plugin added by optimization.minimize, then it probably has to be changed.

edmorley commented 6 years ago

The optimization.minimize supposed to add Uglify plugin to plugins list, unless you're using some custom minifier.

I could be reading the webpack source incorrectly - but if you see the second source link above, it doesn't actually modify the plugins list, but applies the plugin separately.

As such, I'm pretty sure this issue is real.

edmorley commented 6 years ago

Many thanks :-)

NekR commented 6 years ago

Should be fixed now: https://github.com/NekR/offline-plugin/releases/tag/v5.0.3