Extremely fast and smart HTML + JS + CSS minifier
Project description
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.
- Uses minify-js and lightningcss for super fast JS and CSS minification.
View the changelog to see the latest updates.
Performance
Comparison with html-minifier 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
The CLI is called minhtml. Precompiled binaries are available for Linux (ARM64 and x64), macOS (ARM64 and x64), and Windows (x64). You can download them in the GitHub release.
If you have Cargo installed, you can also build and install from source: cargo install minhtml
.
Use
Use the --help
argument for more details.
minhtml --output /path/to/output.min.html --keep-closing-tags --minify-css /path/to/src.html
To quickly parallel process a batch of files in place:
minhtml --keep-closing-tags --minify-css /path/to/**/*.html
Deno
- Package: https://wilsonl.in/minify-html/deno/0.13.2/index.js
- Binding: WASM
- Platforms: All
Use
import init, {minify} from "https://wilsonl.in/minify-html/deno/0.13.2/index.js";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
await init();
const minified = decoder.decode(minify(encoder.encode("<p> Hello, world! </p>"), { keep_spaces_between_attributes: true, keep_comments: true }));
All Cfg
fields are available as snake_case properties on the object provided as the second argument; if any are not set, they default to false
.
Node.js
- Package: @minify-html/node
- Binding: Neon
- Platforms: Linux (ARM64 and x64), macOS (ARM64 and x64), Windows (x64); Node.js 8.6.0 and higher
Get
Using npm:
npm i @minify-html/node
Using Yarn:
yarn add @minify-html/node
Use
TypeScript definitions are available.
import { Buffer } from "node:buffer";
import minifyHtml from "@minify-html/node";
// Or `const minifyHtml = require("@minify-html/node")` if not using TS/ESM.
const minified = minifyHtml.minify(Buffer.from("<p> Hello, world! </p>"), { keep_spaces_between_attributes: true, keep_comments: true });
All Cfg
fields are available as snake_case properties on the object provided as the second argument; if any are not set, they default to false
.
Java
- Package: in.wilsonl.minifyhtml
- Binding: JNI
- Platforms: Linux (ARM64 and x64), macOS (ARM64 and x64), Windows (x64); Java 7 and higher
Get
Add as a Maven dependency:
<dependency>
<groupId>in.wilsonl.minifyhtml</groupId>
<artifactId>minify-html</artifactId>
<version>0.13.2</version>
</dependency>
Use
import in.wilsonl.minifyhtml.Configuration;
import in.wilsonl.minifyhtml.MinifyHtml;
Configuration cfg = new Configuration.Builder()
.setKeepHtmlAndHeadOpeningTags(true)
.setMinifyCss(true)
.build();
String minified = MinifyHtml.minify("<p> Hello, world! </p>", cfg);
All Cfg
fields are available as camelCase setter methods on the Builder
; if any are not set, they default to false
.
Python
- Package: minify-html
- Binding: PyO3
- Platforms: Linux (ARM64 and x64), macOS (ARM64 and x64), Windows (x64); Python 3.8 to 3.12
Get
Add the PyPI project as a dependency and install it using pip
or pipenv
.
Use
import minify_html
minified = minify_html.minify("<p> Hello, world! </p>", minify_js=True, remove_processing_instructions=True)
All Cfg
fields are available as Python keyword arguments; if any are omitted, they default to False
.
Ruby
- Package: minify_html
- Binding: rb-sys and magnus
- Platforms: Linux (ARM64 and x64), macOS (ARM64 and x64), Windows (x64); Ruby 2.7 to 3.2
Get
Add the library as a dependency to Gemfile
or *.gemspec
.
Use
require 'minify_html'
print minify_html("<p> Hello, world! </p>", { :keep_spaces_between_attributes => true, :minify_js => true })
All Cfg
fields are available; if any are omitted, they default to false
.
WASM
- Package: @minify-html/wasm
- Binding: WASM
- Platforms: All
A bundler may be required to use the WebAssembly module, see this for more details.
Use
import init, {minify} from "@minify-html/wasm";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
await init();
const minified = decoder.decode(minify(encoder.encode("<p> Hello, world! </p>"), { keep_spaces_between_attributes: true, keep_comments: true }));
All Cfg
fields are available as snake_case properties on the object provided as the second argument; if any are not set, 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. To prevent this, refer to these configuration options:
do_not_minify_doctype
ensure_spec_compliant_unquoted_attribute_values
keep_spaces_between_attributes
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 assumes HTML and SVG elements are used in specific ways, based on standards and best practices. By making these assumptions, it can apply optimal whitespace minification strategies. If these assumptions do not hold, consider adjusting the HTML source or turning off whitespace minification.
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 opening and 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), any>
, and any whitespace encoded.
Attributes have their whitespace (after any decoding) trimmed and collapsed when possible.
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 when possible.
Entities
Entities are decoded if they're valid and shorter or equal in length when decoded. UTF-8 sequences that have a shorter entity representation are encoded.
Numeric entities that do not refer to a valid Unicode Scalar Value are replaced with the replacement character.
Encoding is avoided when possible; for example, <
are only encoded in content if they are followed by a valid tag name character.
If necessary, the shortest entity representation is chosen.
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
minify-html can process any HTML, handling all possible syntax (including invalid ones) gracefully like browsers. See Parsing.md for more details.
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 Distribution
Built Distributions
Hashes for minify_html-0.13.2-cp312-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 404ca0a84224826ca95eaf22f16eee218492c64b8c7b1f9422ff36fbdcb09626 |
|
MD5 | 1a8580ff2ec10ccf00b54841066834d3 |
|
BLAKE2b-256 | d737713c99e6973534104d46559ceb839c24f08977fe5b14c44afb1ddf4feefa |
Hashes for minify_html-0.13.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8383db6a6a302d891d3dd8ab812503222410082a288e9ec4ca6e5cbbc287ca5e |
|
MD5 | 7128353ebe2016c7c70d1213a41ea87d |
|
BLAKE2b-256 | 9e723af212d018b25b15354fa10859d3bac5bf694509d8483de43a76e4706f17 |
Hashes for minify_html-0.13.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e118a025aa1151ca100fc14e76236b9ce1efd709a7d44a9daffb7dd0bccce832 |
|
MD5 | ed58086e8cdcf6ee0e5c233a44e4f4c4 |
|
BLAKE2b-256 | 8e25ae2ecf28a6ab863ffedf27d97b17d80b8421adb1804f6a846119677b0158 |
Hashes for minify_html-0.13.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a70cbf546bd007a44d468cd178006d0208b7afec981a422566b4da3e898ac3f |
|
MD5 | 7783c7ba1d6bf6922f50f88f26f7ba56 |
|
BLAKE2b-256 | 86a3fdf1512c3e1282a94ad41cc32241d7972f3d3855bfcdd7d5880b547b7a6a |
Hashes for minify_html-0.13.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eab42403c4016e36880f5f6a563243c1c659e96ca96b0694c127af5a0330cb5c |
|
MD5 | bb836bf253ce0df540cbddb8eeeb8852 |
|
BLAKE2b-256 | 6a68942a8f08ffce533d8e1089c3d2a1b011a41b7f8b88e1a79a3bdc14dc5905 |
Hashes for minify_html-0.13.2-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0613b0d896cc5e4d0a7ec169252562937fb27da24d3cd4f85bdb732f31587b3e |
|
MD5 | d5a7687e63daa050a52c202f57135fd6 |
|
BLAKE2b-256 | fe7dedd9bb2f13fc53a7a0945b019912a08f0bd8fd27eeda910e1c6e0cad2ba0 |
Hashes for minify_html-0.13.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 444c809e0af68a8036c21c3ab60c294ccfdddb7a77411e9b4144ce026a2c97f6 |
|
MD5 | cfec9f6c3cf26a1dbc5a46109e6dd598 |
|
BLAKE2b-256 | 1f57d764e96ba12d0852acdbf4ae55f16f03429ab52f2df2d1c312d7f47f56b5 |
Hashes for minify_html-0.13.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b58c40d55c0dfef6c69bcb5869f6da34b8bc4396a01c5be1bb3e62ec5d1203f1 |
|
MD5 | 1481fe3bb3309e3c4e406ef2bbdb8616 |
|
BLAKE2b-256 | adb4624250532caf0f7f9134980a6d25d3f8e7a41fc8e83f1a11e0e182c23cc2 |
Hashes for minify_html-0.13.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3951c139d31b5d4feefb8d56922315a8eaae71b8829da739ca7159b1542954d2 |
|
MD5 | 29844a4eb1d85df40723fe054125cb4e |
|
BLAKE2b-256 | 22cd822ea182c8056b25fa419e1c61b5983a4b8cb25885ec0a9454192a3bd536 |
Hashes for minify_html-0.13.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 196e53412fc2f117d4187c5f5aacea418043201a6973ea9fc7ab0914ab93fc0a |
|
MD5 | b3587e839661dfef8cb7ededeb427032 |
|
BLAKE2b-256 | 79ded644ac35830befcfe3f2d46ff31a13ff584c621ac192aac20257ce68d950 |
Hashes for minify_html-0.13.2-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3e07f7f9b97d4d78072c0dd9992811e4105a0249271e03c06d8128bbaede192 |
|
MD5 | 36e1446a2458c8e562ca4e7a31a22e73 |
|
BLAKE2b-256 | d8a1663d6e317dd1c131e2f454ad52300e7062cc597c788af04be9ce48cb4d71 |
Hashes for minify_html-0.13.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85aeafcfbd31397b724ec5074e6827686e485f3d92c3e14b5a50059b1c8cb732 |
|
MD5 | e15ba1dc2808ab3fea10888cd4cac45c |
|
BLAKE2b-256 | f564aeb63405eb39a22eabf7c3f36f2fbb92b28a47349dd63e6569038463ce2a |
Hashes for minify_html-0.13.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ced6c5ff5a7ca17d3d287e1fef530c6f78487feb0714157012ea0157eb5c72e6 |
|
MD5 | e4dd54d1667f1dda756784715f5f9e80 |
|
BLAKE2b-256 | 10d430dcf8ab5865f025b9bd6bcaebdebda9eecaf778ec65a2d595f9083e5589 |
Hashes for minify_html-0.13.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9628458c5ce0dab18740a881e308f1621bcbb082e9fa59b01cab3b9af76f9028 |
|
MD5 | 0bc3d13aeba48818c2c7c26864575091 |
|
BLAKE2b-256 | 1e76a5ae61c99c4af01826bb6785c82807f011f104a3efcb5ae5a9e6fd7d226d |
Hashes for minify_html-0.13.2-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db5170219ea1475a2701c1a86e9fcdf57bb29894c9a09823b3bdc1cca328cc0c |
|
MD5 | 5a1a2a0b2c632dd3d84e8606aa6b658c |
|
BLAKE2b-256 | 4859080609b09aea9c823209a0efc9f2376f205d932396b513a300b078accc5e |
Hashes for minify_html-0.13.2-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b7ba9f8e22a03072d5d954e080fb2dfbc6714c66059e1aafdce050b9f8b7941 |
|
MD5 | 27c52c63b909dcdd09ecb19b92a0c179 |
|
BLAKE2b-256 | 5c0028f1bf29383dead20fb8b84dc9826c3378d093380210af730969b4a62378 |
Hashes for minify_html-0.13.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3ad851e327befc09e82c9d93ed464e5ec5fdc189794a812c48a0a846d36ecf1 |
|
MD5 | d728b2257b4df775fccc1108bc90f166 |
|
BLAKE2b-256 | 18d5d708d7100ec3c35484e293696ee10b29a590a6e99c86e5df75c09779238a |
Hashes for minify_html-0.13.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e10dba5141cebab7185fcbaaba4817ff26387d6aaa13f272c8d916d288fefc6a |
|
MD5 | 358201d0edf2763910e14eae3c1538f1 |
|
BLAKE2b-256 | 3a0e1bf8e509c03dff4912a9afcd1c9494a9321ad65e79666b7a7581532e180f |
Hashes for minify_html-0.13.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f37cba6a272c820f246781d763358b6e2badce15a16bdd06fe607a8a4762156f |
|
MD5 | 91ecaf8c88ff1f943f08cc626d30a71f |
|
BLAKE2b-256 | aa3c5c8c2c07169bcdf86cecc54dd45a6f82040aca7889de010133f9ff6cf922 |
Hashes for minify_html-0.13.2-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 514b5bf58e7993593c168c4ffb08a90f8fdcabc53a91a8699d5a34304f0495d8 |
|
MD5 | 5fea94d3dffa8145db99e4a0808e659a |
|
BLAKE2b-256 | e95409f7a50be501f2314649f5e8aa75ed48c4bd08c4ec1e3bb634098ffb2a46 |
Hashes for minify_html-0.13.2-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41ab0a3df8af6d6b29d1be55059f5e5b51f325a79800a21dfd51711fd1d566ff |
|
MD5 | 9f511cbe32787b6a740a402e3c43b596 |
|
BLAKE2b-256 | 39321e8f0f7dca0f6d91a36919e1a182978aff7d3f2a8e4d869f3de52fd58106 |
Hashes for minify_html-0.13.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6e0b8c3d03536051d2d18ecf9e6cb0adc268d0002b208997755a4c571a69ccb |
|
MD5 | b54dc67b9db3d13abb0c5a24e0c7ff98 |
|
BLAKE2b-256 | 4ee131709b41648233027d0507f6a72557d1cbaf9828c49b6372e4230486815c |
Hashes for minify_html-0.13.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbfa7a6c253724f2855a56ec8d5a063aac0b0b61982677bd6f837769499b363a |
|
MD5 | 6acaabed6ad45e88a663f9120dda3dc8 |
|
BLAKE2b-256 | b596a0dbb893269e7a300a6790194c695766fee6f5c9cb1614ba402806e502c3 |
Hashes for minify_html-0.13.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be09986cdc141b344c527431476ba916ea82098c52d6294ac2a91d2b1d6ece30 |
|
MD5 | 8e1c53ccfaba7802a0a7a8914d47ac44 |
|
BLAKE2b-256 | 15312c2e0cf18004738fbc37bc3927939c373713fc04dafc35cf18b89ede25d9 |
Hashes for minify_html-0.13.2-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82f81447cdc44eda94a067aafc2568ef486c8645f1e428713146e0ebb96a89c1 |
|
MD5 | 3371fffff641071acf7805e943d9c2dc |
|
BLAKE2b-256 | c13fcec8c839d10b6a556dee37052b1219d615e82b33ffc48700601b5b740ddb |