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

Enable minification by default when 'mode' is production #1048

Merged
merged 2 commits into from
Sep 17, 2018
Merged

Enable minification by default when 'mode' is production #1048

merged 2 commits into from
Sep 17, 2018

Conversation

edmorley
Copy link
Contributor

Previously minification was disabled by default. Now, if minify is undefined and mode is 'production', then it is enabled using the following options:

{
  collapseWhitespace: true,
  removeComments: true,
  removeRedundantAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  useShortDoctype: true
}

These options were based on the settings used by create-react-app, Neutrino and vue-cli, and are hopefully fairly conservative. See:
#1036 (comment)
https://github.com/kangax/html-minifier#options-quick-reference

These same defaults can enabled regardless of mode, by setting minify to true (which previously passed an empty object to html-minifier, meaning most minification features were disabled). Similarly, minification can be disabled even in production, by setting minify to false.

This change has no effect on users who pass an object to minify.

Fixes #1036.

Previously minification was disabled by default. Now, if `minify`
is `undefined` and `mode` is `'production'`, then it is enabled using
the following options:

```
{
  collapseWhitespace: true,
  removeComments: true,
  removeRedundantAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  useShortDoctype: true
}
```

These options were based on the settings used by create-react-app,
Neutrino and vue-cli, and are hopefully fairly conservative. See:
#1036 (comment)
https://github.com/kangax/html-minifier#options-quick-reference

These same defaults can enabled regardless of `mode`, by setting
`minify` to `true` (which previously passed an empty object to
html-minifier, meaning most minification features were disabled).
Similarly, minification can be disabled even in production, by setting
`minify` to `false`.

This change has no effect on users who pass an object to `minify`.

Fixes #1036.
Copy link
Owner

@jantimon jantimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks awesome!!

Got one small improvement to get it closer to the original webpack production mode behaviour

index.js Outdated
@@ -99,6 +99,19 @@ class HtmlWebpackPlugin {
this.options.filename = path.relative(compiler.options.output.path, filename);
}

const minify = this.options.minify;
if (minify === true || (minify === undefined && compiler.options.mode === 'production')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add the following code to make it easier to understand?

// 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;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea :-)

typings.d.ts Outdated
@@ -53,7 +53,7 @@ interface HtmlWebpackPluginOptions {
* HTML Minification options
* @https://github.com/kangax/html-minifier#options-quick-reference
*/
minify: boolean | {},
minify: undefined | boolean | {},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All properties are optional so we don't need to add the undefined here :)

Copy link
Contributor Author

@edmorley edmorley Sep 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this I got a type error:

index.js:39:11 - error TS2322: Type '{ template: string; templateContent: false; templateParameters: (compilation: any, assets: { publ...' is not assignable to type 'HtmlWebpackPluginOptions'.
  Types of property 'minify' are incompatible.
    Type 'undefined' is not assignable to type 'boolean | {}'.

39     const defaultOptions = {
             ~~~~~~~~~~~~~~

I can try changing it to optional instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry you are absolutely right - usually all options are non optional after the constructor.
But as we don't know the compiler state at this time your solution is absolutely correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer optional or a union with undefined?

@edmorley
Copy link
Contributor Author

One of the Travis jobs failed due to an infra error - could you restart it? (Travis only permits those with commit access to restart jobs unfortunately)

@jantimon jantimon merged commit 3c97405 into jantimon:webpack-4 Sep 17, 2018
@jantimon
Copy link
Owner

Thank you so much 👍

@edmorley edmorley deleted the default-minify-production branch September 17, 2018 09:00
@edmorley
Copy link
Contributor Author

Thank you for the review/merge :-)

edmorley added a commit to neutrinojs/neutrino that referenced this pull request Sep 20, 2018
The next release of `html-webpack-plugin` will enable minification
by default when `mode` is production, using `minify` options that
are based on those used by popular projects:
jantimon/html-webpack-plugin#1048

Those changes haven't yet been released, so in the meantime we can
emulate them via preset options, so that we can test out the new
defaults sooner (ie: in the upcoming alpha/beta), rather than making
the breaking change just before Neutrino 9 final.

Once a new version of `html-webpack-plugin` is released, we can stop
overriding `minify` entirely.
edmorley added a commit to neutrinojs/neutrino that referenced this pull request Sep 28, 2018
The new version includes jantimon/html-webpack-plugin#1048, which
means we can remove the temporary additions made in #1121.
@lock
Copy link

lock bot commented Oct 17, 2018

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants