# HTML minifier [](https://greenkeeper.io/) [![Version npm][version]](http://browsenpm.org/package/minimize)[![Build Status][build]](https://travis-ci.org/Swaagie/minimize)[![Dependencies][david]](https://david-dm.org/Swaagie/minimize)[![Coverage Status][cover]](https://coveralls.io/r/Swaagie/minimize?branch=master) [version]: http://img.shields.io/npm/v/minimize.svg?style=flat-square [build]: http://img.shields.io/travis/Swaagie/minimize/master.svg?style=flat-square [david]: https://img.shields.io/david/Swaagie/minimize.svg?style=flat-square [cover]: http://img.shields.io/coveralls/Swaagie/minimize/master.svg?style=flat-square Minimize is a HTML minifier based on the node-htmlparser. This depedency will ensure output is solid and correct. Minimize is focussed on HTML5 and will not support older HTML drafts. It is not worth the effort and the web should move forward. Currently, HTML minifier is only usuable server side. Client side minification will be added in a future release. **Minimize does not parse inline PHP or raw template files. Templates are not valid HTML and this is outside the scope of the minimize. The _output_ of the templaters should be parsed and minified.** ## Features - fast and stable HTML minification (no inline PHP or templates) - highly configurable - CLI interface usable with stdin and files - can distinguish conditional IE comments and/or SSI - build on the foundations of [htmlparser2][fb55] - pluggable interface that allows to hook into each element ## Usage To get the minified content make sure to provide a callback. Optional an options object can be provided. All options are listed below and `false` per default. ```js var Minimize = require('minimize') , content = new Minimize().parse(content); console.log(content); ``` #### Asynchronous usage Simply pass a callback as second argument. This is relevant is plugins perform asynchronous operations. ```js var Minimize = require('minimize') , minimize = new Minimize(); minimize.parse(content, function (error, data) { console.log(data); }); ``` #### Gulp plugin - [gulp-minimize](https://github.com/foru17/gulp-minimize): Gulp plugin based on minimize. #### Options List of available options. Note that all options are set to `false` by default and need to be explicitly enabled by providing `true`. For example `empty: true`. - [empty](#empty) - [cdata](#cdata) - [comments](#comments) - [ssi](#server-side-includes-ssi) - [conditionals](#conditionals) - [spare](#spare) - [quotes](#quotes) - [loose](#loose) - [dom](#dom) - xmlMode - lowerCaseAttributeNames - lowerCaseTags #### Custom parser Supplying a custom instance to do the HTML parsing is possible. I.e. this can be useful if the HTML contains SVG or if you need to specific options to the parser. ```js var Minimize = require('minimize') , html = require('htmlparser2') , minimize = new Minimize( new html.Parser( new html.FeedHandler(minimize.emits('read')), { /* options */ } ) ); minimize.parse(content, function (error, data) { console.log(data); }); ``` ## Options ###### Empty Empty attributes can usually be removed, by default all are removed, excluded HTML5 _data-*_ and microdata attributes. To retain empty elements regardless value, do: ```js var Minimize = require('minimize') , minimize = new Minimize({ empty: true }); minimize.parse( '
', function (error, data) { // data output: } ); ``` ###### CDATA CDATA is only required for HTML to parse as valid XML. For normal webpages this is rarely the case, thus CDATA around javascript can be omitted. By default CDATA is removed, if you would like to keep it, pass true: ```js var Minimize = require('minimize') , minimize = new Minimize({ cdata: true }); minimize.parse( '', function (error, data) { // data output: } ); ``` ###### Comments Comments inside HTML are usually beneficial while developing. Hiding your comments in production is sane, safe and will reduce data transfer. If you ensist on keeping them, fo1r instance to show a nice easter egg, set the option to true. Keeping comments will also retain any Server Side Includes or conditional IE statements. ```js var Minimize = require('minimize') , minimize = new Minimize({ comments: true }); minimize.parse( '\n