HTML Minimizer Webpack Plugin

raw JSON →
6.0.0 verified Sat Apr 25 auth: no javascript

Webpack plugin (v6.0.0) that minifies HTML files using one of three engines: swc (Rust), html-minifier-terser (JavaScript, default), or @minify-html/node (Rust). Integrates into Webpack's optimization.minimizer pipeline. Requires Node >= 20.9.0 (v6), Webpack 5.1+. Provides TypeScript types. Releases: v6.0.0 (Mar 2026), v5.0.x (2025), v5.0.0 (Jan 2024, dropped Node <18.12.0). Differentiators: multi-engine support, deep Webpack integration, asset/resource compatibility.

error Error: HtmlMinimizerPlugin is not a constructor
cause Using named import instead of default import in CommonJS.
fix
Change to const HtmlMinimizerPlugin = require('html-minimizer-webpack-plugin');
error TypeError: Cannot read properties of undefined (reading 'minify')
cause Plugin configured without proper minimizer function; default engine not found.
fix
Make sure you have installed html-minifier-terser (auto-installed) or specify minify option.
error Module not found: Error: Can't resolve '@swc/html'
cause Missing optional dependency @swc/html when using swc minify.
fix
Run npm install @swc/html --save-dev
breaking Node.js >= 20.9.0 required, dropped support for older versions.
fix Upgrade Node.js to >=20.9.0 or stay on v5.x (requires Node >=18.12.0).
breaking Node.js >= 18.12.0 required in v5.0.0.
fix Upgrade Node.js to >=18.12.0 or use v4.x (Node 14+).
gotcha HTML is only minimized in production mode by default. In development, set optimization.minimize: true explicitly.
fix Add optimization.minimize: true to webpack config to enable minification in non-production modes.
gotcha The plugin processes only assets emitted by other plugins like copy-webpack-plugin or html-webpack-plugin; it does not handle inline HTML in JavaScript.
fix Ensure your HTML files are emitted as assets (e.g., via copy-webpack-plugin or html-webpack-plugin) before they reach the minimizer.
gotcha When using @swc/html, minify option must be HtmlMinimizerPlugin.swcMinify or swcMinifyFragment; using a string 'swc' will not work.
fix Pass the static property, e.g., minify: HtmlMinimizerPlugin.swcMinify.
deprecated The option 'minimizerOptions' is incorrectly typed in some TypeScript versions; use 'options' instead (v5+).
fix Use 'options' property for minimizer options in v6; check your TypeScript strictness.
npm install html-minimizer-webpack-plugin
yarn add html-minimizer-webpack-plugin
pnpm add html-minimizer-webpack-plugin

Basic configuration using default html-minifier-terser engine with custom options. Requires optimization.minimize: true.

// webpack.config.js (CommonJS)
const HtmlMinimizerPlugin = require('html-minimizer-webpack-plugin');

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new HtmlMinimizerPlugin({
        minimizerOptions: {
          // html-minifier-terser options
          collapseWhitespace: true,
          removeComments: true,
        },
      }),
    ],
  },
};