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.3"

Use

Check out the docs for API and usage examples.

Deno

Use

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


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.3.tar.gz (136.2 kB view details)

Uploaded Source

Built Distributions

minify_html-0.10.3-cp311-none-win_amd64.whl (552.2 kB view details)

Uploaded CPython 3.11Windows x86-64

minify_html-0.10.3-cp311-cp311-manylinux_2_28_aarch64.whl (632.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

minify_html-0.10.3-cp311-cp311-manylinux_2_27_x86_64.whl (700.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64

minify_html-0.10.3-cp311-cp311-macosx_10_7_x86_64.whl (589.8 kB view details)

Uploaded CPython 3.11macOS 10.7+ x86-64

minify_html-0.10.3-cp310-none-win_amd64.whl (552.2 kB view details)

Uploaded CPython 3.10Windows x86-64

minify_html-0.10.3-cp310-cp310-manylinux_2_28_aarch64.whl (632.8 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

minify_html-0.10.3-cp310-cp310-manylinux_2_27_x86_64.whl (700.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64

minify_html-0.10.3-cp310-cp310-macosx_10_7_x86_64.whl (589.8 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

minify_html-0.10.3-cp39-none-win_amd64.whl (552.2 kB view details)

Uploaded CPython 3.9Windows x86-64

minify_html-0.10.3-cp39-cp39-manylinux_2_28_aarch64.whl (632.8 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

minify_html-0.10.3-cp39-cp39-manylinux_2_27_x86_64.whl (700.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64

minify_html-0.10.3-cp39-cp39-macosx_10_7_x86_64.whl (589.8 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

minify_html-0.10.3-cp38-none-win_amd64.whl (552.2 kB view details)

Uploaded CPython 3.8Windows x86-64

minify_html-0.10.3-cp38-cp38-manylinux_2_28_aarch64.whl (633.0 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ ARM64

minify_html-0.10.3-cp38-cp38-manylinux_2_27_x86_64.whl (701.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.27+ x86-64

minify_html-0.10.3-cp38-cp38-macosx_10_7_x86_64.whl (589.9 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: minify_html-0.10.3.tar.gz
  • Upload date:
  • Size: 136.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for minify_html-0.10.3.tar.gz
Algorithm Hash digest
SHA256 78dcbcad3cee62b346c69ca6fc28f1bb31dedfa2673c6f30154c7935238fdf2b
MD5 9565b10cfdb345f814b4df270f6b228b
BLAKE2b-256 2c50f14f89087128557cb7bd658e9a31ce0e0f78e74a6c3490e119b48a544eb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 d2e12ab82cff60a334d277cabb5d841c5ca0a1858b2edfab726232de5691695e
MD5 5cbbbdcec8f704ae0d7f1ea02dea54e7
BLAKE2b-256 2ffe6ba1084d7d97dcbcc65825bcd55465a8a336538e282651670a0609460f77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9f446d5ccbd858a94f55b9ec02f90c1955bfd895db839384daac2e17e317c497
MD5 e47378eb6eda6729288011bd005feaa7
BLAKE2b-256 bc31d7cc745f11ebd37a4157df3a1117aa2ab66bff063e960173de84367a5db0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp311-cp311-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 8b94b13998d4f91b9f2f31c67ae7d6e6cdd201a7c48a62622a82fb39f21e5d80
MD5 870c2f057809b4662a608e8b770f2649
BLAKE2b-256 b9dabf37e1a10a77265e297e6dfa19b417abc35616a95b317f66021b6b65934a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 7791e2c757b25775b776cbaa253b41d1d4b8c75906414032b25d8cbf9028c581
MD5 3419db5df095551b414e10fc742a98c6
BLAKE2b-256 fd07e425d5ee34dee9d929c95985ce93088e5b0f0e3747284c6e1c8708b1643e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 4173d4bcd722ffff2e4b535603c4bb122e5f859b3d7689282a5697696735ae2e
MD5 e84842888d47f1c7c196b4cbee5d1be9
BLAKE2b-256 086dcba525b6f2525932c784f04f430cfe2ff9dc96bae7a365efdb796d4e6a60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 30d9c427db1aebe5b25559c6dbfa653572e7a2af8fc7703d7c6bdabb41cc20e7
MD5 ff899e8175fca86db93ab6a1d9743a00
BLAKE2b-256 8f90d9ed02e871317ac1e36b5df11707e6c3145afa49d55e58d4f46a393568ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp310-cp310-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 2cced1cdc1acfc11b4e74d4c68c693bf714f19f71133f2e0de001c08a116f93c
MD5 dc8170c1450466932f448e48c6fb5031
BLAKE2b-256 a93c9c3ae8f1e19280659b52b409a9fa641d3cdbbb291edb2e5ea4b1fd2e5d99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a837ad8474be871a069b6cc15c6acc9e679f70c81d627f583dd94638416dec73
MD5 3442db5b8bb849eec26a9d10e4b3b04d
BLAKE2b-256 35b560c12625e38dc4be9cf8c7635ef7017fcc04e98e04d3266c1aa3f4d04e70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: minify_html-0.10.3-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 552.2 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.10.3-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 c537ae4105cc4138f5fb5c4b54c09b2a8bdc9894c97c2eacf7d6a35477f5b27b
MD5 e3265b1ee5a6881822784dfc77ed3e52
BLAKE2b-256 29a8f8d9d811b1fb000a6a254f30e164ea09eb3da3687caae006b91b9ed31c24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 922134f7829b26d3d35e53ba27625351cbb486cda3f19b4bd9d8d8a5b3237f0c
MD5 80f6e650340d64b1e57740e636b13ca3
BLAKE2b-256 21b342cbff46d3a8db3caf62fd04fc41a6e77c82f1c60079a74f93bded654326

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp39-cp39-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 63f095e79cab2774363156065a1e1664100b6665548965a517fb78d23885ec59
MD5 6ad5677d501a7c5c556c35ea3217f045
BLAKE2b-256 e99d0dba1a3d64b5bf743252bfa0b637cda081a399c7b3f09a37194aaf81cc3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 141293cf8c2fdbab556c6c25b129bdd41226baabc31c6de83b61e7aaf676a8d5
MD5 0216285533eb53f275c6a439c4279000
BLAKE2b-256 49b4e732f86d968728906868c9edd48418c07e3fc347a38c99a6ff8a1004a8b5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: minify_html-0.10.3-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 552.2 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.10.3-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 0194f41a544d7f53a4af8fddd0e17529e43405acb1784847688708ab7cc4ed3d
MD5 c8686aafc11a5cbb0ec9713c3a5f6f56
BLAKE2b-256 c982aba41530517a267945de66bd58b6794e4a4d8f7f45c9659bfdc58b23c1dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b1797b3a906c670d4d75eb5db732758e23adac507d6c91a2f53e439ef985348c
MD5 2aa21d3226ec9614f09d7d8b2a269daf
BLAKE2b-256 cb60e201e2487d4b728baa3460492561b0554f4410e9ee86b8d9d44d7f8121e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp38-cp38-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 ce1ce8bff3e8aae489f821d4c1ba62730142729e7f895cda048dbe38f3fd47d6
MD5 7be41474a27ec0b0e3bb96907601930d
BLAKE2b-256 7d3ed84afed8066cbc6619c0060e9db210acf3becfecee5692e57caa689e6886

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minify_html-0.10.3-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 9657fc1c60b8b05a2e01b6a1942d202cf782aaae8299a2eae746734964a26535
MD5 4f4798618cb00fd7c4919376be422966
BLAKE2b-256 7f196156fb528b16f39277ea172475185c7725befebb85c8a8a49d1b74052a82

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