Class: Squoosh::Squoosher

Inherits:
Object
  • Object
show all
Defined in:
lib/squoosh.rb

Overview

Minify HTML, JavaScript, and CSS using a single set of options. Minified versions of the JavaScript and CSS encountered are cached to speed up minification when the same scripts or inline style sheets appear multiple times.

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for minifying.

  • remove_comments Remove all comments that aren’t “loud”

  • omit_tags Omit unnecessary start and end tags

  • loud_comments Keep all comments matching this regex

  • minify_javascript Minify JavaScript <script> and inline JavaScript

  • minify_css Minify CSS <style> and inline CSS

  • uglifier_options Options to pass to Uglifier

  • sass_options Options to pass to Sassc

{
  remove_comments: true,
  omit_tags: true,
  compress_spaces: true,
  loud_comments: /\A\s*!/,
  minify_javascript: true,
  minify_css: true,
  uglifier_options: {
    output: {
      ascii_only: false,
      comments: /\A!/
    }
  },
  sass_options: {
    style: :compressed
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Squoosher

Create a new instance of Squoosher.

Parameters:

  • options (Hash) (defaults to: {})

    options to override the default options



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/squoosh.rb', line 69

def initialize(options = {})
  options.each_key do |key|
    unless DEFAULT_OPTIONS.include?(key)
      raise ArgumentError, "Invalid option `#{key}'"
    end
  end
  @options = DEFAULT_OPTIONS.merge(options)
  @js_cache = {}
  @inline_script_cache = {}
  @css_cache = {}
end

Instance Method Details

#minify_css(content) ⇒ String

Minify CSS using Sassc.

Parameters:

  • content (String)

    the CSS to minify

Returns:

  • (String)

    the minified CSS



103
104
105
106
107
108
# File 'lib/squoosh.rb', line 103

def minify_css(content)
  @css_cache[content] ||= begin
    root = SassC::Engine.new(content, @options[:sass_options])
    root.render.rstrip
  end
end

#minify_html(content) ⇒ String

Minify HTML and inline JavaScript and CSS.

If the content does not start with an HTML document type <!DOCTYPE html>, then content is returned unchanged.

Parameters:

  • content (String)

    the HTML to minify

Returns:

  • (String)

    the minified HTML



88
89
90
91
92
93
94
95
96
97
# File 'lib/squoosh.rb', line 88

def minify_html(content)
  doc = Nokogiri.HTML5(content)
  return content unless doc&.internal_subset&.html5_dtd?

  remove_comments(doc) if @options[:remove_comments]
  compress_javascript(doc) if @options[:minify_javascript]
  compress_css(doc) if @options[:minify_css]
  doc.children.each { |c| compress_spaces(c) } if @options[:compress_spaces]
  doc.children.map { |node| stringify_node(node) }.join
end

#minify_js(content) ⇒ String

Minify JavaScript using Uglify.

Parameters:

  • content (String)

    the JavaScript to minify

Returns:

  • (String)

    the minified JavaScript



114
115
116
# File 'lib/squoosh.rb', line 114

def minify_js(content)
  @js_cache[content] ||= uglify(content, @options[:uglifier_options])
end