package
html minimizer plugin for Webpack
npm install html-minimizer-webpack-pluginGitHub-flavored package documentation.
Across all versions, last 30 days.
53,990 in 30 days
Runtime metadata.
Tagged topics.
Estimated unpacked size across 37 packages.
13.2 MB
Includes dependencies.
5 direct dependencies.
^30.0.0
30.5.1
^4.2.0
4.8.3
^7.2.0
7.2.0
^7.0.3
7.1.4
7.0.2
This plugin can use 3 tools to optimize and minify your HTML:
swc - very fast Rust-based platform for the Web.html-minifier-terser (by default) - JavaScript-based HTML minifier.@minify-html/node - A Rust HTML minifier meticulously optimised for speed and effectiveness, with bindings for other languages.This plugin integrates seamlessly into your Webpack build pipeline to reduce HTML size and improve loading performance.
To begin, you'll need to install html-minimizer-webpack-plugin:
npm install html-minimizer-webpack-plugin --save-devor
yarn add -D html-minimizer-webpack-pluginor
pnpm add -D html-minimizer-webpack-pluginAdditional step: If you want to use @swc/html you need to install it:
npm install @swc/html --save-devor
yarn add -D @swc/htmlor
pnpm add -D @swc/htmlAdditional step: If you want to use @minify-html/node you need to install it:
npm install @minify-html/node --save-devor
yarn add -D @minify-html/nodeor
pnpm add -D @minify-html/nodeThen add the plugin to your webpack configuration. For example:
webpack.config.js
const CopyPlugin = require("copy-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
type: "asset/resource",
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, "dist"),
from: "./src/*.html",
},
],
[!NOTE]
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 - aroundhtmlandbodyelements, inside theheadelement 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 usingoptions@minify-html/node- Please read documentation https://github.com/wilsonzlin/minify-html#whitespace for detailed whitespace behavior.
testType:
type test = string | RegExp | (string | RegExp)[];Default: /\.html(\?.*)?$/i
Test to match files against.
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
test: /\.foo\.html/i,
}),
],
},
};includeType:
type include = string | RegExp | (string | RegExp)[];Default: undefined
Files to include for minification.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
include: /\/includes/,
}),
],
},
};excludeType:
type exclude = string | RegExp | (string | RegExp)[];Default: undefined
Files to exclude from minification.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
exclude: /\/excludes/,
}),
],
},
};parallelType:
type parallel = undefined | boolean | number;Default: true
Enables multi-process parallelization to improve build performance.
If true, uses os.cpus().length - 1 or os.availableParallelism() - 1 (if available).
If number, sets the number of concurrent workers.
[!NOTE]
Parallelization can speed up your build significantly and is therefore highly recommended.
booleanEnable or disable multi-process parallel running.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
parallel: true,
}),
],
},
};numberEnable multi-process parallel running and set number of concurrent runs.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
parallel: 4,
}),
],
},
};minifyType:
type minify =
| ((
data: Record<string, string>,
minimizerOptions: Record<string, any>,
) => {
code: string;
errors?: unknown[] | undefined;
warnings?: unknown[] | undefined;
})
| ((
data: Record<string, string>,
minimizerOptions: Record<string, any
Default: HtmlMinimizerPlugin.htmlMinifierTerser
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.htmlMinifierTerserHtmlMinimizerPlugin.minifyHtmlNode[!NOTE]
The difference between
swcMinifyandswcMinifyFragmentis 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
requireinsideminifyfunction whenparalleloption enabled.
functionYou can define a custom minify function, giving full control over how the HTML is processed.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
minimizerOptions: {
collapseWhitespace: true,
},
minify: (data, minimizerOptions) => {
const htmlMinifier = require("html-minifier-terser");
const [[filename, input]] = Object.entries(data);
return {
code: htmlMinifier.minify(input, minimizerOptions),
warnings: [],
errors: [],
};
},
}),
],
arrayIf 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: [],
minimizerOptionsType:
type minimizerOptions = Record<string, any> | Record<string, any>[];Default:
{
caseSensitive: true,
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
}Html-minifier-terser optimizations options.
objectApplies the same options to the default or custom minify function.
module.exports = {
optimization: {
minimize: true,
minimizer: [
new HtmlMinimizerPlugin({
minimizerOptions: {
collapseWhitespace: false,
},
}),
],
},
};arrayThe 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: [],
swc/htmlAvailable options.
HTML Documents:
const CopyPlugin = require("copy-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
type: "asset/resource",
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, "dist"),
from: "./src/*.html",
},
],
HTML Fragments:
Use this for partial HTML files (e.g. inside <template></template> tags or HTML strings).
const path = require("node:path");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
type: "asset/resource",
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, "dist"),
from:
@minify-html/nodeAvailable options.
HTML Documents:
const CopyPlugin = require("copy-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
type: "asset/resource",
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, "dist"),
from: "./src/*.html",
},
],
You can use multiple HtmlMinimizerPlugin plugins to compress different files with the different minify function.
We welcome all contributions! If you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.
sokra
tobias.koppers@googlemail.com