HTML will only be minimized in production mode by default. To enable minification in development, explicitly set optimization.minimize: true.
Finally, run webpack using the method you normally use (e.g., via CLI or an npm script).
[!NOTE]
Removing and collapsing spaces in the tools differ (by default).
@swc/html - Remove and collapse whitespaces only in safe places (for example - around html and body elements, inside the head element and between metadata elements - <meta>/script/link/etc.)
html-minifier-terser - Always collapse multiple whitespaces to 1 space (never remove it entirely), but you can change it using options
Allows you to override default minify function.
By default, plugin uses html-minifier-terser package.
We currently support:
HtmlMinimizerPlugin.swcMinify (used to compress HTML documents, i.e. with HTML doctype and <html>/<body>/<head> tags)
HtmlMinimizerPlugin.swcMinifyFragment (used to compress HTML fragments, i.e. when you have part of HTML which will be inserted into another HTML parts)
HtmlMinimizerPlugin.htmlMinifierTerser
HtmlMinimizerPlugin.minifyHtmlNode
[!NOTE]
The difference between swcMinify and swcMinifyFragment is the error reporting.
You will get errors (invalid or broken syntax) if you have them in your HTML document or fragment. Which allows you to see all the errors and problems at the build stage.
Useful for using and testing unpublished versions or forks.
[!WARNING]
Always use require inside minify function when parallel option enabled.
function
You can define a custom minify function, giving full control over how the HTML is processed.
If an array of functions is passed to the minify option, the minimizerOptions can be either as:
An array; If minimizerOptions is array, the function index in the minify array corresponds to the options object with the same index in the minimizerOptions array.
A single object; If you use minimizerOptions like object, all minify function accept it.
webpack.config.js
module.exports = { optimization: { minimize: true, minimizer: [ new HtmlMinimizerPlugin({ minimizerOptions: [ // Options for the first function (HtmlMinimizerPlugin.htmlMinifierTerser) { collapseWhitespace: true, }, // Options for the second function {}, ], minify: [ HtmlMinimizerPlugin.htmlMinifierTerser, (data, minimizerOptions) => { const [[filename, input]] = Object.entries(data); // To do something return { code: "optimised code", warnings: [],
minimizerOptions
Type:
type minimizerOptions = Record<string, any> | Record<string, any>[];
The function index in the minify array corresponds to the options object with the same index in the minimizerOptions array.
If you use minimizerOptions like object, all minify function accept it.
webpack.config.js
module.exports = { optimization: { minimize: true, minimizer: [ new HtmlMinimizerPlugin({ minimizerOptions: [ // Options for the first function (HtmlMinimizerPlugin.htmlMinifierTerser) { collapseWhitespace: true, }, // Options for the second function {}, ], minify: [ HtmlMinimizerPlugin.htmlMinifierTerser, (data, minimizerOptions) => { const [[filename, input]] = Object.entries(data); // To do something return { code: "optimised code", warnings: [],
You can use multiple HtmlMinimizerPlugin plugins to compress different files with the different minify function.
Contributing
We welcome all contributions!
If you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.
// HTML fragments - HTML fragments, i.e. HTML files without doctype or used in `<template></template>` tags or HTML parts which injects into another HTML parts