# 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) **[Online demo](http://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.** --- 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 high performance and streaming, which implies O(n). 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 - [ ] General speed-up of all minifiers (use ASM for whitespace funcs) - [ ] Improve JS minifiers by shortening variables and proper semicolon omission - [ ] Speed-up SVG minifier, it is very slow - [ ] 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) - [ ] Look into compression of images, fonts and other web resources (into package `compress`?) - [ ] 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. - [ ] Create a package to format files, much like `gofmt` for Go 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 Java or JavaScript and don't focus on performance, they are pretty slow. Additionally, loading the whole file into memory at once is bad for really large files (or impossible for 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. ## 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)! ## 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 3.05ms ± 1% CSS/sample_gumby.css-4 4.25ms ± 1% HTML/sample_amazon.html-4 3.33ms ± 0% HTML/sample_bbc.html-4 1.39ms ± 7% HTML/sample_blogpost.html-4 222µs ± 1% HTML/sample_es6.html-4 18.0ms ± 1% HTML/sample_stackoverflow.html-4 3.08ms ± 1% HTML/sample_wikipedia.html-4 6.06ms ± 1% JS/sample_ace.js-4 9.92ms ± 1% JS/sample_dot.js-4 91.4µs ± 4% JS/sample_jquery.js-4 4.00ms ± 1% JS/sample_jqueryui.js-4 7.93ms ± 0% JS/sample_moment.js-4 1.46ms ± 1% JSON/sample_large.json-4 5.07ms ± 4% JSON/sample_testsuite.json-4 2.96ms ± 0% JSON/sample_twitter.json-4 11.3µs ± 0% SVG/sample_arctic.svg-4 64.7ms ± 0% SVG/sample_gopher.svg-4 227µs ± 0% SVG/sample_usa.svg-4 35.9ms ± 6% XML/sample_books.xml-4 48.1µs ± 4% XML/sample_catalog.xml-4 20.2µs ± 0% XML/sample_omg.xml-4 9.02ms ± 0% name speed CSS/sample_bootstrap.css-4 45.0MB/s ± 1% CSS/sample_gumby.css-4 43.8MB/s ± 1% HTML/sample_amazon.html-4 142MB/s ± 0% HTML/sample_bbc.html-4 83.0MB/s ± 7% HTML/sample_blogpost.html-4 94.5MB/s ± 1% HTML/sample_es6.html-4 56.8MB/s ± 1% HTML/sample_stackoverflow.html-4 66.7MB/s ± 1% HTML/sample_wikipedia.html-4 73.5MB/s ± 1% JS/sample_ace.js-4 64.9MB/s ± 1% JS/sample_dot.js-4 56.4MB/s ± 4% JS/sample_jquery.js-4 61.8MB/s ± 1% JS/sample_jqueryui.js-4 59.2MB/s ± 0% JS/sample_moment.js-4 67.8MB/s ± 1% JSON/sample_large.json-4 150MB/s ± 4% JSON/sample_testsuite.json-4 233MB/s ± 0% JSON/sample_twitter.json-4 134MB/s ± 0% SVG/sample_arctic.svg-4 22.7MB/s ± 0% SVG/sample_gopher.svg-4 25.6MB/s ± 0% SVG/sample_usa.svg-4 28.6MB/s ± 6% XML/sample_books.xml-4 92.1MB/s ± 4% XML/sample_catalog.xml-4 95.6MB/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: - `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 `