Fast and smart HTML + JS minifier
Project description
minify-html
An HTML minifier meticulously optimised for both speed and effectiveness, available for Rust, Node.js, Python, Java, and Ruby.
- Advanced minification strategy beats other minifiers with only one pass.
- Uses zero memory allocations, SIMD searching, direct tries, and lookup tables.
- Well tested with a large test suite and extensive fuzzing.
- Natively binds to esbuild for super fast JS minification.
Performance
Comparison with html-minfier and minimize, run on the top web pages. See the breakdown here.
Usage
CLI
Precompiled binaries are available for x86-64 Linux, macOS, and Windows.
Get
Use
Use the --help
argument for more details.
minify-html --src /path/to/src.html --out /path/to/output.min.html
API
Rust
Get
[dependencies]
minify-html = { version = "0.3.7", features = ["js-esbuild"] }
Building with the js-esbuild
feature requires the Go compiler to be installed as well, to build the JS minifier.
If the js-esbuild
feature is not enabled, cfg.minify_js
will have no effect.
Use
Check out the docs for API and usage examples.
Node.js
- Package: @minify-html/js
- Binding: N-API
- Platforms: Linux, macOS, Windows; Node.js 8.6.0 and higher
Get
Using npm:
npm i @minify-html/js
Using Yarn:
yarn add @minify-html/js
Use
const minifyHtml = require("@minify-html/js");
const cfg = minifyHtml.createConfiguration({ minifyJs: false });
const minified = minifyHtml.minify("<p> Hello, world! </p>", cfg);
// Alternatively, minify in place to avoid copying.
const source = Buffer.from("<p> Hello, world! </p>");
// This is a Buffer representing a slice of `source`, not newly allocated memory.
const minified = minifyHtml.minifyInPlace(source, cfg);
minify-html is also available for TypeScript:
import * as minifyHtml from "@minify-html/js";
import * as fs from "fs";
const cfg = minifyHtml.createConfiguration({ minifyJs: false });
const minified = minifyHtml.minify("<p> Hello, world! </p>", cfg);
// Or alternatively:
const minified = minifyHtml.minifyInPlace(fs.readFileSync("source.html"), cfg);
Java
- Package: in.wilsonl.minifyhtml
- Binding: JNI
- Platforms: Linux, macOS, Windows; Java 7 and higher
Get
Add as a Maven dependency:
<dependency>
<groupId>in.wilsonl.minifyhtml</groupId>
<artifactId>minify-html</artifactId>
<version>0.3.7</version>
</dependency>
Use
import in.wilsonl.minifyhtml.MinifyHtml;
MinifyHtml.Configuration cfg = new MinifyHtml.Configuration.Builder()
.setMinifyJs(false)
.build();
try {
String minified = MinifyHtml.minify("<p> Hello, world! </p>", cfg);
} catch (MinifyHtml.SyntaxException e) {
System.err.println(e.getMessage());
}
// Alternatively, minify in place:
assert source instanceof ByteBuffer && source.isDirect();
MinifyHtml.minifyInPlace(source, cfg);
Python
- Package: minify-html
- Binding: PyO3
- Platforms: Linux, macOS, Windows; Python 3.5 and higher
Get
Add the PyPI project as a dependency and install it using pip
or pipenv
.
Use
import minify_html
try:
minified = minify_html.minify("<p> Hello, world! </p>", minify_js=False)
except SyntaxError as e:
print(e)
Ruby
- Package: minify_html
- Binding: Rutie
- Platforms: Linux, macOS; Ruby 2.5 and higher
Get
Add the library as a dependency to Gemfile
or *.gemspec
.
Use
require 'minify_html'
print MinifyHtml.minify("<p> Hello, world! </p>", { :minify_js => false })
Minification
Whitespace
minify-html has advanced context-aware whitespace minification that does things such as:
- Leave whitespace untouched in
pre
andcode
, 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 elements.
Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).
Before | After |
---|---|
<p>↵
··The·quick·brown·fox↵
··jumps·over·the·lazy↵
··dog.↵
</p>
|
<p>·The·quick·brown·fox·jumps·over·the·lazy·dog.·</p>
|
Destroy whole whitespace
Applies to: any element except whitespace sensitive, content, content-first, and formatting elements.
Remove any text nodes between tags that only consist of whitespace characters.
Before | After |
---|---|
<ul>↵
··<li>A</li>↵
··<li>B</li>↵
··<li>C</li>↵
</ul>
|
<ul>↵
··<li>A</li><li>B</li><li>C</li>↵
</ul>
|
Trim whitespace
Applies to: any element except whitespace sensitive and formatting elements.
Remove any leading/trailing whitespace from any leading/trailing text nodes of a tag.
Before | After |
---|---|
<p>↵
··Hey,·I·<em>just</em>·found↵
··out·about·this·<strong>cool</strong>·website!↵
··<sup>[1]</sup>↵
</p>
|
<p>Hey,·I·<em>just</em>·found↵
··out·about·this·<strong>cool</strong>·website!↵
··<sup>[1]</sup></p>
|
Element types
minify-html recognises elements based on one of a few ways it assumes they are used. By making these assumptions, it can apply optimal whitespace minification strategies.
Group | Elements | Expected children |
---|---|---|
Formatting | a , strong , and others |
Formatting elements, text. |
Content | h1 , p , and others |
Formatting elements, text. |
Layout | div , ul , and others |
Layout elements, content elements. |
Content-first | label , li , and others |
Like content but could be layout with only one child. |
Formatting elements
Whitespace is collapsed.
Formatting elements are usually inline elements that wrap around part of some text in a content element, so its whitespace isn't trimmed as they're probably part of the content.
Content elements
Whitespace is trimmed and collapsed.
Content elements usually represent a contiguous and complete unit of content such as a paragraph. As such, whitespace is significant but sequences of them are most likely due to formatting.
Before
<p>↵
··Hey,·I·<em>just</em>·found↵
··out·about·this·<strong>cool</strong>·website!↵
··<sup>[1]</sup>↵
</p>
After
<p>Hey,·I·<em>just</em>·found·out·about·this·<strong>cool</strong>·website!·<sup>[1]</sup></p>
Layout elements
Whitespace is trimmed and collapsed. Whole whitespace is removed.
These elements should only contain other elements and no text. This makes it possible to remove whole whitespace, which is useful when using display: inline-block
so that whitespace between elements (e.g. indentation) does not alter layout and styling.
Before
<ul>↵
··<li>A</li>↵
··<li>B</li>↵
··<li>C</li>↵
</ul>
After
<ul><li>A</li><li>B</li><li>C</li></ul>
Content-first elements
Whitespace is trimmed and collapsed.
These elements are usually like content elements but are occasionally used like a layout element with one child. Whole whitespace is not removed as it might contain content, but this is OK for using as layout as there is only one child and whitespace is trimmed.
Before
<li>↵
··<article>↵
····<section></section>↵
····<section></section>↵
··</article>↵
</li>
After
<li><article><section></section><section></section></article></li>
Tags
Optional closing tags are removed.
Attributes
Any entities in attribute values are decoded, and then the shortest representation of the value is calculated and used:
- Double quoted, with any
"
encoded. - Single quoted, with any
'
encoded. - Unquoted, with
"
/'
first character (if applicable),>
last character (if applicable), and any whitespace encoded.
class
and d
attributes have their whitespace (after any decoding) trimmed and collapsed.
Boolean attribute values are removed. Some other attributes are completely removed if their value is empty or the default value after any processing.
type
attributes on script
tags with a value equaling a JavaScript MIME type are removed.
If an attribute value is empty after any processing, everything but the name is completely removed (i.e. no =
), as an empty attribute is implicitly the same as an attribute with an empty string value.
Spaces are removed between attributes if possible.
Entities
Entities are decoded if they're valid and shorter or equal in length when decoded.
Numeric entities that do not refer to a valid Unicode Scalar Value are replaced with the replacement character.
If an entity is unintentionally formed after decoding, the leading ampersand is encoded, e.g. &amp;
becomes &amp;
. This is done as &
is equal to or shorter than all other entity representations of characters part of an entity ([&#a-zA-Z0-9;]
), and there is no other conflicting entity name that starts with amp
.
It's possible to get an unintentional entity after removing comments, e.g. &am<!-- -->p
.
Left chevrons after any decoding in text are encoded to <
if possible or <
otherwise.
Comments
Comments are removed.
Ignored
Bangs, processing instructions, and empty elements are not removed as it is assumed there is a special reason for their declaration.
Parsing
- Input must be UTF-8.
- Opening tags must not be omitted.
- Escaped and double-escaped script content are not supported.
Issues and contributions
Pull requests and any contributions welcome!
If minify-html did something unexpected, misunderstood some syntax, or incorrectly kept/removed some code, raise an issue with some relevant code that can be used to reproduce and investigate the issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for minify_html-0.3.7-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c8c82e148d1ceca11349a7894937f9247ece345152b9320cbb8215bc14fe0bc |
|
MD5 | 4d827c154798928559a392f77bcb28b4 |
|
BLAKE2b-256 | aee19b8ed8c3b3adca4d67a9544735ee6947ea88b557f304642b7f4324693432 |
Hashes for minify_html-0.3.7-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99d313d3c3d003230afe6af718abb74bf683947017d2ceecd18452a7e93ba33d |
|
MD5 | f3f364b6cb875cd10f8ea6a2fb558da3 |
|
BLAKE2b-256 | d0d0d563c11fadd6f7768b7ebd2e50610164c9dca74e40349a04cb2ccb9b325f |
Hashes for minify_html-0.3.7-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8be08e985f3e5f982af47f6c7fd9f93e704c9f8fec25f41b7a9c9a30fdc67d08 |
|
MD5 | 2c839cee127fc89a6cc7568ee03352a2 |
|
BLAKE2b-256 | 609711aa9197168d785059b9e22e3d65ade1ead33040be45f0bbed8b6990b5c2 |
Hashes for minify_html-0.3.7-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c39b1223ce2399292315222e715ec79d9097dda09ed0eec4db4e24f580929054 |
|
MD5 | c87b9ffbbd0376fb061940dca87a86e2 |
|
BLAKE2b-256 | 9a79c014b709f4043f786f0395e0038c0fcc3f3cbab3468b9b8d865e26c0a856 |
Hashes for minify_html-0.3.7-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48456e58efe7c8e505fc1ddd6732dca9b3cd2c3fb5b283b91b84d81daea390d0 |
|
MD5 | 1f0aa97ba02c64cc7fba0792f4c7a330 |
|
BLAKE2b-256 | 41aed916ddfecc52a9c0f0702604b12397441ba45a8644a2ecf4d1cb05da5df7 |
Hashes for minify_html-0.3.7-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 240058e6d3a60520684a26f3c48a6408d231673bde074ca65e86fe6b16994a2f |
|
MD5 | b9cba18d36137d0720ce92ed6ca41ff4 |
|
BLAKE2b-256 | b76e29a5763c71946e389b1cc506cd336787d900626f217bdc422400308a1d46 |
Hashes for minify_html-0.3.7-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 223bcb9602e5f03444e77e7d66f0c4ccd633f343b132660b38cc92253a96b5f7 |
|
MD5 | cb031f816b1abd84293714475b223de2 |
|
BLAKE2b-256 | 8cd0c4e23cb05bfc4cf43e8ed62e48800e257139e2ba4a89c9f4370b99e615ad |
Hashes for minify_html-0.3.7-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51e62f01ddf8c1802e7dc6f5911a231060a9c8d2090bf79ff146a3463d77ae65 |
|
MD5 | 8f297b2b27ad4c82520877f99d9cdaee |
|
BLAKE2b-256 | 7308ff7e4d48a7279eaf08d265398ea3e94ecff60b75ee89e3fc7d74fb02d992 |
Hashes for minify_html-0.3.7-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16b1bcd4cd7934fa366149c1f1e9c8af9c6503b9eed2e3c15b9032e878718fb3 |
|
MD5 | 683947468775dbae1e471523c081267f |
|
BLAKE2b-256 | 73ec872d0c6da53f2868816380beae0b5802ce4a79d740f138dc1cc690e75224 |
Hashes for minify_html-0.3.7-cp35-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | adc772f51743902cd94cbd158a3759cb6a10f3f7348563bf624d8ac795bdf913 |
|
MD5 | 18ce28876cc462e26c739ef8ce0fb53a |
|
BLAKE2b-256 | 4e8a9165395c067c916bb055ed9e8e82ab73ac45d390e10d7a8025adca236d11 |
Hashes for minify_html-0.3.7-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b63d18227bc69fcb8aee8eab6e0fcf9be39b990d3729672be18e65f485878170 |
|
MD5 | 756968b11b78703e97076da91882e48b |
|
BLAKE2b-256 | 4e51d25faf0fb8d4426e4904c2141d3b562bd140264cd16daa5929d2d6aa1e05 |
Hashes for minify_html-0.3.7-cp35-cp35m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b38e7973382d053bb43c24b1c5e63a01bdb3c08d9304d9ab7e87163f46bbaec2 |
|
MD5 | eac5dc35f1c23a5bc866300b48c39673 |
|
BLAKE2b-256 | 0abe6a19922f1debc5badb237c31ef6e600b05f69ce07091f09e1da4ca6da3fb |