Html-webpack-plugin: Setting "minify" to "true" doesn't work

Created on 30 Oct 2018  路  9Comments  路  Source: jantimon/html-webpack-plugin

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]

wontfix

Most helpful comment

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.

All 9 comments

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

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.

@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, 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).

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.

+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.

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.

Any news on this?

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.

Was this page helpful?
0 / 5 - 0 ratings