From eb367afeed2905d1036f46940aa6c91323f7faab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 24 Sep 2011 01:56:49 +0200 Subject: `rake assets:precompile` loads the application but does not initialize it. To the app developer, this means configuration add in config/initializers/* will not be executed. Plugins developers need to special case their initializers that are meant to be run in the assets group by adding :group => :assets. Conflicts: railties/CHANGELOG railties/test/application/assets_test.rb --- actionpack/lib/sprockets/assets.rake | 3 +- actionpack/lib/sprockets/bootstrap.rb | 65 +++++++++++++++++++++++++++++++++++ actionpack/lib/sprockets/railtie.rb | 59 +++---------------------------- 3 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 actionpack/lib/sprockets/bootstrap.rb (limited to 'actionpack/lib/sprockets') diff --git a/actionpack/lib/sprockets/assets.rake b/actionpack/lib/sprockets/assets.rake index 7241671db0..5dd48fea98 100644 --- a/actionpack/lib/sprockets/assets.rake +++ b/actionpack/lib/sprockets/assets.rake @@ -11,8 +11,9 @@ namespace :assets do ENV["RAILS_ENV"] ||= "production" Kernel.exec $0, *ARGV else - Rake::Task["environment"].invoke Rake::Task["tmp:cache:clear"].invoke + Rails.application.initialize!(:assets) + Sprockets::Bootstrap.new(Rails.application).run # Ensure that action view is loaded and the appropriate sprockets hooks get executed ActionView::Base diff --git a/actionpack/lib/sprockets/bootstrap.rb b/actionpack/lib/sprockets/bootstrap.rb new file mode 100644 index 0000000000..ed1ed09374 --- /dev/null +++ b/actionpack/lib/sprockets/bootstrap.rb @@ -0,0 +1,65 @@ +module Sprockets + class Bootstrap + def initialize(app) + @app = app + end + + # TODO: Get rid of config.assets.enabled + def run + app, config = @app, @app.config + return unless app.assets + + config.assets.paths.each { |path| app.assets.append_path(path) } + + if config.assets.compress + # temporarily hardcode default JS compressor to uglify. Soon, it will work + # the same as SCSS, where a default plugin sets the default. + unless config.assets.js_compressor == false + app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) } + end + + unless config.assets.css_compressor == false + app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) } + end + end + + if config.assets.compile + app.routes.prepend do + mount app.assets => config.assets.prefix + end + end + + if config.assets.digest + app.assets = app.assets.index + end + end + + protected + + def expand_js_compressor(sym) + case sym + when :closure + require 'closure-compiler' + Closure::Compiler.new + when :uglifier + require 'uglifier' + Uglifier.new + when :yui + require 'yui/compressor' + YUI::JavaScriptCompressor.new + else + sym + end + end + + def expand_css_compressor(sym) + case sym + when :yui + require 'yui/compressor' + YUI::CssCompressor.new + else + sym + end + end + end +end \ No newline at end of file diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb index 9b31604dbe..edcd4c1113 100644 --- a/actionpack/lib/sprockets/railtie.rb +++ b/actionpack/lib/sprockets/railtie.rb @@ -1,5 +1,6 @@ module Sprockets - autoload :Helpers, "sprockets/helpers" + autoload :Bootstrap, "sprockets/bootstrap" + autoload :Helpers, "sprockets/helpers" autoload :LazyCompressor, "sprockets/compressors" autoload :NullCompressor, "sprockets/compressors" autoload :StaticCompiler, "sprockets/static_compiler" @@ -12,7 +13,7 @@ module Sprockets load "sprockets/assets.rake" end - initializer "sprockets.environment" do |app| + initializer "sprockets.environment", :group => :assets do |app| config = app.config next unless config.assets.enabled @@ -51,59 +52,7 @@ module Sprockets # are compiled, and so that other Railties have an opportunity to # register compressors. config.after_initialize do |app| - next unless app.assets - config = app.config - - config.assets.paths.each { |path| app.assets.append_path(path) } - - if config.assets.compress - # temporarily hardcode default JS compressor to uglify. Soon, it will work - # the same as SCSS, where a default plugin sets the default. - unless config.assets.js_compressor == false - app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) } - end - - unless config.assets.css_compressor == false - app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) } - end - end - - if config.assets.compile - app.routes.prepend do - mount app.assets => config.assets.prefix - end - end - - if config.assets.digest - app.assets = app.assets.index - end + Sprockets::Bootstrap.new(app).run end - - protected - def expand_js_compressor(sym) - case sym - when :closure - require 'closure-compiler' - Closure::Compiler.new - when :uglifier - require 'uglifier' - Uglifier.new - when :yui - require 'yui/compressor' - YUI::JavaScriptCompressor.new - else - sym - end - end - - def expand_css_compressor(sym) - case sym - when :yui - require 'yui/compressor' - YUI::CssCompressor.new - else - sym - end - end end end -- cgit v1.2.3