Squoosh

Minifies HTML, JavaScript, and CSS, including inline JavaScript and CSS.

Travis-CI Build
Status

CSS minification is handled by Sass whereas JavaScript minification is handled by Uglifier which requires node.js.

HTML minification is handled as follows. First, an HTML 5 (which really means the WHATWG HTML living standard) parser constructs a DOM as specified by the standard. Next, semantically meaningless inter-element whitespace nodes are removed from the DOM and semantically meaningfull runs of whitespace are compressed to single spaces, except in pre, textarea, and foreign elements. Then, inline JavaScript and CSS are compressed using Sass and Uglifier. Finally, the DOM is serialized, compressing attributes where possible and omitting optional start and end tags where possible.

Unlike some other HTML minifiers, Squoosh uses neither Java nor regular expressions to parse HTML.

Limitations

Squoosh will not minify

  • HTML 4 and earlier;
  • XHTML, any version;
  • MathML elements; nor
  • SVG elements.

Installation

Add this line to your application's Gemfile:

gem 'squoosh'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install squoosh

Usage

You can read the documentation here.

The three basic minification functions are

  • Squoosh::minify_html
  • Squoosh::minify_js
  • Squoosh::minify_css

The Squoosher class caches (in memory) minified JavaScript and CSS which can significantly speed up minifying HTML with repeated scripts and style sheets.

Using with Jekyll

Create a _plugins/squoosh.rb file with the contents

require 'squoosh'

Jekyll::Hooks.register [:documents, :pages], :post_render, priority: :high do |doc|
  case File.extname(doc.destination('./'))
  when '.html', '.htm'
    doc.output = Squoosh::minify_html doc.output
  when '.js'
    doc.output = Squoosh::minify_js doc.output
  end
end

CSS minification could be handled similarly, or foo.css files could simply be renamed to foo.scss and

sass:
  style: compressed

added to _config.yml.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/stevecheckoway/squoosh.

License

The gem is available as open source under the terms of the MIT License.