Skip to main content

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 for super fast JS 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.

Chart showing speed of HTML minifiersChart showing compression of HTML minifiers

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 | Linux ARM64 | macOS x64 | macOS ARM64 | Windows x64

Use

Use the --help argument for more details.

minify-html --output /path/to/output.min.html --keep-closing-tags --minify-css /path/to/src.html
Rust

Get

[dependencies]
minify-html = { version = "0.9.1" }

Use

Check out the docs for API and usage examples.

Deno

Use

import init, {minify} from "https://wilsonl.in/minify-html/deno/0.9.1/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 * as minifyHtml from "@minify-html/node";
// Or `const minifyHtml = require("@minify-html/node")` if not using TS/ESM.

const minified = minifyHtml.minify("<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

Get

Add as a Maven dependency:

<dependency>
  <groupId>in.wilsonl.minifyhtml</groupId>
  <artifactId>minify-html</artifactId>
  <version>0.9.1</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.10

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: 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>", { :keep_spaces_between_attributes => true, :minify_js => true })

All Cfg fields are available; if any are omitted, they default to false.

WASM

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 and code, 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).

BeforeAfter
<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.

BeforeAfter
<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.

BeforeAfter
<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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

minify_html-0.9.1-cp310-none-win_amd64.whl (545.5 kB view details)

Uploaded CPython 3.10Windows x86-64

minify_html-0.9.1-cp310-cp310-manylinux_2_28_aarch64.whl (625.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

minify_html-0.9.1-cp310-cp310-manylinux_2_27_x86_64.whl (693.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64

minify_html-0.9.1-cp310-cp310-macosx_11_0_arm64.whl (529.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

minify_html-0.9.1-cp310-cp310-macosx_10_7_x86_64.whl (582.7 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

minify_html-0.9.1-cp39-none-win_amd64.whl (545.5 kB view details)

Uploaded CPython 3.9Windows x86-64

minify_html-0.9.1-cp39-cp39-manylinux_2_28_aarch64.whl (625.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

minify_html-0.9.1-cp39-cp39-manylinux_2_27_x86_64.whl (693.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64

minify_html-0.9.1-cp39-cp39-macosx_11_0_arm64.whl (529.6 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

minify_html-0.9.1-cp39-cp39-macosx_10_7_x86_64.whl (582.7 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

minify_html-0.9.1-cp38-none-win_amd64.whl (545.4 kB view details)

Uploaded CPython 3.8Windows x86-64

minify_html-0.9.1-cp38-cp38-manylinux_2_28_aarch64.whl (625.0 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ ARM64

minify_html-0.9.1-cp38-cp38-manylinux_2_27_x86_64.whl (693.5 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.27+ x86-64

minify_html-0.9.1-cp38-cp38-macosx_11_0_arm64.whl (529.6 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

minify_html-0.9.1-cp38-cp38-macosx_10_7_x86_64.whl (582.5 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

File details

Details for the file minify_html-0.9.1-cp310-none-win_amd64.whl.

File metadata

  • Download URL: minify_html-0.9.1-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 545.5 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for minify_html-0.9.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 df4a4f13560a9916c09a5ff6235026d2b32637637d07c0b6ffec5ad2c2aa339a
MD5 ef0fe3bab7ef3bf626dcaf5d01999792
BLAKE2b-256 e205b154a21b164378ac26b778621012bb1a139525d86a8ee2778bf39b3c0847

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8be7e411d516fa50e52e1ef1282a47bb9df34d8967755954e0ee2c1a7c1d3f05
MD5 9ed634feef731a87d2787936329b127b
BLAKE2b-256 99726c66e29a40d825f26ad6370870515ecb695112e71388379aefdfa6fbd66a

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp310-cp310-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp310-cp310-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 82e1281644967c26b442d59b284c4936f567f986e7a4eeae7dea7a2bb168d1f0
MD5 ddffbf7b9aae1c41a77a52ab2ca2750e
BLAKE2b-256 8539d244964166dd6e02f3056a87e1cad3e75dac26730f6869ccaefa38cd68ae

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d0292a2a2c93538b1bbb22ae3d4e25197601b2edf61886ea57ef5db91415bfdf
MD5 c9a9c454ee28ecffa8e4599b0f2ad0fb
BLAKE2b-256 63253f869274766bc78b83d332966a40dcca5ce8be3f7d9b8e01172dd82215b4

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 07d00ff948f36bc7bd4f84b3656da41ad382745b659d151a1e44cad0f78a9ad5
MD5 fb2df81071ea297b3c565d14abea25d3
BLAKE2b-256 876a38fac91b4b88727df958b4a4d2e11d7fa9c91f47622165c40dbd8f3c04a9

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp39-none-win_amd64.whl.

File metadata

  • Download URL: minify_html-0.9.1-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 545.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for minify_html-0.9.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 3a12405e6f94620eeb6c0f2db7bae613e265e048260abba5251137f3e64b567e
MD5 2027b616c2bab1c0fe23e0a8336b7b84
BLAKE2b-256 984781af8509ab6dca92a0643e180a8c0ff4e00309aa9fd2250405f9dc9d7f9c

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp39-cp39-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9767b3c97093ee6a4e1a15cef921610ac97eb0fe97cd21f571e2081899564b5e
MD5 9ff750f421502ad5c9ea8521fba4a13a
BLAKE2b-256 00c68d273b783a01e362c90dbb17dd9537c0da7465dae563ff4f49429de7dbdf

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp39-cp39-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp39-cp39-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 2de34a0eb8484161b73de22a3bde2b644ae8d60d66bbfb0370e189c074f05c77
MD5 967bcf99f94f91c2dc909a3f0f55b4bb
BLAKE2b-256 aeedf064932ba0c894888f7a4364f20bd07c10781052184d18647a9877e0aa5e

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ab2ecd3e3971d4fc634cf432327f7f3adb9f4a78b59d57623c67aa67137fb7c
MD5 342012e9bc0f0e5d331118fb655f544e
BLAKE2b-256 7d1d12c3d15df645c6e50db849bbedbf6dd321eeba5c875cf6412ab8e08b7f44

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 8276d00fec34efb3644bc8d57362cb7fa503b0eb245210635425511ade4a8f26
MD5 5b7cf656967ca79857f0cf2f93efa9dd
BLAKE2b-256 62415eae3a73067e7bfc2e7a8d4036d9f19ae2539ff7f2254eee73222e7f9362

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp38-none-win_amd64.whl.

File metadata

  • Download URL: minify_html-0.9.1-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 545.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for minify_html-0.9.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 f564bb8ca45a5c2d2a27cdcd8085fab0ceaacf3b043ba2ee05c3b3a5d2c62843
MD5 c0e0196001f12c3891d86ac2e4374635
BLAKE2b-256 4b87fa4fe6f7b3c5db6a4217dc73bfe4774e48932534548b30bf444b3686fcd5

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp38-cp38-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8baeff6e1cc2b08a31a5b87bce3c83170c6a6d5371b0c15a4e88332af873c986
MD5 e640d044ce6aa3586a302e768f17da93
BLAKE2b-256 0a215d063ae71c5b52c0532ed5b69ba92a45962f6babc706bd29817e4a67764d

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp38-cp38-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp38-cp38-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 908125c075befaa6098601f31077906d727992c1125d7fa5a1a4093229c4b5f0
MD5 e46c6d11ca3bd3095ead55f2dd2f8851
BLAKE2b-256 0e12e6200054bf766e7708d9867a82fcf8a1ae04df0bae02e4ddd5bc451b055d

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dd28862b766c2278393efa13928ce65485529e0cae9b0758b6783d0e6d246213
MD5 f850c6d0dbd1f4cb3e54a87dda4d31d8
BLAKE2b-256 1f58977ac2b089321bac4a9d13a5d9a9f554f870e842b72e1312395942b5821f

See more details on using hashes here.

File details

Details for the file minify_html-0.9.1-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.9.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 4458e1dfc67e104361ba57b24bb22e4998de2d71bddbf50246a4c457b26a5f66
MD5 d7d8a0a4d138a187a2e6082b5d348ba9
BLAKE2b-256 9bfe1c14078caf9c3913d6ca39214ba7d5ad0b2071910758908c3af4c2b29845

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page