Skip to content
Browse files

Make compressors lazily load.

  • Loading branch information...
1 parent 86390c3 commit 2f6e3895a8ea2a39e43525aba91a6dbca977ef6a @josevalim josevalim committed Jul 6, 2011
View
5 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"
View
21 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
View
6 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 @@ def asset_environment(app)
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
View
11 railties/test/application/assets_test.rb
@@ -35,6 +35,17 @@ def app
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();"

0 comments on commit 2f6e389

Please sign in to comment.
Something went wrong with that request. Please try again.