# Minify [![Build Status](https://travis-ci.org/tdewolff/minify.svg?branch=master)](https://travis-ci.org/tdewolff/minify) [![GoDoc](http://godoc.org/github.com/tdewolff/minify?status.svg)](http://godoc.org/github.com/tdewolff/minify) [![Join the chat at https://gitter.im/tdewolff/minify](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tdewolff/minify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) **I will be away for 5 months, starting May. v2 should be the preferred stable release to use. Master has some new changes for SVG that haven't yet endured the test of time, bug reports are appreciated.** Minify is a minifier package written in [Go][1]. It has build-in HTML5, CSS3, JS, JSON, SVG and XML minifiers and provides an interface to implement any minifier. Minification is the process of removing bytes from a file (such as whitespace) without changing its output and therefore speeding up transmission over the internet. The implemented minifiers are high performance and streaming (which implies O(n)). It associates minification functions with mimetypes, allowing embedded resources (like CSS or JS in HTML files) to be minified too. The user can add any mime-based implementation. Users can also implement a mimetype using an external command (like the ClosureCompiler, UglifyCSS, ...). It is possible to pass parameters through the mediatype to specify the charset for example. [Demo](http://go.tacodewolff.nl/) · [CLI](https://github.com/tdewolff/minify/tree/master/cmd/minify) · [Download](https://dl.equinox.io/tdewolff/minify/stable) #### Table of Contents - [Minify](#minify---) - [Prologue](#prologue) - [Installation](#installation) - [API stability](#api-stability) - [Testing](#testing) - [HTML](#html--) - [Whitespace removal](#whitespace-removal) - [CSS](#css--) - [JS](#js--) - [JSON](#json--) - [SVG](#svg--) - [XML](#xml--) - [Usage](#usage) - [New](#new) - [From reader](#from-reader) - [From bytes](#from-bytes) - [From string](#from-string) - [Custom minifier](#custom-minifier) - [Mediatypes](#mediatypes) - [Examples](#examples) - [Common minifiers](#common-minifiers) - [Custom minifier](#custom-minifier-1) - [ResponseWriter](#responsewriter) - [Templates](#templates) - [License](#license) #### Status * CSS: **fully implemented** * HTML: **fully implemented** * JS: basic JSmin-like implementation * JSON: **fully implemented** * SVG: partially implemented; in development * XML: **fully implemented** ## 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](http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)). Some implementations are much more profound, such as the [YUI Compressor](http://yui.github.io/yuicompressor/) and [Google Closure Compiler](https://github.com/google/closure-compiler) for JS. As most existing implementations either use Java or JavaScript and don't focus on performance, they are pretty slow. And loading the whole file into memory is bad for really large files (or impossible for infinite streams). This minifier proves to be that fast and extensive minifier that can handle HTML and any other filetype it may contain (CSS, JS, ...). It streams the input and output and can minify files concurrently. ## Installation Run the following command go get github.com/tdewolff/minify or add the following imports and run the project with `go get` ``` go import ( "github.com/tdewolff/minify" "github.com/tdewolff/minify/css" "github.com/tdewolff/minify/html" "github.com/tdewolff/minify/js" "github.com/tdewolff/minify/json" "github.com/tdewolff/minify/svg" "github.com/tdewolff/minify/xml" ) ``` ## 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. - minify-v1.0.0 depends on parse-v1.0.0 - minify-v1.1.0 depends on parse-v1.1.0 - minify-v2.0.0 depends on parse-v2.0.0 - minify-tip will always compile with my other packages on tip The API differences between v1 and v2 are listed below. If `m := minify.New()` and `w` and `r` are your writer and reader respectfully, then **v1** → **v2**: - `minify.Bytes(m, ...)` → `m.Bytes(...)` - `minify.String(m, ...)` → `m.String(...)` - `html.Minify(m, "text/html", w, r)` → `html.Minify(m, w, r, nil)` also for `css`, `js`, ... - `css.Minify(m, "text/css;inline=1", w, r)` → `css.Minify(m, w, r, map[string]string{"inline":"1"})` ## Testing For all subpackages and the imported `parse` and `buffer` packages, test coverage of 100% is pursued. Besides full coverage, the minifiers are [fuzz tested](https://github.com/tdewolff/fuzz) using [github.com/dvyukov/go-fuzz](http://www.github.com/dvyukov/go-fuzz), see [the wiki](https://github.com/tdewolff/minify/wiki) for the most important bugs found by fuzz testing. Furthermore am I working on adding visual testing to ensure that minification doesn't change anything visually. By using the WebKit browser to render the original and minified pages we can check whether any pixel is different. These tests ensure that everything works as intended, the code does not crash (whatever the input) and that it doesn't change the final result visually. If you still encounter a bug, please report [here](https://github.com/tdewolff/minify/issues)! ## HTML HTML (with JS and CSS) minification typically runs at about 40MB/s ~= 140GB/h, depending on the composition of the file. Website | Original | Minified | Ratio | Time* ------- | -------- | -------- | ----- | ----------------------- [Amazon](http://www.amazon.com/) | 463kB | **414kB** | 90% | 11ms [BBC](http://www.bbc.com/) | 113kB | **96kB** | 85% | 3ms [StackOverflow](http://stackoverflow.com/) | 201kB | **182kB** | 91% | 5ms [Wikipedia](http://en.wikipedia.org/wiki/President_of_the_United_States) | 435kB | **410kB** | 94%** | 10ms *These times are measured on my home computer which is an average development computer. The duration varies a lot but it's important to see it's in the 10ms range! The benchmark uses all the minifiers and excludes reading from and writing to the file from the measurement. **Is already somewhat minified, so this doesn't reflect the full potential of this minifier. 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 comments (except conditional comments) - shorten `doctype` and `meta` charset - lowercase tags, attributes and some values to enhance gzip compression Options: - `KeepDefaultAttrVals` do not remove default attribute value such as `