html-webpack-plugin icon indicating copy to clipboard operation
html-webpack-plugin copied to clipboard

Setting "minify" to "true" doesn't work

Open hstaniszewski opened this issue 8 years ago • 9 comments

Expected behaviour

After setting minify option to true generated html file should be minified.

Current behaviour

html-webpack-plugin does nothing. But If I assign a configuration object to the minify, then the minification works.

Environment

Node.js v9.2.0 darwin 18.0.0 npm version 6.4.1 [email protected] [email protected]

hstaniszewski avatar Oct 30 '18 12:10 hstaniszewski

Hi!

In html-webpack-plugin 3.x, passing minify: true (the default) sets the html-minifier options to { }, ie that package's defaults (which is most minification features disabled): https://github.com/kangax/html-minifier#options-quick-reference

However in html-webpack-plugin 4 (of which there is a beta, 4.0.0-beta.2), minify: true (the default when webpack's mode is production) instead sets the html-minifier options to a more useful value: https://github.com/jantimon/html-webpack-plugin/blob/master/README.md#minification https://github.com/jantimon/html-webpack-plugin/blob/65879ebe5061d821c9cbc9639915d855a612851b/index.js#L109-L124

edmorley avatar Oct 30 '18 13:10 edmorley

So basically, if we want to begin using v4 behavior in v3, in our webpack config we'd replace

new HtmlWebpackPlugin({
  template: 'src/index.html',
  minify: true,
}),

with

new HtmlWebpackPlugin({
  template: 'src/index.html',
  minify: {
    collapseWhitespace: true,
    removeComments: true,
    removeRedundantAttributes: true,
    removeScriptTypeAttributes: true,
    removeStyleLinkTypeAttributes: true,
    useShortDoctype: true
  },
}),

Is that correct? I just tested this myself and it seems to work.

filoxo avatar Oct 31 '18 05:10 filoxo

@edmorley thank you for explaining, now I understand, however, I think the section "Minification" in the readme is misleading because there is the following information

If the minify option is set to true (the default when webpack's mode is 'production'), the generated HTML will be minified

Since the behavior of 4 differs so much from 3, there should be relevant information there.

@filoxo Yes, this is the correct configuration, I use the sam.

hstaniszewski avatar Oct 31 '18 08:10 hstaniszewski

@hstaniszewski, yeah agree it's confusing, though once v4 is released the README will then reflect the stable version's functionality.

@filoxo, that's correct - though you could also make it conditional on mode and not enable in development, if desired (to fully match the v4 default).

edmorley avatar Oct 31 '18 08:10 edmorley

Could we add a warning at the top of the README file that this is the master branch and that it's for v4 instead 3.x which I get installed by default? Was confused about this as well.

lazarljubenovic avatar Nov 20 '18 20:11 lazarljubenovic

+1, expected minifying work out of the box with 3.2.0 in production mode, as written in a readme. It's really misleading, that the readme assumes you use a beta version without any info about this in the top.

dimadk24 avatar Dec 17 '18 11:12 dimadk24

The npm version is always matching with the npm documentation: https://www.npmjs.com/package/html-webpack-plugin

I planed to publish the new major earlier but came across some problems and therefore postponed the official release.

jantimon avatar Dec 17 '18 12:12 jantimon

Any news on this?

berkant avatar May 22 '19 10:05 berkant

This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days.

stale[bot] avatar Nov 18 '19 11:11 stale[bot]