README.md (33148B)
1 # Minify <a name="minify"></a> [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/minify/v2?tab=doc) [![Go Report Card](https://goreportcard.com/badge/github.com/tdewolff/minify)](https://goreportcard.com/report/github.com/tdewolff/minify) [![codecov](https://codecov.io/gh/tdewolff/minify/branch/master/graph/badge.svg?token=Cr7r2EKPj2)](https://codecov.io/gh/tdewolff/minify) [![Donate](https://img.shields.io/badge/patreon-donate-DFB317)](https://www.patreon.com/tdewolff) 2 3 **[Online demo](https://go.tacodewolff.nl/minify)** if you need to minify files *now*. 4 5 **[Binaries](https://github.com/tdewolff/minify/releases) of CLI for various platforms.** See [CLI](https://github.com/tdewolff/minify/tree/master/cmd/minify) for more installation instructions. 6 7 **[Python bindings](https://pypi.org/project/tdewolff-minify/)** install with `pip install tdewolff-minify` 8 9 **[JavaScript bindings](https://www.npmjs.com/package/@tdewolff/minify)** install with `npm i @tdewolff/minify` 10 11 **[.NET bindings](https://github.com/JKamsker/NMinify)** install with `Install-Package NMinify` or `dotnet add package NMinify`, thanks to Jonas Kamsker for the port 12 13 --- 14 15 *Did you know that the shortest valid piece of HTML5 is `<!doctype html><title>x</title>`? See for yourself at the [W3C Validator](http://validator.w3.org/)!* 16 17 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. 18 19 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, ...). 20 21 ### Sponsors 22 23 [![SiteGround](https://www.siteground.com/img/downloads/siteground-logo-black-transparent-vector.svg)](https://www.siteground.com/) 24 25 Please see https://www.patreon.com/tdewolff for ways to contribute, otherwise please contact me directly! 26 27 #### Table of Contents 28 29 - [Minify](#minify) 30 - [Prologue](#prologue) 31 - [Installation](#installation) 32 - [API stability](#api-stability) 33 - [Testing](#testing) 34 - [Performance](#performance) 35 - [HTML](#html) 36 - [Whitespace removal](#whitespace-removal) 37 - [CSS](#css) 38 - [JS](#js) 39 - [Comparison with other tools](#comparison-with-other-tools) 40 - [Compression ratio (lower is better)](#compression-ratio-lower-is-better) 41 - [Time (lower is better)](#time-lower-is-better) 42 - [JSON](#json) 43 - [SVG](#svg) 44 - [XML](#xml) 45 - [Usage](#usage) 46 - [New](#new) 47 - [From reader](#from-reader) 48 - [From bytes](#from-bytes) 49 - [From string](#from-string) 50 - [To reader](#to-reader) 51 - [To writer](#to-writer) 52 - [Middleware](#middleware) 53 - [Custom minifier](#custom-minifier) 54 - [Mediatypes](#mediatypes) 55 - [Examples](#examples) 56 - [Common minifiers](#common-minifiers) 57 - [External minifiers](#external-minifiers) 58 - [Closure Compiler](#closure-compiler) 59 - [UglifyJS](#uglifyjs) 60 - [esbuild](#esbuild) 61 - [Custom minifier](#custom-minifier-example) 62 - [ResponseWriter](#responsewriter) 63 - [Templates](#templates) 64 - [FAQ](#faq) 65 - [License](#license) 66 67 ### Roadmap 68 69 - [ ] Use ASM/SSE to further speed-up core parts of the parsers/minifiers 70 - [x] Improve JS minifiers by shortening variables and proper semicolon omission 71 - [ ] Speed-up SVG minifier, it is very slow 72 - [x] Proper parser error reporting and line number + column information 73 - [ ] Generation of source maps (uncertain, might slow down parsers too much if it cannot run separately nicely) 74 - [ ] 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. 75 76 ## Prologue 77 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. 78 79 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. 80 81 ## Installation 82 Make sure you have [Git](https://git-scm.com/) and [Go](https://golang.org/dl/) (1.13 or higher) installed, run 83 ``` 84 mkdir Project 85 cd Project 86 go mod init 87 go get -u github.com/tdewolff/minify/v2 88 ``` 89 90 Then add the following imports to be able to use the various minifiers 91 ``` go 92 import ( 93 "github.com/tdewolff/minify/v2" 94 "github.com/tdewolff/minify/v2/css" 95 "github.com/tdewolff/minify/v2/html" 96 "github.com/tdewolff/minify/v2/js" 97 "github.com/tdewolff/minify/v2/json" 98 "github.com/tdewolff/minify/v2/svg" 99 "github.com/tdewolff/minify/v2/xml" 100 ) 101 ``` 102 103 You can optionally run `go mod tidy` to clean up the `go.mod` and `go.sum` files. 104 105 See [CLI tool](https://github.com/tdewolff/minify/tree/master/cmd/minify) for installation instructions of the binary. 106 107 ### Docker 108 109 If you want to use Docker, please see https://hub.docker.com/r/tdewolff/minify. 110 111 ```bash 112 $ docker run -it tdewolff/minify --help 113 ``` 114 115 ## API stability 116 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. 117 118 ## Testing 119 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)! 120 121 ## Performance 122 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. 123 124 ``` 125 name time/op 126 CSS/sample_bootstrap.css-4 2.70ms ± 0% 127 CSS/sample_gumby.css-4 3.57ms ± 0% 128 CSS/sample_fontawesome.css-4 767µs ± 0% 129 CSS/sample_normalize.css-4 85.5µs ± 0% 130 HTML/sample_amazon.html-4 15.2ms ± 0% 131 HTML/sample_bbc.html-4 3.90ms ± 0% 132 HTML/sample_blogpost.html-4 420µs ± 0% 133 HTML/sample_es6.html-4 15.6ms ± 0% 134 HTML/sample_stackoverflow.html-4 3.73ms ± 0% 135 HTML/sample_wikipedia.html-4 6.60ms ± 0% 136 JS/sample_ace.js-4 28.7ms ± 0% 137 JS/sample_dot.js-4 357µs ± 0% 138 JS/sample_jquery.js-4 10.0ms ± 0% 139 JS/sample_jqueryui.js-4 20.4ms ± 0% 140 JS/sample_moment.js-4 3.47ms ± 0% 141 JSON/sample_large.json-4 3.25ms ± 0% 142 JSON/sample_testsuite.json-4 1.74ms ± 0% 143 JSON/sample_twitter.json-4 24.2µs ± 0% 144 SVG/sample_arctic.svg-4 34.7ms ± 0% 145 SVG/sample_gopher.svg-4 307µs ± 0% 146 SVG/sample_usa.svg-4 57.4ms ± 0% 147 SVG/sample_car.svg-4 18.0ms ± 0% 148 SVG/sample_tiger.svg-4 5.61ms ± 0% 149 XML/sample_books.xml-4 54.7µs ± 0% 150 XML/sample_catalog.xml-4 33.0µs ± 0% 151 XML/sample_omg.xml-4 7.17ms ± 0% 152 153 name speed 154 CSS/sample_bootstrap.css-4 50.7MB/s ± 0% 155 CSS/sample_gumby.css-4 52.1MB/s ± 0% 156 CSS/sample_fontawesome.css-4 61.2MB/s ± 0% 157 CSS/sample_normalize.css-4 70.8MB/s ± 0% 158 HTML/sample_amazon.html-4 31.1MB/s ± 0% 159 HTML/sample_bbc.html-4 29.5MB/s ± 0% 160 HTML/sample_blogpost.html-4 49.8MB/s ± 0% 161 HTML/sample_es6.html-4 65.6MB/s ± 0% 162 HTML/sample_stackoverflow.html-4 55.0MB/s ± 0% 163 HTML/sample_wikipedia.html-4 67.5MB/s ± 0% 164 JS/sample_ace.js-4 22.4MB/s ± 0% 165 JS/sample_dot.js-4 14.5MB/s ± 0% 166 JS/sample_jquery.js-4 24.8MB/s ± 0% 167 JS/sample_jqueryui.js-4 23.0MB/s ± 0% 168 JS/sample_moment.js-4 28.6MB/s ± 0% 169 JSON/sample_large.json-4 234MB/s ± 0% 170 JSON/sample_testsuite.json-4 394MB/s ± 0% 171 JSON/sample_twitter.json-4 63.0MB/s ± 0% 172 SVG/sample_arctic.svg-4 42.4MB/s ± 0% 173 SVG/sample_gopher.svg-4 19.0MB/s ± 0% 174 SVG/sample_usa.svg-4 17.8MB/s ± 0% 175 SVG/sample_car.svg-4 29.3MB/s ± 0% 176 SVG/sample_tiger.svg-4 12.2MB/s ± 0% 177 XML/sample_books.xml-4 81.0MB/s ± 0% 178 XML/sample_catalog.xml-4 58.6MB/s ± 0% 179 XML/sample_omg.xml-4 159MB/s ± 0% 180 ``` 181 182 ## HTML 183 184 HTML (with JS and CSS) minification typically shaves off about 10%. 185 186 The HTML5 minifier uses these minifications: 187 188 - strip unnecessary whitespace and otherwise collapse it to one space (or newline if it originally contained a newline) 189 - strip superfluous quotes, or uses single/double quotes whichever requires fewer escapes 190 - strip default attribute values and attribute boolean values 191 - strip some empty attributes 192 - strip unrequired tags (`html`, `head`, `body`, ...) 193 - strip unrequired end tags (`tr`, `td`, `li`, ... and often `p`) 194 - strip default protocols (`http:`, `https:` and `javascript:`) 195 - strip all comments (including conditional comments, old IE versions are not supported anymore by Microsoft) 196 - shorten `doctype` and `meta` charset 197 - lowercase tags, attributes and some values to enhance gzip compression 198 199 Options: 200 201 - `KeepConditionalComments` preserve all IE conditional comments such as `<!--[if IE 6]><![endif]-->` and `<![if IE 6]><![endif]>`, see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax 202 - `KeepDefaultAttrVals` preserve default attribute values such as `<script type="application/javascript">` 203 - `KeepDocumentTags` preserve `html`, `head` and `body` tags 204 - `KeepEndTags` preserve all end tags 205 - `KeepQuotes` preserve quotes around attribute values 206 - `KeepWhitespace` preserve whitespace between inline tags but still collapse multiple whitespace characters into one 207 208 After recent benchmarking and profiling it became really fast and minifies pages in the 10ms range, making it viable for on-the-fly minification. 209 210 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! 211 212 ### Whitespace removal 213 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. 214 215 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! 216 217 ## CSS 218 219 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. 220 221 The CSS minifier will only use safe minifications: 222 223 - remove comments and unnecessary whitespace (but keep `/*! ... */` which usually contains the license) 224 - remove trailing semicolons 225 - optimize `margin`, `padding` and `border-width` number of sides 226 - shorten numbers by removing unnecessary `+` and zeros and rewriting with/without exponent 227 - remove dimension and percentage for zero values 228 - remove quotes for URLs 229 - remove quotes for font families and make lowercase 230 - rewrite hex colors to/from color names, or to three digit hex 231 - rewrite `rgb(`, `rgba(`, `hsl(` and `hsla(` colors to hex or name 232 - use four digit hex for alpha values (`transparent` → `#0000`) 233 - replace `normal` and `bold` by numbers for `font-weight` and `font` 234 - replace `none` → `0` for `border`, `background` and `outline` 235 - lowercase all identifiers except classes, IDs and URLs to enhance gzip compression 236 - shorten MS alpha function 237 - rewrite data URIs with base64 or ASCII whichever is shorter 238 - calls minifier for data URI mediatypes, thus you can compress embedded SVG files if you have that minifier attached 239 - shorten aggregate declarations such as `background` and `font` 240 241 It does purposely not use the following techniques: 242 243 - (partially) merge rulesets 244 - (partially) split rulesets 245 - collapse multiple declarations when main declaration is defined within a ruleset (don't put `font-weight` within an already existing `font`, too complex) 246 - remove overwritten properties in ruleset (this not always overwrites it, for example with `!important`) 247 - rewrite properties into one ruleset if possible (like `margin-top`, `margin-right`, `margin-bottom` and `margin-left` → `margin`) 248 - put nested ID selector at the front (`body > div#elem p` → `#elem p`) 249 - rewrite attribute selectors for IDs and classes (`div[id=a]` → `div#a`) 250 - put space after pseudo-selectors (IE6 is old, move on!) 251 252 There are a couple of comparison tables online, such as [CSS Minifier Comparison](http://www.codenothing.com/benchmarks/css-compressor-3.0/full.html), [CSS minifiers comparison](http://www.phpied.com/css-minifiers-comparison/) and [CleanCSS tests](http://goalsmashers.github.io/css-minification-benchmark/). Comparing speed between each, this minifier will usually be between 10x-300x faster than existing implementations, and even rank among the top for minification ratios. It falls short with the purposely not implemented and often unsafe techniques. 253 254 Options: 255 256 - `KeepCSS2` prohibits using CSS3 syntax (such as exponents in numbers, or `rgba(` → `rgb(`), might be incomplete 257 - `Precision` number of significant digits to preserve for numbers, `0` means no trimming 258 259 ## JS 260 261 The JS minifier typically shaves off about 35% -- 65% of filesize depening on the file, which is a compression close to many other minifiers. Common speeds of PHP and JS implementations are about 100-300kB/s (see [Uglify2](http://lisperator.net/uglifyjs/), [Adventures in PHP web asset minimization](https://www.happyassassin.net/2014/12/29/adventures-in-php-web-asset-minimization/)). This implementation is orders of magnitude faster at around ~25MB/s. 262 263 The following features are implemented: 264 265 - remove superfluous whitespace 266 - remove superfluous semicolons 267 - shorten `true`, `false`, and `undefined` to `!0`, `!1` and `void 0` 268 - rename variables and functions to shorter names (not in global scope) 269 - move `var` declarations to the top of the global/function scope (if more than one) 270 - collapse if/else statements to expressions 271 - minify conditional expressions to simpler ones 272 - merge sequential expression statements to one, including into `return` and `throw` 273 - remove superfluous grouping in expressions 274 - shorten or remove string escapes 275 - convert object key or index expression from string to identifier or decimal 276 - merge concatenated strings 277 - rewrite numbers (binary, octal, decimal, hexadecimal) to shorter representations 278 279 Options: 280 281 - `KeepVarNames` keeps variable names as they are and omits shortening variable names 282 - `Precision` number of significant digits to preserve for numbers, `0` means no trimming 283 - `Version` ECMAScript version to use for output, `0` is the latest 284 285 ### Comparison with other tools 286 287 Performance is measured with `time [command]` ran 10 times and selecting the fastest one, on a Thinkpad T460 (i5-6300U quad-core 2.4GHz running Arch Linux) using Go 1.15. 288 289 - [minify](https://github.com/tdewolff/minify): `minify -o script.min.js script.js` 290 - [esbuild](https://github.com/evanw/esbuild): `esbuild --minify --outfile=script.min.js script.js` 291 - [terser](https://github.com/terser/terser): `terser script.js --compress --mangle -o script.min.js` 292 - [UglifyJS](https://github.com/Skalman/UglifyJS-online): `uglifyjs --compress --mangle -o script.min.js script.js` 293 - [Closure Compiler](https://github.com/google/closure-compiler): `closure-compiler -O SIMPLE --js script.js --js_output_file script.min.js --language_in ECMASCRIPT_NEXT -W QUIET --jscomp_off=checkVars` optimization level `SIMPLE` instead of `ADVANCED` to make similar assumptions as do the other tools (do not rename/assume anything of global level variables) 294 295 #### Compression ratio (lower is better) 296 All tools give very similar results, although UglifyJS compresses slightly better. 297 298 | Tool | ace.js | dot.js | jquery.js | jqueryui.js | moment.js | 299 | --- | --- | --- | --- | --- | --- | 300 | **minify** | 53.7% | 64.8% | 34.2% | 51.3% | 34.8% | 301 | esbuild | 53.8% | 66.3% | 34.4% | 53.1% | 34.8% | 302 | terser | 53.2% | 65.2% | 34.2% | 51.8% | 34.7% | 303 | UglifyJS | 53.1% | 64.7% | 33.8% | 50.7% | 34.2% | 304 | Closure Compiler | 53.4% | 64.0% | 35.7% | 53.6% | 34.3% | 305 306 #### Time (lower is better) 307 Most tools are extremely slow, with `minify` and `esbuild` being orders of magnitudes faster. 308 309 | Tool | ace.js | dot.js | jquery.js | jqueryui.js | moment.js | 310 | --- | --- | --- | --- | --- | --- | 311 | **minify** | 49ms | 5ms | 22ms | 35ms | 13ms | 312 | esbuild | 64ms | 9ms | 31ms | 51ms | 17ms | 313 | terser | 2900s | 180ms | 1400ms | 2200ms | 730ms | 314 | UglifyJS | 3900ms | 210ms | 2000ms | 3100ms | 910ms | 315 | Closure Compiler | 6100ms | 2500ms | 4400ms | 5300ms | 3500ms | 316 317 ## JSON 318 319 Minification typically shaves off about 15% of filesize for common indented JSON such as generated by [JSON Generator](http://www.json-generator.com/). 320 321 The JSON minifier only removes whitespace, which is the only thing that can be left out, and minifies numbers (`1000` => `1e3`). 322 323 Options: 324 325 - `Precision` number of significant digits to preserve for numbers, `0` means no trimming 326 - `KeepNumbers` do not minify numbers if set to `true`, by default numbers will be minified 327 328 ## SVG 329 330 The SVG minifier uses these minifications: 331 332 - trim and collapse whitespace between all tags 333 - strip comments, empty `doctype`, XML prelude, `metadata` 334 - strip SVG version 335 - strip CDATA sections wherever possible 336 - collapse tags with no content to a void tag 337 - minify style tag and attributes with the CSS minifier 338 - minify colors 339 - shorten lengths and numbers and remove default `px` unit 340 - shorten `path` data 341 - use relative or absolute positions in path data whichever is shorter 342 343 TODO: 344 - convert attributes to style attribute whenever shorter 345 - merge path data? (same style and no intersection -- the latter is difficult) 346 347 Options: 348 349 - `Precision` number of significant digits to preserve for numbers, `0` means no trimming 350 351 ## XML 352 353 The XML minifier uses these minifications: 354 355 - strip unnecessary whitespace and otherwise collapse it to one space (or newline if it originally contained a newline) 356 - strip comments 357 - collapse tags with no content to a void tag 358 - strip CDATA sections wherever possible 359 360 Options: 361 362 - `KeepWhitespace` preserve whitespace between inline tags but still collapse multiple whitespace characters into one 363 364 ## Usage 365 Any input stream is being buffered by the minification functions. This is how the underlying buffer package inherently works to ensure high performance. The output stream however is not buffered. It is wise to preallocate a buffer as big as the input to which the output is written, or otherwise use `bufio` to buffer to a streaming writer. 366 367 ### New 368 Retrieve a minifier struct which holds a map of mediatype → minifier functions. 369 ``` go 370 m := minify.New() 371 ``` 372 373 The following loads all provided minifiers. 374 ``` go 375 m := minify.New() 376 m.AddFunc("text/css", css.Minify) 377 m.AddFunc("text/html", html.Minify) 378 m.AddFunc("image/svg+xml", svg.Minify) 379 m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) 380 m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify) 381 m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify) 382 ``` 383 384 You can set options to several minifiers. 385 ``` go 386 m.Add("text/html", &html.Minifier{ 387 KeepDefaultAttrVals: true, 388 KeepWhitespace: true, 389 }) 390 ``` 391 392 ### From reader 393 Minify from an `io.Reader` to an `io.Writer` for a specific mediatype. 394 ``` go 395 if err := m.Minify(mediatype, w, r); err != nil { 396 panic(err) 397 } 398 ``` 399 400 ### From bytes 401 Minify from and to a `[]byte` for a specific mediatype. 402 ``` go 403 b, err = m.Bytes(mediatype, b) 404 if err != nil { 405 panic(err) 406 } 407 ``` 408 409 ### From string 410 Minify from and to a `string` for a specific mediatype. 411 ``` go 412 s, err = m.String(mediatype, s) 413 if err != nil { 414 panic(err) 415 } 416 ``` 417 418 ### To reader 419 Get a minifying reader for a specific mediatype. 420 ``` go 421 mr := m.Reader(mediatype, r) 422 if _, err := mr.Read(b); err != nil { 423 panic(err) 424 } 425 ``` 426 427 ### To writer 428 Get a minifying writer for a specific mediatype. Must be explicitly closed because it uses an `io.Pipe` underneath. 429 ``` go 430 mw := m.Writer(mediatype, w) 431 if mw.Write([]byte("input")); err != nil { 432 panic(err) 433 } 434 if err := mw.Close(); err != nil { 435 panic(err) 436 } 437 ``` 438 439 ### Middleware 440 Minify resources on the fly using middleware. It passes a wrapped response writer to the handler that removes the Content-Length header. The minifier is chosen based on the Content-Type header or, if the header is empty, by the request URI file extension. This is on-the-fly processing, you should preferably cache the results though! 441 ``` go 442 fs := http.FileServer(http.Dir("www/")) 443 http.Handle("/", m.Middleware(fs)) 444 ``` 445 446 ### Custom minifier 447 Add a minifier for a specific mimetype. 448 ``` go 449 type CustomMinifier struct { 450 KeepLineBreaks bool 451 } 452 453 func (c *CustomMinifier) Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error { 454 // ... 455 return nil 456 } 457 458 m.Add(mimetype, &CustomMinifier{KeepLineBreaks: true}) 459 // or 460 m.AddRegexp(regexp.MustCompile("/x-custom$"), &CustomMinifier{KeepLineBreaks: true}) 461 ``` 462 463 Add a minify function for a specific mimetype. 464 ``` go 465 m.AddFunc(mimetype, func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error { 466 // ... 467 return nil 468 }) 469 m.AddFuncRegexp(regexp.MustCompile("/x-custom$"), func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error { 470 // ... 471 return nil 472 }) 473 ``` 474 475 Add a command `cmd` with arguments `args` for a specific mimetype. 476 ``` go 477 m.AddCmd(mimetype, exec.Command(cmd, args...)) 478 m.AddCmdRegexp(regexp.MustCompile("/x-custom$"), exec.Command(cmd, args...)) 479 ``` 480 481 ### Mediatypes 482 Using the `params map[string]string` argument one can pass parameters to the minifier such as seen in mediatypes (`type/subtype; key1=val2; key2=val2`). Examples are the encoding or charset of the data. Calling `Minify` will split the mimetype and parameters for the minifiers for you, but `MinifyMimetype` can be used if you already have them split up. 483 484 Minifiers can also be added using a regular expression. For example a minifier with `image/.*` will match any image mime. 485 486 ## Examples 487 ### Common minifiers 488 Basic example that minifies from stdin to stdout and loads the default HTML, CSS and JS minifiers. Optionally, one can enable `java -jar build/compiler.jar` to run for JS (for example the [ClosureCompiler](https://code.google.com/p/closure-compiler/)). Note that reading the file into a buffer first and writing to a pre-allocated buffer would be faster (but would disable streaming). 489 ``` go 490 package main 491 492 import ( 493 "log" 494 "os" 495 "os/exec" 496 497 "github.com/tdewolff/minify/v2" 498 "github.com/tdewolff/minify/v2/css" 499 "github.com/tdewolff/minify/v2/html" 500 "github.com/tdewolff/minify/v2/js" 501 "github.com/tdewolff/minify/v2/json" 502 "github.com/tdewolff/minify/v2/svg" 503 "github.com/tdewolff/minify/v2/xml" 504 ) 505 506 func main() { 507 m := minify.New() 508 m.AddFunc("text/css", css.Minify) 509 m.AddFunc("text/html", html.Minify) 510 m.AddFunc("image/svg+xml", svg.Minify) 511 m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) 512 m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify) 513 m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify) 514 515 if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil { 516 panic(err) 517 } 518 } 519 ``` 520 521 ### External minifiers 522 Below are some examples of using common external minifiers. 523 524 #### Closure Compiler 525 See [Closure Compiler Application](https://developers.google.com/closure/compiler/docs/gettingstarted_app). Not tested. 526 527 ``` go 528 m.AddCmdRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), 529 exec.Command("java", "-jar", "build/compiler.jar")) 530 ``` 531 532 ### UglifyJS 533 See [UglifyJS](https://github.com/mishoo/UglifyJS2). 534 535 ``` go 536 m.AddCmdRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), 537 exec.Command("uglifyjs")) 538 ``` 539 540 ### esbuild 541 See [esbuild](https://github.com/evanw/esbuild). 542 543 ``` go 544 m.AddCmdRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), 545 exec.Command("esbuild", "$in.js", "--minify", "--outfile=$out.js")) 546 ``` 547 548 ### <a name="custom-minifier-example"></a> Custom minifier 549 Custom minifier showing an example that implements the minifier function interface. Within a custom minifier, it is possible to call any minifier function (through `m minify.Minifier`) recursively when dealing with embedded resources. 550 ``` go 551 package main 552 553 import ( 554 "bufio" 555 "fmt" 556 "io" 557 "log" 558 "strings" 559 560 "github.com/tdewolff/minify/v2" 561 ) 562 563 func main() { 564 m := minify.New() 565 m.AddFunc("text/plain", func(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error { 566 // remove newlines and spaces 567 rb := bufio.NewReader(r) 568 for { 569 line, err := rb.ReadString('\n') 570 if err != nil && err != io.EOF { 571 return err 572 } 573 if _, errws := io.WriteString(w, strings.Replace(line, " ", "", -1)); errws != nil { 574 return errws 575 } 576 if err == io.EOF { 577 break 578 } 579 } 580 return nil 581 }) 582 583 in := "Because my coffee was too cold, I heated it in the microwave." 584 out, err := m.String("text/plain", in) 585 if err != nil { 586 panic(err) 587 } 588 fmt.Println(out) 589 // Output: Becausemycoffeewastoocold,Iheateditinthemicrowave. 590 } 591 ``` 592 593 ### ResponseWriter 594 #### Middleware 595 ``` go 596 func main() { 597 m := minify.New() 598 m.AddFunc("text/css", css.Minify) 599 m.AddFunc("text/html", html.Minify) 600 m.AddFunc("image/svg+xml", svg.Minify) 601 m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) 602 m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify) 603 m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify) 604 605 fs := http.FileServer(http.Dir("www/")) 606 http.Handle("/", m.MiddlewareWithError(fs)) 607 } 608 609 func handleError(w http.ResponseWriter, r *http.Request, err error) { 610 http.Error(w, err.Error(), http.StatusInternalServerError) 611 } 612 ``` 613 614 In order to properly handle minify errors, it is necessary to close the response writer since all writes are concurrently handled. There is no need to check errors on writes since they will be returned on closing. 615 616 ```go 617 func main() { 618 m := minify.New() 619 m.AddFunc("text/html", html.Minify) 620 m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) 621 622 input := `<script>const i = 1_000_</script>` // Faulty JS 623 req := httptest.NewRequest(http.MethodGet, "/", nil) 624 rec := httptest.NewRecorder() 625 m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 626 w.Header().Set("Content-Type", "text/html") 627 _, _ = w.Write([]byte(input)) 628 629 if err = w.(io.Closer).Close(); err != nil { 630 panic(err) 631 } 632 })).ServeHTTP(rec, req) 633 } 634 ``` 635 636 #### ResponseWriter 637 ``` go 638 func Serve(w http.ResponseWriter, r *http.Request) { 639 mw := m.ResponseWriter(w, r) 640 defer mw.Close() 641 w = mw 642 643 http.ServeFile(w, r, path.Join("www", r.URL.Path)) 644 } 645 ``` 646 647 #### Custom response writer 648 ResponseWriter example which returns a ResponseWriter that minifies the content and then writes to the original ResponseWriter. Any write after applying this filter will be minified. 649 ``` go 650 type MinifyResponseWriter struct { 651 http.ResponseWriter 652 io.WriteCloser 653 } 654 655 func (m MinifyResponseWriter) Write(b []byte) (int, error) { 656 return m.WriteCloser.Write(b) 657 } 658 659 // MinifyResponseWriter must be closed explicitly by calling site. 660 func MinifyFilter(mediatype string, res http.ResponseWriter) MinifyResponseWriter { 661 m := minify.New() 662 // add minfiers 663 664 mw := m.Writer(mediatype, res) 665 return MinifyResponseWriter{res, mw} 666 } 667 ``` 668 669 ``` go 670 // Usage 671 func(w http.ResponseWriter, req *http.Request) { 672 w = MinifyFilter("text/html", w) 673 if _, err := io.WriteString(w, "<p class="message"> This HTTP response will be minified. </p>"); err != nil { 674 panic(err) 675 } 676 if err := w.Close(); err != nil { 677 panic(err) 678 } 679 // Output: <p class=message>This HTTP response will be minified. 680 } 681 ``` 682 683 ### Templates 684 685 Here's an example of a replacement for `template.ParseFiles` from `template/html`, which automatically minifies each template before parsing it. 686 687 Be aware that minifying templates will work in most cases but not all. Because the HTML minifier only works for valid HTML5, your template must be valid HTML5 of itself. Template tags are parsed as regular text by the minifier. 688 689 ``` go 690 func compileTemplates(filenames ...string) (*template.Template, error) { 691 m := minify.New() 692 m.AddFunc("text/html", html.Minify) 693 694 var tmpl *template.Template 695 for _, filename := range filenames { 696 name := filepath.Base(filename) 697 if tmpl == nil { 698 tmpl = template.New(name) 699 } else { 700 tmpl = tmpl.New(name) 701 } 702 703 b, err := ioutil.ReadFile(filename) 704 if err != nil { 705 return nil, err 706 } 707 708 mb, err := m.Bytes("text/html", b) 709 if err != nil { 710 return nil, err 711 } 712 tmpl.Parse(string(mb)) 713 } 714 return tmpl, nil 715 } 716 ``` 717 718 Example usage: 719 720 ``` go 721 templates := template.Must(compileTemplates("view.html", "home.html")) 722 ``` 723 724 ## FAQ 725 ### Newlines remain in minified output 726 While you might expect the minified output to be on a single line for it to be fully minified, this is not true. In many cases, using a literal newline doesn't affect the file size, and in some cases it may even reduce the file size. 727 728 A typical example is HTML. Whitespace is significant in HTML, meaning that spaces and newlines between or around tags may affect how they are displayed. There is no distinction between a space or a newline and they may be interchanged without affecting the displayed HTML. Remember that a space (0x20) and a newline (0x0A) are both one byte long, so that there is no difference in file size when interchanging them. This minifier removes unnecessary whitespace by replacing stretches of spaces and newlines by a single whitespace character. Specifically, if the stretch of white space characters contains a newline, it will replace it by a newline and otherwise by a space. This doesn't affect the file size, but may help somewhat for debugging or file transmission objectives. 729 730 Another example is JavaScript. Single or double quoted string literals may not contain newline characters but instead need to escape them as `\n`. These are two bytes instead of a single newline byte. Using template literals it is allowed to have literal newline characters and we can use that fact to shave-off one byte! The result is that the minified output contains newlines instead of escaped newline characters, which makes the final file size smaller. Of course, changing from single or double quotes to template literals depends on other factors as well, and this minifier makes a calculation whether the template literal results in a shorter file size or not before converting a string literal. 731 732 ## License 733 Released under the [MIT license](LICENSE.md). 734 735 [1]: http://golang.org/ "Go Language"