Skip to content

Possibility to extend default 'minimize' options #413

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

Closed
fretfan opened this issue Dec 25, 2021 · 7 comments · Fixed by #414
Closed

Possibility to extend default 'minimize' options #413

fretfan opened this issue Dec 25, 2021 · 7 comments · Fixed by #414

Comments

@fretfan
Copy link
Contributor

fretfan commented Dec 25, 2021

Feature Proposal

Make it possible to extend default 'minimize' options when defined as object, without repeating all other default options

Feature Use Case

Currently to change removeComments to false I need this snippet of code:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          minimize: {
            caseSensitive: true,
            collapseWhitespace: true,
            conservativeCollapse: true,
            keepClosingSlash: true,
            minifyCSS: true,
            minifyJS: true,
            removeComments: false, // overridden
            removeRedundantAttributes: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true,
          },
        },
      },
    ],
  },
};

With this feature it could look something like this:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          overrideMinimizeDefaults: true, // tells to use minimize default options + specified in minimize block
          minimize: {
            removeComments: false, // overriden
          },
        },
      },
    ],
  },
};

Or can try implementing something similar to '...' syntax here: https://webpack.js.org/plugins/mini-css-extract-plugin/#minimizing-for-production

optimization: {
    minimizer: [
      // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
      // `...`,
      new CssMinimizerPlugin(),
    ],
  },

Please paste the results of npx webpack-cli info here, and mention other relevant information

System:
    OS: Windows 10 10.0.19043
    CPU: (4) x64 Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
    Memory: 5.91 GB / 11.92 GB
  Binaries:
    Node: 14.16.1 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.12 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.62)
    Internet Explorer: 11.0.19041.1202
@alexander-akait
Copy link
Member

You already can do it, you don't need to set all options, because "html-minifier-terser" do it

@fretfan
Copy link
Contributor Author

fretfan commented Dec 26, 2021

https://github.com/webpack-contrib/html-loader#boolean-1
I mean, if you use minimize: true - defaults above are applied, but if minimize: {collapseWhitespace: false} used, only collapseWhitespace: false applied, all other defaults are not applied.

@alexander-akait
Copy link
Member

hm, I think we should open an issue in html-minifier-terser, because it should be default behavior

@fretfan
Copy link
Contributor Author

fretfan commented Dec 29, 2021

html-minifier-terser has different defaults (mostly everything disabled) https://github.com/terser/html-minifier-terser/tree/v6.0.2#options-quick-reference

I found where defaults are set in html-loader code: https://github.com/webpack-contrib/html-loader/blob/v3.0.1/src/utils.js#L499

@alexander-akait
Copy link
Member

Oh, yes, by default html-minifier-terser is not safe to use, I think we can export it and you can extend them using { ...htmlLoaderMinimizerOptions, yourOptions: true }, feel free to send a PR

@eamodio
Copy link

eamodio commented Jan 8, 2022

@alexander-akait wouldn't it be a better experience if this were just the default behavior? Instead of users having to know to import and mix in the defaults themselves?

For example, changing the following like to something like the code below?
https://github.dev/webpack-contrib/html-loader/blob/eb4297da5702867dc835329ff1c1d871be6e70bc/src/index.js#L41-L42

  if (options.minimize) {
    plugins.push(minimizerPlugin({ minimize: typeof options.minimize !== 'boolean' ? { ...defaultMinimizerOptions, ...options.minimize }: options.minimize, errors }));
  }

I have to admit, I thought that was already the behavior.

@alexander-akait
Copy link
Member

Yep, it will be better, but it will be breaking change, we can add TODO for the next major release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants