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

To quickly parallel process a batch of files in place:

minify-html --keep-closing-tags --minify-css /path/to/**/*.html
Rust

Get

[dependencies]
minify-html = "0.10.4"

Use

Check out the docs for API and usage examples.

Deno

Use

import init, {minify} from "https://wilsonl.in/minify-html/deno/0.10.4/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 (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 * as 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

Get

Add as a Maven dependency:

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


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

minify_html-0.10.4.tar.gz (135.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

minify_html-0.10.4-cp311-none-win_amd64.whl (557.8 kB view details)

Uploaded CPython 3.11Windows x86-64

minify_html-0.10.4-cp311-cp311-manylinux_2_28_aarch64.whl (643.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

minify_html-0.10.4-cp311-cp311-manylinux_2_27_x86_64.whl (709.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64

minify_html-0.10.4-cp311-cp311-macosx_10_7_x86_64.whl (618.8 kB view details)

Uploaded CPython 3.11macOS 10.7+ x86-64

minify_html-0.10.4-cp310-none-win_amd64.whl (557.8 kB view details)

Uploaded CPython 3.10Windows x86-64

minify_html-0.10.4-cp310-cp310-manylinux_2_28_aarch64.whl (643.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

minify_html-0.10.4-cp310-cp310-manylinux_2_27_x86_64.whl (709.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64

minify_html-0.10.4-cp310-cp310-macosx_10_7_x86_64.whl (618.8 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

minify_html-0.10.4-cp39-none-win_amd64.whl (557.8 kB view details)

Uploaded CPython 3.9Windows x86-64

minify_html-0.10.4-cp39-cp39-manylinux_2_28_aarch64.whl (643.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

minify_html-0.10.4-cp39-cp39-manylinux_2_27_x86_64.whl (709.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64

minify_html-0.10.4-cp39-cp39-macosx_10_7_x86_64.whl (618.8 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

minify_html-0.10.4-cp38-none-win_amd64.whl (557.7 kB view details)

Uploaded CPython 3.8Windows x86-64

minify_html-0.10.4-cp38-cp38-manylinux_2_28_aarch64.whl (643.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ ARM64

minify_html-0.10.4-cp38-cp38-manylinux_2_27_x86_64.whl (708.6 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.27+ x86-64

minify_html-0.10.4-cp38-cp38-macosx_10_7_x86_64.whl (618.5 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

File details

Details for the file minify_html-0.10.4.tar.gz.

File metadata

  • Download URL: minify_html-0.10.4.tar.gz
  • Upload date:
  • Size: 135.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for minify_html-0.10.4.tar.gz
Algorithm Hash digest
SHA256 462963f022a0620bd2cc5f1a6eee9cabf9bce66ace95db3133cc9caa143e3e7f
MD5 130d2b63b2da7e1d58a8fcb28777477f
BLAKE2b-256 d248a08c911dcf39a80781148877a498077bb5131f325f36dcf479a37271cdc2

See more details on using hashes here.

File details

Details for the file minify_html-0.10.4-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for minify_html-0.10.4-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 4622a9cc464fac8976c6cf400496daa1fafe7250cebce573c2244bf9c4e6c518
MD5 d461d5fae3f46051c862918ac23a1a55
BLAKE2b-256 cc9641e387fcb23267efabd7b583ff679159fdb8bb15f3495486c3e8c013edcc

See more details on using hashes here.

File details

Details for the file minify_html-0.10.4-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for minify_html-0.10.4-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1f81c5d4ed29e5d0b4da1bcec7126494beb00bbc0768774e24c758e7212be655
MD5 47f3dd79a9309476e9c926da5794fb1e
BLAKE2b-256 53acc116ca5d43a6c85b82243dfd34e6638cd9348545fca4f259d6d183c8c11c

See more details on using hashes here.

File details

Details for the file minify_html-0.10.4-cp311-cp311-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.10.4-cp311-cp311-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 c52526eb571f580073577fd0abddbdc9a905658a94f051931000f72577a6ece4
MD5 022ac5a7420b4958c47a92347145fc3d
BLAKE2b-256 5ad77fe072b5ea6dbe51fcc4e1f683e4ad2378d915e02dcd46e3f31efa7112b4

See more details on using hashes here.

File details

Details for the file minify_html-0.10.4-cp311-cp311-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for minify_html-0.10.4-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a329df30a93927fffe7134aeacbbdd83f6cfc9544212e69915a8411346c4a692
MD5 352103fae31f959d86be36f3c54cd2b5
BLAKE2b-256 ef7ebdf82cd48c278c480bdb1102a8181e80bef3f0c6681e179d6c28d43920fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 70ef546b2533abf9a854713afc19e021b5fc8c8d309acc5bb9b4a25c82edeb55
MD5 3ad6717ae6881fab5204161e6bbc0682
BLAKE2b-256 f57c844720b39d8a39cefff1ddcb4543aef06e00d9840b3809cf16fd86088039

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6c213a1dd437610c427731e8c5201b7005a63bcf78d5a80a283a8161bea4ca62
MD5 070bcbf79958ed6b9d81cb111e592971
BLAKE2b-256 a7f6355de71c08b30efb4a2a6ff2783ef6dd8b0014f9898b1864f50dd64ae4e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp310-cp310-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 e5461015005b24ab22d7663d6bd24753dae19c455a59d77aa62d89ed0268373a
MD5 f886c77ef5b8b4dc62a4bad014453d3e
BLAKE2b-256 f1538520b9e69224630a56e6338de3b4397ed6ea4526bb158a711f590411cc0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 9631bc77e143b91e05627a3bfd28e416505e9996729b591c7f2b5f874f9c360c
MD5 82d1869b75d71a6b4e0e594ce555fba4
BLAKE2b-256 85f5207e9c81f17899bbea90da0507133515c489f44928e56fbd279a89cac589

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for minify_html-0.10.4-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 13070b25a4703ee55dc3373179fbf8cbe2a77a1f3c75748271953d7a1ed68a4d
MD5 245f19bc7c8abeb282d0de5e5df9901d
BLAKE2b-256 c3e88172d309e8acc1fb5021c24f4e5f9c5446ecfd7208c056ec8d66ce312837

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 286fc70873f73914f3e7ef8aabef9a5cd0e39e295f313033b786461fae2d0a33
MD5 913a95d226b689678f4e723acbc129ae
BLAKE2b-256 c438d435f74232b6dd28b310b64a902dc1e48ddb6a2b777c2cc5dd5104c0510f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp39-cp39-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 0d016449b5434de7f7717f2a10b4a365f3f73a9d4d088f12823e0180c9899bd8
MD5 1436e35bc32ccb4d0440889ccee6536c
BLAKE2b-256 0f0936e5930b7fad43d19245bc15eb3e0a3a42bfd834e9a5e0a534398e10bcd1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 4177059c91c2d93654f67c720f5b442c3cc27d8f554e95a1f9305a9de7050826
MD5 2501565e3386e42579773c4e8a3d0729
BLAKE2b-256 a9ec1446a3cf1f39816f88f90fafa74ba65ead53d3c95c220be9c8757c6a7030

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for minify_html-0.10.4-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 7b6ee1c49a41394060f14a6dfcdaf0e6ad54fc1d7e013a3ad07a474c1378b7b5
MD5 c39bb119507320fc9c76ef045249d019
BLAKE2b-256 a1dff8f450195cd58fc561cc736a8dc56a8ebe977c96d395de46cd7d5c1c5d6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fd8b6874b18e7e88d0d0d4964f01cf5044636565367900ed71e12fef2619623e
MD5 e808606ca96d4ec8e1d888677465a47d
BLAKE2b-256 20258ea6497d8d291248a836250cd1a3385ba14217040111bf3923874946eea0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp38-cp38-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 23faf85981d159ac237b15828cb65813f4cdfa8a54eef7047a991a5cdf02e596
MD5 d4e32fabc46c7f6c47c1d7e690fe3e41
BLAKE2b-256 26eed2e459011b7db006261c4f8af3cbddc69814e281fe9f792ffaaf8abd8ed9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.4-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 52ece24b2c3efcb0162945ef51d00f5637af86644d7e805cf1f6a263476b816d
MD5 076e9de77f38f1f09e3c0e799acf547f
BLAKE2b-256 b146d448c19ff3c17f12003bd1fe9f90c3c6900711ef641a8f68a6c78304bbe2

See more details on using hashes here.

Supported by

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