MCPcopy
hub / github.com/tdewolff/minify

github.com/tdewolff/minify @v2.24.13 sqlite

repository ↗ · DeepWiki ↗ · release v2.24.13 ↗
16,173 symbols 43,080 edges 96 files 2,060 documented · 13%
README

Minify API reference Go Report Card codecov

Online demo if you need to minify files now.

Binaries of CLI for various platforms. See CLI for more installation instructions.

Windows binary from scoop install with scoop install main/minify

Python bindings install with pip install tdewolff-minify

JavaScript bindings install with npm i @tdewolff/minify

.NET bindings install with Install-Package NMinify or dotnet add package NMinify, thanks to Jonas Kamsker for the port


Did you know that the shortest valid piece of HTML5 is <!doctype html><title>x</title>? See for yourself at the W3C Validator!

Minify is a minifier package written in [Go][1]. It provides HTML5, CSS3, JS, JSON, SVG and XML minifiers and an interface to implement any other minifier. Minification is the process of removing bytes from a file (such as whitespace) without changing its output and therefore shrinking its size and speeding up transmission over the internet and possibly parsing. The implemented minifiers are designed for high performance (see https://github.com/privatenumber/minification-benchmarks where this library is (one of) the fastest JS minifiers).

The core functionality associates mimetypes with minification functions, allowing embedded resources (like CSS or JS within HTML files) to be minified as well. Users can add new implementations that are triggered based on a mimetype (or pattern), or redirect to an external command (like ClosureCompiler, UglifyCSS, ...).

Sponsors

SiteGround

SiteGround

Thank you SiteGround for having sponsored this project for many years! Their contribution is invaluable for code maintenance and improvements. If you are in need of professional web hosting, I can highly recommend their products.

Requesting sponsors

I'm actively looking for support in the form of donations or sponsorships to keep developing this library and highly appreciate any gesture. Please see the Sponsors button in GitHub for ways to contribute, or contact me directly.

Table of Contents

Roadmap

  • [ ] Use ASM/SSE to further speed-up core parts of the parsers/minifiers
  • [x] Improve JS minifiers by shortening variables and proper semicolon omission
  • [ ] Speed-up SVG minifier, it is very slow
  • [x] Proper parser error reporting and line number + column information
  • [ ] Generation of source maps (uncertain, might slow down parsers too much if it cannot run separately nicely)
  • [ ] Create a cmd to pack webfiles (much like webpack), ie. merging CSS and JS files, inlining small external files, minification and gzipping. This would work on HTML files.

Prologue

Minifiers or bindings to minifiers exist in almost all programming languages. Some implementations are merely using several regular expressions to trim whitespace and comments (even though regex for parsing HTML/XML is ill-advised, for a good read see Regular Expressions: Now You Have Two Problems). Some implementations are much more profound, such as the YUI Compressor and Google Closure Compiler for JS. As most existing implementations either use JavaScript, use regexes, and don't focus on performance, they are pretty slow.

This minifier proves to be that fast and extensive minifier that can handle HTML and any other filetype it may contain (CSS, JS, ...). It is usually orders of magnitude faster than existing minifiers.

Installation

Make sure you have Git and Go (1.18 or higher) installed, run

mkdir Project
cd Project
go mod init
go get -u github.com/tdewolff/minify/v2

Then add the following imports to be able to use the various minifiers

import (
    "github.com/tdewolff/minify/v2"
    "github.com/tdewolff/minify/v2/css"
    "github.com/tdewolff/minify/v2/html"
    "github.com/tdewolff/minify/v2/js"
    "github.com/tdewolff/minify/v2/json"
    "github.com/tdewolff/minify/v2/svg"
    "github.com/tdewolff/minify/v2/xml"
)

You can optionally run go mod tidy to clean up the go.mod and go.sum files.

See CLI tool for installation instructions of the binary.

Docker

If you want to use Docker, please see https://hub.docker.com/r/tdewolff/minify.

$ docker run -it tdewolff/minify --help

API stability

There is no guarantee for absolute stability, but I take issues and bugs seriously and don't take API changes lightly. The library will be maintained in a compatible way unless vital bugs prevent me from doing so. There has been one API change after v1 which added options support and I took the opportunity to push through some more API clean up as well. There are no plans whatsoever for future API changes.

Testing

For all subpackages and the imported parse package, test coverage of 100% is pursued. Besides full coverage, the minifiers are fuzz tested using github.com/dvyukov/go-fuzz, see the wiki for the most important bugs found by fuzz testing. These tests ensure that everything works as intended and that the code does not crash (whatever the input). If you still encounter a bug, please file a bug report!

Performance

The benchmarks directory contains a number of standardized samples used to compare performance between changes. To give an indication of the speed of this library, I've ran the tests on my Thinkpad T460 (i5-6300U quad-core 2.4GHz running Arch Linux) using Go 1.15.

name                              time/op
CSS/sample_bootstrap.css-4          2.70ms ± 0%
CSS/sample_gumby.css-4              3.57ms ± 0%
CSS/sample_fontawesome.css-4         767µs ± 0%
CSS/sample_normalize.css-4          85.5µs ± 0%
HTML/sample_amazon.html-4           15.2ms ± 0%
HTML/sample_bbc.html-4              3.90ms ± 0%
HTML/sample_blogpost.html-4          420µs ± 0%
HTML/sample_es6.html-4              15.6ms ± 0%
HTML/sample_stackoverflow.html-4    3.73ms ± 0%
HTML/sample_wikipedia.html-4        6.60ms ± 0%
JS/sample_ace.js-4                  28.7ms ± 0%
JS/sample_dot.js-4                   357µs ± 0%
JS/sample_jquery.js-4               10.0ms ± 0%
JS/sample_jqueryui.js-4             20.4ms ± 0%
JS/sample_moment.js-4               3.47ms ± 0%
JSON/sample_large.json-4            3.25ms ± 0%
JSON/sample_testsuite.json-4        1.74ms ± 0%
JSON/sample_twitter.json-4          24.2µs ± 0%
SVG/sample_arctic.svg-4             34.7ms ± 0%
SVG/sample_gopher.svg-4              307µs ± 0%
SVG/sample_usa.svg-4                57.4ms ± 0%
SVG/sample_car.svg-4                18.0ms ± 0%
SVG/sample_tiger.svg-4              5.61ms ± 0%
XML/sample_books.xml-4              54.7µs ± 0%
XML/sample_catalog.xml-4            33.0µs ± 0%
XML/sample_omg.xml-4                7.17ms ± 0%

name                              speed
CSS/sample_bootstrap.css-4        50.7MB/s ± 0%
CSS/sample_gumby.css-4            52.1MB/s ± 0%
CSS/sample_fontawesome.css-4      61.2MB/s ± 0%
CSS/sample_normalize.css-4        70.8MB/s ± 0%
HTML/sample_amazon.html-4         31.1MB/s ± 0%
HTML/sample_bbc.html-4            29.5MB/s ± 0%
HTML/sample_blogpost.html-4       49.8MB/s ± 0%
HTML/sample_es6.html-4            65.6MB/s ± 0%
HTML/sample_stackoverflow.html-4  55.0MB/s ± 0%
HTML/sample_wikipedia.html-4      67.5MB/s ± 0%
JS/sample_ace.js-4                22.4MB/s ± 0%
JS/sample_dot.js-4                14.5MB/s ± 0%
JS/sample_jquery.js-4             24.8MB/s ± 0%
JS/sample_jqueryui.js-4           23.0MB/s ± 0%
JS/sample_moment.js-4             28.6MB/s ± 0%
JSON/sample_large.json-4           234MB/s ± 0%
JSON/sample_testsuite.json-4       394MB/s ± 0%
JSON/sample_twitter.json-4        63.0MB/s ± 0%
SVG/sample_arctic.svg-4           42.4MB/s ± 0%
SVG/sample_gopher.svg-4           19.0MB/s ± 0%
SVG/sample_usa.svg-4              17.8MB/s ± 0%
SVG/sample_car.svg-4              29.3MB/s ± 0%
SVG/sample_tiger.svg-4            12.2MB/s ± 0%
XML/sample_books.xml-4            81.0MB/s ± 0%
XML/sample_catalog.xml-4          58.6MB/s ± 0%
XML/sample_omg.xml-4               159MB/s ± 0%

HTML

HTML (with JS and CSS) minification typically shaves off about 10%.

The HTML5 minifier uses these minifications:

  • strip unnecessary whitespace and otherwise collapse it to one space (or newline if it originally contained a newline)
  • strip superfluous quotes, or uses single/double quotes whichever requires fewer escapes
  • strip default attribute values and attribute boolean values
  • strip some empty attributes
  • strip unrequired tags (html, head, body, ...)
  • strip unrequired end tags (tr, td, li, ... and often p)
  • strip default protocols (http:, https: and javascript:)
  • strip all comments (including conditional comments, old IE versions are not supported anymore by Microsoft)
  • shorten doctype and meta charset
  • lowercase tags, attributes and some values to enhance gzip compression

Options:

  • KeepSpecialComments preserve all special comments, including Server Side Includes such as and IE conditional comments such as and <![if IE 6]><![endif]>, see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax
  • KeepDefaultAttrVals preserve default attribute values such as <script type="application/javascript">
  • KeepDocumentTags preserve html, head and body tags
  • KeepEndTags preserve all end tags
  • KeepQuotes preserve quotes around attribute values
  • KeepWhitespace preserve whitespace between inline tags but still collapse multiple whitespace characters into one
  • TemplateDelims preserve context within and surrounding the given opening and closing delimiters

After recent benchmarking and profiling it became really fast and minifies pages in the 10ms range, making it viable for on-the-fly minification.

However, be careful when doing on-the-fly minification. Minification typically trims off 10% and does this at worst around about 20MB/s. This means users have to download slower than 2MB/s to make on-the-fly minification worthwhile. This may or may not apply in your situation. Rather use caching!

Whitespace removal

The whitespace removal mechanism collapses all sequences of whitespace (spaces, newlines, tabs) to a single space. If the sequence contained a newline or carriage return it will collapse into a newline character instead. It trims all text parts (in between tags) depending on whether it was preceded by a space from a previous piece of text and whether it is followed up by a block element or an inline element. In the former case we can omit spaces while for inline elements whitespace has significance.

Make sure your HTML doesn't depend on whitespace between block elements that have been changed to inline or inline-block elements using CSS. Your layout should not depend on those whitespaces as the minifier will remove them. An example is a menu consisting of multiple <li> that have display:inline-block applied and have whitespace in between them. It is bad practise to rely on whitespace for element positioning anyways!

CSS

Minification typically shaves off about 10%-15%. This CSS minifier will not do structural changes to your stylesheets. Although this could result in smaller files, the complexity is quite high and the risk of breaking website is high too.

The CSS minifier will only use safe minifications:

  • remove comments and unnecessary whitespace (but keep /*! ... */ which usually contains the license)
  • remove trailing semicolons
  • optimize margin, padding and border-width number of sides
  • shorten numbers by removing unnecessary + and zeros and re

Extension points exported contracts — how you extend this code

Minifier (Interface)
Minifier is the interface for minifiers. The *M parameter is used for minifying embedded resources, such as JS within HT [9 …
minify.go
MinifyConfig (Interface)
(no doc)
bindings/js/src/types.ts
MinifierFunc (FuncType)
TODO: DEPRECATED, remove MinifierFunc is a function that implements Minifer.
minify.go
MinifyOptions (Interface)
(no doc)
bindings/js/src/types.ts

Core symbols most depended-on inside this repo

diag
called by 1811
_benchmarks/sample_typescript.js
len
called by 532
_benchmarks/sample_echarts.js
error
called by 447
_benchmarks/sample_typescript.js
token
called by 441
_benchmarks/sample_typescript.js
each
called by 407
_benchmarks/sample_echarts.js
isNaN
called by 297
_benchmarks/sample_victory.js
write
called by 252
js/js.go
visitNode
called by 234
_benchmarks/sample_typescript.js

Shape

Function 16,006
Method 122
Struct 32
TypeAlias 7
Interface 3
Class 2
FuncType 1

Languages

TypeScript98%
Go2%
Python1%

Modules by API surface

_benchmarks/sample_typescript.js9,061 symbols
_benchmarks/sample_antd.js2,418 symbols
_benchmarks/sample_echarts.js2,166 symbols
_benchmarks/sample_victory.js1,043 symbols
_benchmarks/sample_moment.js270 symbols
_benchmarks/sample_ace.js162 symbols
tests/js/corpus/jquery.dataTables.js152 symbols
tests/js/corpus/ace.js149 symbols
tests/js/corpus/jquery.js81 symbols
_benchmarks/sample_jquery.js81 symbols
tests/js/corpus/moment.js75 symbols
tests/js/corpus/jquery-ui.js40 symbols

Dependencies from manifests, versioned

github.com/djherbis/atimev1.1.0 · 1×
github.com/fsnotify/fsnotifyv1.9.0 · 1×
github.com/tdewolff/argpv0.0.0-2026042407420 · 1×
github.com/tdewolff/parse/v2v2.8.12 · 1×
github.com/tdewolff/testv1.0.12 · 1×
golang.org/x/sysv0.43.0 · 1×
gopkg.in/yaml.v3v3.0.1 · 1×
@tdewolff/minifylatest · 1×
@types/node25.6.0 · 1×

For agents

$ claude mcp add minify \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact