Module: AssetHat::JS

Defined in:
lib/asset_hat/js.rb,
lib/asset_hat/js/vendors.rb

Overview

Methods for minifying JavaScript.

Defined Under Namespace

Modules: Engines, Vendors

Constant Summary collapse

ENGINES =

A list of supported minification <a href=JS/Engines.html>engine</a> names.

[:weak, :jsmin]
VENDORS =

A list of supported <a href=JS/Vendors.html>3rd-party JavaScript plugin/vendor</a> names.

Vendors::VENDORS

Class Method Summary collapse

Class Method Details

.min_filepath(filepath) ⇒ Object

Returns the expected path for the minified version of a JS asset:

AssetHat::JS.min_filepath('public/javascripts/bundles/application.js')
  # => 'public/javascripts/bundles/application.min.js'


19
20
21
# File 'lib/asset_hat/js.rb', line 19

def self.min_filepath(filepath)
  AssetHat.min_filepath(filepath, 'js')
end

.minify(input_string, options = {}) ⇒ Object

Accepts a string of JS, and returns that JS minified. Options:

engine

Default is :jsmin; see <a href=JS/Engines.html#method-c-jsmin>Engines.jsmin</a>. Allowed values are in ENGINES.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asset_hat/js.rb', line 28

def self.minify(input_string, options={})
  options.reverse_merge!(:engine => :jsmin)

  engine = options[:engine].to_sym
  unless ENGINES.include?(engine)
    raise %{
      Unknown JS minification engine '#{engine}'.
      Allowed: #{ENGINES.map{ |e| "'#{e}'" }.join(', ')}
    }.strip.gsub(/\s+/, ' ') and return
  end

  AssetHat::JS::Engines.send(engine, input_string).strip
end