Skip to content

Commit

Permalink
Make compressors lazily load.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 7, 2011
1 parent 86390c3 commit 2f6e389
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions 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
6 changes: 4 additions & 2 deletions 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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions railties/test/application/assets_test.rb
Expand Up @@ -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();"
Expand Down

0 comments on commit 2f6e389

Please sign in to comment.