Skip to content

Commit

Permalink
Added support for including specific versions of jQuery, and structur…
Browse files Browse the repository at this point in the history
…e for versioning other vendor code
  • Loading branch information
rondevera committed Jan 15, 2010
1 parent 4c49cc9 commit 4122c90
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 25 deletions.
20 changes: 16 additions & 4 deletions README
Expand Up @@ -82,10 +82,22 @@ Configuration

### Advanced configuration ###

In `config/assets.yml`, the `engine` settings indicate how CSS and JS are
minified. If the default engines cause problems by minifying too strongly,
try switching each to `weak`. The `weak` engines are much safer, but don't
save as many bytes.
Additional settings are supported in `config/assets.yml`:

* `engine`: Indicates how CSS and JS are minified; omit this setting to use
the defaults. If the default engines cause problems by minifying too
strongly, try switching each to `weak`. The `weak` engines are much safer,
but don't save as many bytes.

* `vendors`: Currently only allows for setting the jQuery version number:

js:
vendors:
jquery:
version: 1.4

In the future, this will be used for configuring the retrieval of other
third-party code.



Expand Down
18 changes: 10 additions & 8 deletions app/helpers/asset_hat_helper.rb
Expand Up @@ -36,7 +36,7 @@ def include_assets(type, *args)
if use_caching
sources += bundles.map { |b| "bundles/#{b}.min.#{type}" }
else
config = AssetHat::config
config = AssetHat.config
filenames = bundles.map { |b| AssetHat::bundle_filenames(b, type) }.
flatten.reject(&:blank?)
end
Expand Down Expand Up @@ -131,9 +131,11 @@ def include_js(*args)
#
# Include jQuery:
# include_js :jquery # Development/test environment
# => <script src="/javascripts/jquery-1.3.2.min.js" ...></script>
# => <script src="/javascripts/jquery-VERSION.min.js" ...></script>
# include_js :jquery # Staging/production environment
# => <script src="http://ajax.googleapis.com/.../jquery.min.js" ...></script>
# # Set jQuery versions either in `config/assets.yml`, or by using
# # `include_js :jquery, :version => '1.4'`.
#
# Include a bundle of JS files (i.e., a concatenated set of files;
# configure in config/assets.yml):
Expand All @@ -153,16 +155,16 @@ def include_js(*args)

return if args.blank?
html = []
options = args.extract_options!

if args.include?(:jquery)
args.delete :jquery
source = (Rails.env.development? || Rails.env.test?) ?
'jquery-1.3.2.min.js' :
'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'
included_vendors = (args & AssetHat::JS::VENDORS)
included_vendors.each do |vendor|
args.delete vendor
source = AssetHat::JS::Vendors.source_for(vendor, options.slice(:version))
html << include_assets(:js, source, :cache => true)
end

html << include_assets(:js, *args)
html << include_assets(:js, *(args + [options]))
html.join("\n").strip
end

Expand Down
3 changes: 3 additions & 0 deletions config/assets.yml
Expand Up @@ -13,6 +13,9 @@
# - clearfix
# js:
# engine: jsmin
# vendors:
# jquery:
# version: 1.4
# bundles:
# plugins:
# - init
Expand Down
28 changes: 27 additions & 1 deletion lib/asset_hat/js.rb
Expand Up @@ -5,6 +5,8 @@
module AssetHat
module JS
ENGINES = [:weak, :jsmin]
VENDORS = [:jquery]
# TODO: Support jQuery UI, Prototype, MooTools, etc.

def self.min_filepath(filepath)
AssetHat::min_filepath(filepath, 'js')
Expand Down Expand Up @@ -53,7 +55,31 @@ def self.weak(input_string)
def self.jsmin(input_string)
JSMin.minify(input_string)
end
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 = (Rails.env.development? || Rails.env.test?) ?
"jquery-#{version}.min.js" :
"http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js"
end
src
end
end # module Vendors

end

Expand Down
16 changes: 8 additions & 8 deletions lib/asset_hat/tasks.rb
Expand Up @@ -106,7 +106,7 @@

