diff options
author | José Valim <jose.valim@gmail.com> | 2011-07-06 21:38:54 -0300 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-07-06 21:41:13 -0300 |
commit | 2f6e3895a8ea2a39e43525aba91a6dbca977ef6a (patch) | |
tree | 6ff56dde61784acf20356e2336331ef82578530f | |
parent | 86390c3bf30dd61f3bac047e0887b74ff422c02f (diff) | |
download | rails-2f6e3895a8ea2a39e43525aba91a6dbca977ef6a.tar.gz rails-2f6e3895a8ea2a39e43525aba91a6dbca977ef6a.tar.bz2 rails-2f6e3895a8ea2a39e43525aba91a6dbca977ef6a.zip |
Make compressors lazily load.
-rw-r--r-- | Gemfile | 5 | ||||
-rw-r--r-- | actionpack/lib/sprockets/compressors.rb | 21 | ||||
-rw-r--r-- | actionpack/lib/sprockets/railtie.rb | 6 | ||||
-rw-r--r-- | railties/test/application/assets_test.rb | 11 |
4 files changed, 40 insertions, 3 deletions
@@ -10,7 +10,10 @@ end gem "coffee-script" gem "sass" -gem "uglifier", ">= 1.0.0" + +# This needs to be with require false to avoid +# it being automatically loaded by sprockets +gem "uglifier", ">= 1.0.0", :require => false gem "rake", ">= 0.8.7" gem "mocha", ">= 0.9.8" diff --git a/actionpack/lib/sprockets/compressors.rb b/actionpack/lib/sprockets/compressors.rb new file mode 100644 index 0000000000..6544953df4 --- /dev/null +++ b/actionpack/lib/sprockets/compressors.rb @@ -0,0 +1,21 @@ +module Sprockets + class NullCompressor + def compress(content) + content + end + end + + class LazyCompressor + def initialize(&block) + @block = block + end + + def compressor + @compressor ||= @block.call || NullCompressor.new + end + + def compress(content) + compressor.compress(content) + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb index d1e97c594e..970773c6a4 100644 --- a/actionpack/lib/sprockets/railtie.rb +++ b/actionpack/lib/sprockets/railtie.rb @@ -1,5 +1,7 @@ module Sprockets autoload :Helpers, "sprockets/helpers" + autoload :LazyCompressor, "sprockets/compressors" + autoload :NullCompressor, "sprockets/compressors" # TODO: Get rid of config.assets.enabled class Railtie < ::Rails::Railtie @@ -66,8 +68,8 @@ module Sprockets if assets.compress # temporarily hardcode default JS compressor to uglify. Soon, it will work # the same as SCSS, where a default plugin sets the default. - env.js_compressor = expand_js_compressor(assets.js_compressor || :uglifier) - env.css_compressor = expand_css_compressor(assets.css_compressor) + env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) } + env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) } end env diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index afe93aa732..e1eee71a9e 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -35,6 +35,17 @@ module ApplicationTests assert_match "alert()", last_response.body end + test "assets do not require compressors until it is used" do + app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();" + ENV["RAILS_ENV"] = "production" + require "#{app_path}/config/environment" + + assert !defined?(Uglifier) + get "/assets/demo.js" + assert_match "alert()", last_response.body + assert defined?(Uglifier) + end + test "assets are compiled properly" do app_file "app/assets/javascripts/application.js", "alert();" app_file "app/assets/javascripts/foo/application.js", "alert();" |