lib/asset_hat/js.rb in asset_hat-0.1.5 vs lib/asset_hat/js.rb in asset_hat-0.2.0
- old
+ new
@@ -1,19 +1,32 @@
require 'jsmin'
- # - http://github.com/rgrove/jsmin
- # - http://gemcutter.org/gems/jsmin
+require File.join(File.dirname(__FILE__), 'js', 'vendors')
module AssetHat
+ # Methods for minifying JavaScript.
module JS
+ # A list of supported minification
+ # <a href=JS/Engines.html>engine</a> names.
ENGINES = [:weak, :jsmin]
- VENDORS = [:jquery]
- # TODO: Support jQuery UI, Prototype, MooTools, etc.
+ # A list of supported
+ # <a href=JS/Vendors.html>3rd-party JavaScript plugin/vendor</a> names.
+ VENDORS = Vendors::VENDORS
+
+ # 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'
def self.min_filepath(filepath)
AssetHat.min_filepath(filepath, 'js')
end
+ # Accepts a string of JS, and returns that JS minified. Options:
+ #
+ # [engine] Default is <code>:jsmin</code>; see
+ # <a href=JS/Engines.html#method-c-jsmin>Engines.jsmin</a>.
+ # Allowed values are in ENGINES.
def self.minify(input_string, options={})
options.reverse_merge!(:engine => :jsmin)
engine = options[:engine].to_sym
unless ENGINES.include?(engine)
@@ -24,11 +37,16 @@
end
AssetHat::JS::Engines.send(engine, input_string)
end
+ # Swappable JavaScript minification engines.
module Engines
+ # 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.
def self.weak(input_string)
input = StringIO.new(input_string)
output = StringIO.new
input.each do |line|
@@ -50,38 +68,19 @@
output.rewind
output.read
end
+ # JavaScript minification engine that simply uses the JSMin gem, a Ruby
+ # port of Crockford's JSMin.
+ #
+ # Sources:
+ # - http://github.com/rgrove/jsmin
+ # - http://rubygems.org/gems/jsmin
def self.jsmin(input_string)
JSMin.minify(input_string)
end
end # module Engines
-
- module Vendors
- JQUERY_DEFAULT_VERSION = '1.4'
-
- def self.source_for(vendor, options={})
- version = options[:version]
- if version.blank?
- version = begin
- AssetHat.config['js']['vendors'][vendor.to_s]['version']
- rescue
- AssetHat::JS::Vendors.const_get(
- :"#{vendor.to_s.upcase}_DEFAULT_VERSION")
- end
- end
-
- case vendor
- when :jquery
- src = ActionController::Base.consider_all_requests_local ?
- "jquery-#{version}.min.js" :
- "http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js"
- else nil
- end
- src
- end
- end # module Vendors
end
end