args.with_defaults :verbose => false
min_options = {
:engine => AssetHat::config['css']['engine']
:engine => AssetHat.config['css']['engine']
}.reject { |k,v| v.blank? }

input = File.open(args.filepath, 'r').read
Expand All @@ -126,11 +126,11 @@
raise 'Usage: rake asset_hat:css:minify_bundle[application]' and return
end

config = AssetHat::config
config = AssetHat.config
old_bundle_size = 0.0
new_bundle_size = 0.0
min_options = {
:engine => AssetHat::config['css']['engine']
:engine => config['css']['engine']
}.reject { |k,v| v.blank? }

# Get bundle filenames
Expand Down Expand Up @@ -178,7 +178,7 @@
desc 'Concatenates and minifies all CSS bundles'
task :minify do
# Get input bundles
config = AssetHat::config
config = AssetHat.config
bundles = config['css']['bundles'].keys

# Minify bundles
Expand All @@ -199,7 +199,7 @@

args.with_defaults :verbose => false
min_options = {
:engine => AssetHat::config['js']['engine']
:engine => AssetHat.config['js']['engine']
}.reject { |k,v| v.blank? }

if args.verbose && args.filepath.match(/\.min\.js$/)
Expand All @@ -224,11 +224,11 @@
raise 'Usage: rake asset_hat:js:minify_bundle[application]' and return
end

config = AssetHat::config
config = AssetHat.config
old_bundle_size = 0.0
new_bundle_size = 0.0
min_options = {
:engine => AssetHat::config['js']['engine']
:engine => config['js']['engine']
}.reject { |k,v| v.blank? }

# Get bundle filenames
Expand Down Expand Up @@ -271,7 +271,7 @@
desc 'Concatenates and minifies all JS bundles'
task :minify do
# Get input bundles
config = AssetHat::config
config = AssetHat.config
bundles = config['js']['bundles'].keys

# Minify bundles
Expand Down
2 changes: 1 addition & 1 deletion rails/init.rb
Expand Up @@ -2,7 +2,7 @@

# Precalculate (and memoize) asset commit IDs
AssetHat::TYPES.each do |type|
AssetHat::config[type.to_s]['bundles'].keys.each do |bundle|
AssetHat.config[type.to_s]['bundles'].keys.each do |bundle|
# Memoize commit ID for this bundle
AssetHat::last_bundle_commit_id(bundle, type) if AssetHat::cache?

Expand Down
15 changes: 12 additions & 3 deletions test/asset_hat_helper_test.rb
Expand Up @@ -71,7 +71,7 @@ class AssetHatHelperTest < ActionView::TestCase
context 'with real bundle files' do
setup do
@asset_id = ENV['RAILS_ASSET_ID'] = '222'
@config = AssetHat::config
@config = AssetHat.config
end
teardown { ENV['RAILS_ASSET_ID'] = nil }

Expand Down Expand Up @@ -125,8 +125,17 @@ class AssetHatHelperTest < ActionView::TestCase
end

should 'include jQuery' do
version = AssetHat::JS::Vendors::JQUERY_DEFAULT_VERSION
output = include_js(:jquery, :cache => true)
assert_equal js_tag("jquery-1.3.2.min.js?#{@commit_id}"), output
assert_equal(
js_tag("jquery-#{version}.min.js?#{@commit_id}"), output)
end

should 'include jQuery by version' do
version = '1.3.2'
output = include_js(:jquery, :version => version, :cache => true)
assert_equal(
js_tag("jquery-#{version}.min.js?#{@commit_id}"), output)
end

should 'include multiple files by name' do
Expand Down Expand Up @@ -187,7 +196,7 @@ class AssetHatHelperTest < ActionView::TestCase
context 'with real bundle files' do
setup do
@asset_id = ENV['RAILS_ASSET_ID'] = '222'
@config = AssetHat::config
@config = AssetHat.config
end
teardown { ENV['RAILS_ASSET_ID'] = nil }

Expand Down

0 comments on commit 4122c90

Please sign in to comment.