# 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) [![Coverage Status](https://coveralls.io/repos/github/tdewolff/minify/badge.svg?branch=master)](https://coveralls.io/github/tdewolff/minify?branch=master) [![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) ***BE AWARE: YOU NEED GO 1.9.7+, 1.10.3+, 1.11 to run the latest release!!!*** If you cannot upgrade Go, please pin to **minify@v2.3.6** and **parse@v2.3.4** --- **[Online demo](https://go.tacodewolff.nl/minify) if you need to minify files *now*.** **[Command line tool](https://github.com/tdewolff/minify/tree/master/cmd/minify) that minifies concurrently and supports watching file changes.** **[All releases](https://github.com/tdewolff/minify/releases) for various platforms.** --- *Did you know that the shortest valid piece of HTML5 is `x`? See for yourself at the [W3C Validator](http://validator.w3.org/)!* 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. 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, ...). #### Table of Contents - [Minify](#minify) - [Prologue](#prologue) - [Installation](#installation) - [API stability](#api-stability) - [Testing](#testing) - [Performance](#performance) - [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) - [To reader](#to-reader) - [To writer](#to-writer) - [Middleware](#middleware) - [Custom minifier](#custom-minifier) - [Mediatypes](#mediatypes) - [Examples](#examples) - [Common minifiers](#common-minifiers) - [Custom minifier](#custom-minifier-example) - [ResponseWriter](#responsewriter) - [Templates](#templates) - [License](#license) ### Status * CSS: **fully implemented** * HTML: **fully implemented** * JS: improved JSmin implementation * JSON: **fully implemented** * SVG: partially implemented; in development * XML: **fully implemented** ### Roadmap - [ ] Use ASM/SSE to further speed-up core parts of the parsers/minifiers - [ ] 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](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 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 Run the following command go get -u github.com/tdewolff/minify/v2 or add the following imports and run the project with `go get` ``` go 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" ) ``` ## 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](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. 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](https://github.com/tdewolff/minify/issues)! ## 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.9.2. ``` name time/op CSS/sample_bootstrap.css-4 2.26ms ± 0% CSS/sample_gumby.css-4 2.92ms ± 1% HTML/sample_amazon.html-4 2.33ms ± 2% HTML/sample_bbc.html-4 1.02ms ± 1% HTML/sample_blogpost.html-4 171µs ± 2% HTML/sample_es6.html-4 14.5ms ± 0% HTML/sample_stackoverflow.html-4 2.41ms ± 1% HTML/sample_wikipedia.html-4 4.76ms ± 0% JS/sample_ace.js-4 7.41ms ± 0% JS/sample_dot.js-4 63.7µs ± 0% JS/sample_jquery.js-4 2.99ms ± 0% JS/sample_jqueryui.js-4 5.92ms ± 2% JS/sample_moment.js-4 1.09ms ± 1% JSON/sample_large.json-4 2.95ms ± 0% JSON/sample_testsuite.json-4 1.51ms ± 1% JSON/sample_twitter.json-4 6.75µs ± 1% SVG/sample_arctic.svg-4 62.3ms ± 1% SVG/sample_gopher.svg-4 218µs ± 0% SVG/sample_usa.svg-4 33.1ms ± 3% XML/sample_books.xml-4 36.2µs ± 0% XML/sample_catalog.xml-4 14.9µs ± 0% XML/sample_omg.xml-4 6.31ms ± 1% name speed CSS/sample_bootstrap.css-4 60.8MB/s ± 0% CSS/sample_gumby.css-4 63.9MB/s ± 1% HTML/sample_amazon.html-4 203MB/s ± 2% HTML/sample_bbc.html-4 113MB/s ± 1% HTML/sample_blogpost.html-4 123MB/s ± 2% HTML/sample_es6.html-4 70.7MB/s ± 0% HTML/sample_stackoverflow.html-4 85.2MB/s ± 1% HTML/sample_wikipedia.html-4 93.6MB/s ± 0% JS/sample_ace.js-4 86.9MB/s ± 0% JS/sample_dot.js-4 81.0MB/s ± 0% JS/sample_jquery.js-4 82.8MB/s ± 0% JS/sample_jqueryui.js-4 79.3MB/s ± 2% JS/sample_moment.js-4 91.2MB/s ± 1% JSON/sample_large.json-4 258MB/s ± 0% JSON/sample_testsuite.json-4 457MB/s ± 1% JSON/sample_twitter.json-4 226MB/s ± 1% SVG/sample_arctic.svg-4 23.6MB/s ± 1% SVG/sample_gopher.svg-4 26.7MB/s ± 0% SVG/sample_usa.svg-4 30.9MB/s ± 3% XML/sample_books.xml-4 122MB/s ± 0% XML/sample_catalog.xml-4 130MB/s ± 0% XML/sample_omg.xml-4 180MB/s ± 1% ``` ## 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: - `KeepConditionalComments` preserve all IE conditional comments such as `` and ``, see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax - `KeepDefaultAttrVals` preserve default attribute values such as `