From 2f6e3895a8ea2a39e43525aba91a6dbca977ef6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 6 Jul 2011 21:38:54 -0300 Subject: Make compressors lazily load. --- Gemfile | 5 ++++- actionpack/lib/sprockets/compressors.rb | 21 +++++++++++++++++++++ actionpack/lib/sprockets/railtie.rb | 6 ++++-- railties/test/application/assets_test.rb | 11 +++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 actionpack/lib/sprockets/compressors.rb diff --git a/Gemfile b/Gemfile index 4b53aeb42f..49c5cfb7ac 100644 --- a/Gemfile +++ b/Gemfile @@ -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();" -- cgit v1.2.3