Module: AssetHat::JS::Engines

Defined in:
lib/asset_hat/js.rb

Overview

Swappable JavaScript minification engines.

Class Method Summary collapse

Class Method Details

.jsmin(input_string) ⇒ Object

JavaScript minification engine that simply uses the JSMin gem, a Ruby port of Crockford’s JSMin.

Sources:



79
80
81
# File 'lib/asset_hat/js.rb', line 79

def self.jsmin(input_string)
  JSMin.minify(input_string + "\n")
end

.weak(input_string) ⇒ Object

Barebones JavaScript minification engine that:

  • Skips leading/trailing whitespace for each line, excluding line breaks; and

  • Removes one-line comments that had no actual code on that line.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/asset_hat/js.rb', line 48

def self.weak(input_string)
  input   = StringIO.new(input_string)
  output  = StringIO.new

  input.each do |line|
    # Remove indentation and trailing whitespace
    line.strip!
    next if line.blank?

    # Skip single-line comments
    next if !(line =~ /^\/\//).nil?
    # TODO: Also skip single-line comments that began mid-line, but not
    #       inside a string or regex

    # TODO: Skip multi-line comments
    # - Should not strip from within a string or regex
    # - Should not strip comments that begin with `/*!` (e.g., licenses)

    output.write(line + "\n")
  end

  output.rewind
  output.read
end