Super-configurable, well-tested, JavaScript-based HTML minifier (enhanced successor of HTML Minifier)
npm install html-minifier-next  
Your HTML optimization precision tool: HTML Minifier Next (HMN) is a super-configurable, well-tested, JavaScript-based HTML minifier able to also handle in-document CSS, JavaScript, and SVG minification.
The project was based on HTML Minifier Terser (HMT), which in turn had been based on Juriy “kangax” Zaytsev’s HTML Minifier (HM); as of 2025, both HTML Minifier Terser and HTML Minifier had been unmaintained for several years. HMN offers additional features and has been optimized for speed. While an independent project, it is still backwards-compatible with HMT and HM.
From npm for use as a command-line app:
``shell`
npm i -g html-minifier-next
Directly with npx (no installation required):
`shell`
npx html-minifier-next --help
From npm for programmatic use:
`shell`
npm i html-minifier-next
Use html-minifier-next --help to check all available options:
| Option | Description | Example |
| --- | --- | --- |
| --input-dir
, -I | Specify an input directory | --input-dir=src |
| --ignore-dir , -X | Exclude directories—relative to input directory—from processing (comma-separated, overrides config file setting) | --ignore-dir=libs, --ignore-dir=libs,vendor,node_modules |
| --output-dir , -O | Specify an output directory | --output-dir=dist |
| --output , -o | Specify output file (reads from file arguments or STDIN) | File to file: html-minifier-next input.html -o output.html
Pipe to file: cat input.html \| html-minifier-next -o output.html
File to STDOUT: html-minifier-next input.html |
| --file-ext , -f | Specify file extension(s) to process (comma-separated, overrides config file setting); defaults to html,htm,xhtml,shtml; use for all files | --file-ext=html,php, --file-ext='' |
| --preset , -p | Use a preset configuration (conservative or comprehensive) | --preset=conservative |
| --config-file , -c | Use a configuration file | --config-file=html-minifier.json |
| --verbose, -v | Show detailed processing information (active options, file statistics) | html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace |
| --dry, -d | Dry run: Process and report statistics without writing output | html-minifier-next input.html --dry --collapse-whitespace |$3
You can use a configuration file to specify options. The file can be either JSON format or a JavaScript module that exports the configuration object:
JSON configuration example:
`json
{
"collapseWhitespace": true,
"removeComments": true,
"fileExt": "html,php",
"ignoreDir": "libs,vendor"
}
`JavaScript module configuration example:
`javascript
module.exports = {
collapseWhitespace: true,
removeComments: true,
fileExt: "html,php",
ignoreDir: ["libs", "vendor"]
};
`$3
ESM with Node.js ≥16.14:
`javascript
import { minify } from 'html-minifier-next';const result = await minify('
foo
', {
removeAttributeQuotes: true,
removeOptionalTags: true
});
console.log(result); // “foo”
`CommonJS:
`javascript
const { minify } = require('html-minifier-next');(async () => {
const result = await minify('
foo
', { preset: 'comprehensive' });
console.log(result); // “foo”
})();
`See the original blog post for details of how it works, descriptions of most options, testing results, and conclusions.
Presets
HTML Minifier Next provides presets for common use cases. Presets are pre-configured option sets that can be used as a starting point:
*
conservative: Basic minification with whitespace collapsing, comment removal, and removal of select attributes.
* comprehensive: More advanced minification for better file size reduction, including relevant conservative options plus attribute quote removal, optional tag removal, and more.To review the specific options set, presets.js lists them in an accessible manner.
Using presets:
`shell
Via CLI flag
html-minifier-next --preset conservative input.htmlVia config file
html-minifier-next --config-file=html-minifier.json input.html
where html-minifier.json contains: { "preset": "conservative" }
Override preset options
html-minifier-next --preset conservative --remove-empty-attributes input.html
`Priority order: Presets are applied first, then config file options, then CLI flags. This allows you to start with a preset and customize as needed.
Options quick reference
Most of the options are disabled by default. Experiment and find what works best for you and your project.
Options can be used in config files (camelCase) or via CLI flags (kebab-case with
-- prefix). Options that default to true use --no- prefix in CLI to disable them.| Option (config/CLI) | Description | Default |
| --- | --- | --- |
|
cacheCSS
--cache-css | Set CSS minification cache size; higher values improve performance for batch processing | 500 |
| cacheJS
--cache-js | Set JavaScript minification cache size; higher values improve performance for batch processing | 500 |
| caseSensitive
--case-sensitive | Treat attributes in case-sensitive manner (useful for custom HTML elements) | false |
| collapseAttributeWhitespace
--collapse-attribute-whitespace | Trim and collapse whitespace characters within attribute values | false |
| collapseBooleanAttributes
--collapse-boolean-attributes | Omit attribute values from boolean attributes | false |
| collapseInlineTagWhitespace
--collapse-inline-tag-whitespace | Collapse whitespace more aggressively between inline elements—use with collapseWhitespace: true | false |
| collapseWhitespace
--collapse-whitespace | Collapse whitespace that contributes to text nodes in a document tree | false |
| conservativeCollapse
--conservative-collapse | Always collapse to one space (never remove it entirely)—use with collapseWhitespace: true | false |
| continueOnMinifyError
--no-continue-on-minify-error | Continue on minification errors; when false, minification errors throw and abort processing | true |
| continueOnParseError
--continue-on-parse-error | Handle parse errors instead of aborting | false |
| customAttrAssign
--custom-attr-assign | Array of regexes that allow to support custom attribute assign expressions (e.g., ) | [] |
| customAttrCollapse
--custom-attr-collapse | Regex that specifies custom attribute to strip newlines from (e.g., /ng-class/) | undefined |
| customAttrSurround
--custom-attr-surround | Array of regexes that allow to support custom attribute surround expressions (e.g., ) | [] |
| customEventAttributes
--custom-event-attributes | Array of regexes that allow to support custom event attributes for minifyJS (e.g., ng-click) | [ /^on[a-z]{3,}$/ ] |
| customFragmentQuantifierLimit
--custom-fragment-quantifier-limit | Set maximum quantifier limit for custom fragments to prevent ReDoS attacks | 200 |
| decodeEntities
--decode-entities | Use direct Unicode characters whenever possible | false |
| ignoreCustomComments
--ignore-custom-comments | Array of regexes that allow to ignore matching comments | [ /^!/, /^\s*#/ ] |
| ignoreCustomFragments
--ignore-custom-fragments | Array of regexes that allow to ignore certain fragments, when matched (e.g., , {{ … }}, etc.) | [ /<%[\s\S]?%>/, /<\?[\s\S]?\?>/ ] |
| includeAutoGeneratedTags
--include-auto-generated-tags | Insert elements generated by HTML parser | false |
| inlineCustomElements
--inline-custom-elements | Array of names of custom elements which are inline, for whitespace handling | [] |
| keepClosingSlash
--keep-closing-slash | Keep the trailing slash on void elements | false |
| maxInputLength
--max-input-length | Maximum input length to prevent ReDoS attacks (disabled by default) | undefined |
| maxLineLength
--max-line-length | Specify a maximum line length; compressed output will be split by newlines at valid HTML split-points | undefined |
| mergeScripts
--merge-scripts | Merge consecutive inline script elements into one (only merges compatible scripts with same type, matching async/defer/nomodule/nonce) | false |
| minifyCSS
--minify-css | Minify CSS in style elements and attributes (uses Lightning CSS) | false (could be true, Object, Function(text, type)) |
| minifyJS
--minify-js | Minify JavaScript in script elements and event attributes (uses Terser or SWC) | false (could be true, Object, Function(text, inline)) |
| minifySVG
--minify-svg | Minify SVG elements and attributes (numeric precision, default attributes, colors) | false (could be true, Object) |
| minifyURLs
--minify-urls | Minify URLs in various attributes | false (could be true, String, Object, Function(text)) |
| noNewlinesBeforeTagClose
--no-newlines-before-tag-close | Never add a newline before a tag that closes an element | false |
| partialMarkup
--partial-markup | Treat input as a partial HTML fragment, preserving stray end tags (closing tags without opening tags) and preventing auto-closing of unclosed tags at end of input | false |
| preserveLineBreaks
--preserve-line-breaks | Always collapse to one line break (never remove it entirely) when whitespace between tags includes a line break—use with collapseWhitespace: true | false |
| preventAttributesEscaping
--prevent-attributes-escaping | Prevents the escaping of the values of attributes | false |
| processConditionalComments
--process-conditional-comments | Process contents of conditional comments through minifier | false |
| processScripts
--process-scripts | Array of strings corresponding to types of script elements to process through minifier (e.g., text/ng-template, text/x-handlebars-template, etc.) | [] |
| quoteCharacter
--quote-character | Type of quote to use for attribute values (' or ") | Auto-detected (uses the quote requiring less escaping; defaults to " when equal) |
| removeAttributeQuotes
--remove-attribute-quotes | Remove quotes around attributes when possible | false |
| removeComments
--remove-comments | Strip HTML comments | false |
| removeEmptyAttributes
--remove-empty-attributes | Remove all attributes with whitespace-only values | false (could be true, Function(attrName, tag)) |
| removeEmptyElements
--remove-empty-elements | Remove all elements with empty contents | false |
| removeEmptyElementsExcept
--remove-empty-elements-except | Array of elements to preserve when removeEmptyElements is enabled; accepts simple tag names (e.g., ["td"]) or HTML-like markup with attributes (e.g., ["