New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

squoosh

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

squoosh

  • 0.4.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Actions
Status Coverage
Status

Squoosh

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

CSS minification is handled by Sassc 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 Sassc 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;
  • spaces in MathML elements; nor
  • spaces in SVG elements.

HTML 4 and XHTML documents (more precisely, any document that does not have an HTML 5 DOCTYPE, typically <!DOCTYPE html>) is returned unchanged by Squoosh::minify_html.

MathML and SVG elements have their tags minified (including compressing attributes and serializing empty elements as self-closing start tags) and comments inside them are removed. White space is preserved (except for the newline normalization performed by the HTML 5 parser).

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

# frozen_string_literal: true

if Jekyll.env == 'deploy'
  require 'squoosh'

  squoosher = Squoosh::Squoosher.new
  Jekyll::Hooks.register(%i[documents pages],
                         :post_render, priority: :high) do |doc|
    case File.extname(doc.destination('./'))
    when '.html', '.htm'
      doc.output = squoosher.minify_html(doc.output)
    when '.js'
      doc.output = squoosher.minify_js(doc.output)
    end
  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.

FAQs

Package last updated on 15 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc