HTMLMinifier-Terser
HTMLMinifier-Terser is a highly configurable, well-tested JavaScript-based HTML minifier designed to optimize web page load times by significantly reducing the size of HTML, CSS, and JavaScript within HTML documents. The current stable version is 7.2.0, with regular updates addressing bug fixes, dependency updates, and minor feature enhancements. Major versions, like v7.0.0, introduce significant breaking changes such as the migration to ESM and updated Node.js requirements. It differentiates itself through extensive configurability, allowing fine-grained control over the minification process, and its strong performance compared to other minifiers, often yielding smaller output sizes as demonstrated in its minification comparison benchmarks.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../html-minifier-terser/dist/htmlminifier.js from ... not supported.
cause Attempting to `require()` the package in a CommonJS module while it has been transitioned to primarily ESM in v7.fixConvert your consuming module to ESM by adding `"type": "module"` to your `package.json` or changing your file extension to `.mjs`. Alternatively, use dynamic `import()`: `const { minify } = await import('html-minifier-terser');` -
TypeError: Cannot read properties of undefined (reading 'split')
cause This can occur if the input HTML passed to `minify` is not a string or is `undefined`.fixEnsure the first argument passed to `minify` is always a valid string containing the HTML to be minified.
Warnings
- breaking Version 7.0.0 migrated the build tool to Rollup, replaced the `he` dependency with `entities`, and rewrote the package to primarily support ESM. This impacts how the package is imported and consumed.
- breaking The minimum required Node.js version was increased to `v14.13.1` or `>=16.0.0` with the release of v7.0.0.
- gotcha Almost all minification options are disabled by default. Users must explicitly enable options like `collapseWhitespace`, `removeComments`, `minifyJS`, and `minifyCSS` to achieve desired minification levels.
- gotcha Using the CLI via `html-minifier-terser` requires installing the package globally or using `npx`. For programmatic use, install it as a local dependency.
Install
-
npm install html-minifier-terser -
yarn add html-minifier-terser -
pnpm add html-minifier-terser
Imports
- minify
const { minify } = require('html-minifier-terser');import { minify } from 'html-minifier-terser'; - minify with async options
import { minify } from 'html-minifier-terser'; // ... later await minify(html, { minifyCSS: { level: 2 } })
Quickstart
import { minify } from 'html-minifier-terser';
async function runMinification() {
const htmlInput = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test Page</title>
<style>body { color: red; } /* important */</style>
</head>
<body>
<!-- This is a comment -->
<p title=" some title " id="moo">Hello, world! </p>
<script> function greet() { console.log('Hi!'); } </script>
</body>
</html>
`;
const minifiedHtml = await minify(htmlInput, {
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true,
removeAttributeQuotes: true,
processConditionalComments: true,
});
console.log(minifiedHtml);
// Expected output (approx): "<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><title>Test Page</title><style>body{color:red}</style></head><body><p title=some title id=moo>Hello, world!</p><script>function greet(){console.log('Hi!')}</script></body></html>"
}
runMinification();