← 全部工具

html-minifier-next

热度 65 更新于 开发与构建

Highly effective, super-configurable, well-tested web page minifier (enhanced successor to HTML Minifier)

npmauto-collected

安装

npm
npm install -g html-minifier-next

通过 npm 安装。

HTML Minifier Next

Your web page optimization precision tool: HTML Minifier Next (HMN) is a highly effective, super-configurable, well-tested HTML minifier, written in JavaScript, that also handles 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). HMN is the official successor to HTML Minifier: It’s actively maintained, richer in features, easier to use, and significantly faster. Note that HMN is largely compatible with HM and HMT but isn’t fully drop-in—find migration guidance in the changelog.

Installation

HTML Minifier Next is ESM-only and requires Node.js ≥22.13.

For use as a command-line app, use npx:

npx html-minifier-next --help

(For immediate, zero-config use in the current folder: npx html-minifier-next --zero)

For programmatic use, install as a development dependency:

npm i -D html-minifier-next

General usage

CLI

Use npx html-minifier-next --help to check all available options:

| Option | Description | Example | | --- | --- | --- | | --zero, -z | Minify all HTML files in the current folder and its subfolders in place (except nodemodules), using comprehensive settings (standalone—flag is ignored when combined with other options) | npx html-minifier-next --zero | | --input-dir <dir, -I <dir | Specify an input directory | --input-dir=src | | --ignore-dir <patterns, -X <patterns | Exclude directories—relative to input directory—from processing (comma-separated, overrides config file setting) | --ignore-dir=libs, --ignore-dir=libs,vendor,nodemodules | | --output-dir <dir, -O <dir | Specify an output directory | --output-dir=dist | | --input <file, -i <file | Specify input file (alternative to positional argument; pair with --output for file output) | npx html-minifier-next -i input.html -o output.html | | --output <file, -o <file | Specify output file (reads from --input file argument or STDIN; outputs to STDOUT if not specified) | File to file: npx html-minifier-next input.html -o output.html<brFile to file (explicit): npx html-minifier-next -i input.html -o output.html<brPipe to file: cat input.html \| npx html-minifier-next -o output.html<brFile to STDOUT: npx html-minifier-next input.html | | --file-ext <extensions, -f <extensions | Specify file extension(s) to process (comma-separated, overrides config file setting); defaults to html,htm,shtml,shtm; use for all files | --file-ext=html,php, --file-ext='' | | --preset <name, -p <name | Use a preset configuration (conservative or comprehensive) | --preset=conservative | | --config-file <file, -c <file | Use a configuration file (defaults to html-minifier-next.config.json in the working directory, if present) | --config-file=path/to/config.json | | --verbose, -v | Show detailed processing information (active options, file statistics) | npx html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace | | --dry, -d | Dry run: Process and report statistics without writing output | npx html-minifier-next input.html --dry --collapse-whitespace |

Configuration file

You can use a configuration file to specify options. If an html-minifier-next.config.json file is present in the current working directory—only there; subfolders (including --input-dir) and parent folders are not searched—the CLI picks it up automatically, with a note on STDERR confirming this. An explicit --config-file takes precedence. (The standalone --zero mode is deliberately config-free and ignores default config files.) The file can be either in JSON format or a JavaScript module that exports the configuration object:

JSON configuration example:

{
  "collapseWhitespace": true,
  "removeComments": true,
  "fileExt": "html,php",
  "ignoreDir": "libs,vendor"
}