minify-html
A Rust HTML minifier meticulously optimised for speed and effectiveness, with bindings for other languages.
- Advanced minification strategy beats other minifiers while being much faster.
- Uses SIMD searching, direct tries, and lookup tables.
- Handles invalid HTML, with extensive testing and fuzzing.
- Natively binds to esbuild for super fast JS and CSS minification.
Performance
Comparison with html-minfier and minimize, run on the top web pages. See the breakdown here.


The onepass variant is even more optimised for speed. See its README for more details.
Compatibility and usage
CLI
Precompiled binaries are available for Linux, macOS, and Windows.
### Get
[Linux x64](https://wilsonl.in/minify-html/bin/0.7.2-linux-x86_64) |
[Linux ARM64](https://wilsonl.in/minify-html/bin/0.7.2-linux-arm64) |
[macOS x64](https://wilsonl.in/minify-html/bin/0.7.2-macos-x86_64) |
[macOS ARM64](https://wilsonl.in/minify-html/bin/0.7.2-macos-arm64) |
[Windows x64](https://wilsonl.in/minify-html/bin/0.7.2-windows-x86_64.exe)
### Use
Use the `--help` argument for more details.
```bash
minify-html --output /path/to/output.min.html --keep-closing-tags --minify-css /path/to/src.html
```
Rust
### Get
```toml
[dependencies]
minify-html = { version = "0.7.2", features = ["js-esbuild"] }
```
Building with the `js-esbuild` feature requires the Go compiler to be installed as well, to build the [JS and CSS minifier](https://github.com/wilsonzlin/esbuild-rs).
If the `js-esbuild` feature is not enabled, `cfg.minify_js` and `cfg.minify_css` will have no effect.
### Use
Check out the [docs](https://docs.rs/minify-html) for API and usage examples.
Node.js
- Package: [@minify-html/js](https://www.npmjs.com/package/@minify-html/js)
- Binding: [N-API](https://nodejs.org/api/n-api.html)
- Platforms: Linux (ARM64 and x64), macOS (ARM64 and x64), Windows (x64); Node.js 8.6.0 and higher
### Get
Using npm:
```bash
npm i @minify-html/js
```
Using Yarn:
```bash
yarn add @minify-html/js
```
### Use
TypeScript definitions are available.
```ts
import * as minifyHtml from "@minify-html/js";
// Or `const minifyHtml = require("@minify-html/js")` if not using TS/ESM.
const cfg = minifyHtml.createConfiguration({ keep_spaces_between_attributes: true, keep_comments: true });
const minified = minifyHtml.minify("Hello, world!
", cfg); ``` All [`Cfg` fields](https://docs.rs/minify-html/latest/minify_html/struct.Cfg.html) are available as snake_case properties on the object provided to `createConfiguration`; if any are not set, they default to `false`.
Java
- Package: [in.wilsonl.minifyhtml](https://search.maven.org/artifact/in.wilsonl.minifyhtml/minify-html)
- Binding: [JNI](https://github.com/jni-rs/jni-rs)
- Platforms: Linux, macOS, Windows; Java 7 and higher
### Get
Add as a Maven dependency:
```xml
Hello, world!
", cfg); ``` All [`Cfg` fields](https://docs.rs/minify-html/latest/minify_html/struct.Cfg.html) are available as camelCase setter methods on the `Builder`; if any are not set, they default to `false`.
Python
- Package: [minify-html](https://pypi.org/project/minify-html)
- Binding: [PyO3](https://github.com/PyO3/pyo3)
- Platforms: Linux (ARM64 and x64), macOS (ARM64 and x64), Windows (x64); Python 3.8 to 3.10
### Get
Add the PyPI project as a dependency and install it using `pip` or `pipenv`.
### Use
```python
import minify_html
minified = minify_html.minify("Hello, world!
", minify_js=True, remove_processing_instructions=True) ``` All [`Cfg` fields](https://docs.rs/minify-html/latest/minify_html/struct.Cfg.html) are available as Python keyword arguments; if any are omitted, they default to `False`.
Ruby
- Package: [minify_html](https://rubygems.org/gems/minify_html)
- Binding: [Rutie](https://github.com/danielpclark/rutie)
- Platforms: Linux, macOS; Ruby 2.5 and higher
### Get
Add the library as a dependency to `Gemfile` or `*.gemspec`.
### Use
```ruby
require 'minify_html'
print MinifyHtml.minify("Hello, world!
", { :keep_spaces_between_attributes => true, :minify_js => true }) ``` All [`Cfg` fields](https://docs.rs/minify-html/latest/minify_html/struct.Cfg.html) are available; if any are omitted, they default to `false`.Minification
Note that some of the minification done can result in HTML that will not pass validation, but remain interpreted and rendered correctly by the browser; essentially, the laxness of the browser is taken advantage of for better minification. These can be turned off via the Cfg object.
Whitespace
minify-html has advanced context-aware whitespace minification that does things such as:
- Leave whitespace untouched in
preandcode, which are whitespace sensitive. - Trim and collapse whitespace in content tags, as whitespace is collapsed anyway when rendered.
- Remove whitespace in layout tags, which allows the use of inline layouts while keeping formatted code.
Methods
There are three whitespace minification methods. When processing text content, minify-html chooses which ones to use depending on the containing element.
Collapse whitespace
> **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) elements. Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).| Before | After | ||||||||
|---|---|---|---|---|---|---|---|---|---|
|
```html
↵ ··The·quick·brown·fox↵ ··jumps·over·the·lazy↵ ··dog.↵ ``` |
```html
·The·quick·brown·fox·jumps·over·the·lazy·dog.· ``` </table> </details>Destroy whole whitespace> **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs), [content](src/spec/tag/whitespace.rs), [content-first](./src/spec/tag/whitespace.rs), and [formatting](./src/spec/tag/whitespace.rs) elements. Remove any text nodes between tags that only consist of whitespace characters.
|