Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hstaniszewski opened this issue Oct 30, 2018 · 9 comments
Closed

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

hstaniszewski opened this issue Oct 30, 2018 · 9 comments
Labels

Comments

@hstaniszewski
Copy link

@hstaniszewski hstaniszewski commented Oct 30, 2018

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
webpack@4.23.1
html-webpack-plugin@3.2.0

@edmorley

This comment has been minimized.

Copy link
Contributor

@edmorley edmorley commented Oct 30, 2018

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

// Check if webpack is running in production mode
// @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14
const isProductionLikeMode = compiler.options.mode === 'production' || !compiler.options.mode;
const minify = this.options.minify;
if (minify === true || (minify === undefined && isProductionLikeMode)) {
this.options.minify = {
// https://github.com/kangax/html-minifier#options-quick-reference
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
};
}

@filoxo

This comment has been minimized.

Copy link

@filoxo filoxo commented Oct 31, 2018

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.

@hstaniszewski

This comment has been minimized.

Copy link
Author

@hstaniszewski hstaniszewski commented Oct 31, 2018

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

@edmorley

This comment has been minimized.

Copy link
Contributor

@edmorley edmorley commented Oct 31, 2018

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

Repository owner deleted a comment from 9-lives Nov 14, 2018
@lazarljubenovic

This comment has been minimized.

Copy link
Contributor

@lazarljubenovic lazarljubenovic commented Nov 20, 2018

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.

@DimaDK24

This comment has been minimized.

Copy link

@DimaDK24 DimaDK24 commented Dec 17, 2018

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

@jantimon

This comment has been minimized.

Copy link
Owner

@jantimon jantimon commented Dec 17, 2018

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.

@0xbkt

This comment has been minimized.

Copy link

@0xbkt 0xbkt commented May 22, 2019

Any news on this?

@stale

This comment has been minimized.

Copy link

@stale stale bot commented Nov 18, 2019

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 stale bot added the wontfix label Nov 18, 2019
@stale stale bot closed this Dec 3, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
7 participants
You can’t perform that action at this time.