Skip to content

Commit 8248052

Browse files
committed
Make Rails.application.assets available in initializers
1 parent 5eeae68 commit 8248052

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

actionpack/lib/sprockets/railtie.rb

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ class Railtie < ::Rails::Railtie
1111
load "sprockets/assets.rake"
1212
end
1313

14-
# We need to configure this after initialization to ensure we collect
15-
# paths from all engines. This hook is invoked exactly before routes
16-
# are compiled, and so that other Railties have an opportunity to
17-
# register compressors.
18-
config.after_initialize do |app|
19-
assets = app.config.assets
20-
next unless assets.enabled
14+
initializer "sprockets.environment" do |app|
15+
config = app.config
16+
next unless config.assets.enabled
2117

22-
app.assets = asset_environment(app)
18+
require 'sprockets'
19+
20+
app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
21+
env.static_root = File.join(app.root.join('public'), config.assets.prefix)
22+
env.logger = ::Rails.logger
23+
24+
if config.assets.cache_store != false
25+
env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache
26+
end
27+
end
2328

2429
ActiveSupport.on_load(:action_view) do
2530
include ::Sprockets::Helpers::RailsHelper
@@ -28,53 +33,40 @@ class Railtie < ::Rails::Railtie
2833
include ::Sprockets::Helpers::RailsHelper
2934
end
3035
end
31-
32-
app.routes.prepend do
33-
mount app.assets => assets.prefix
34-
end
35-
36-
if config.action_controller.perform_caching
37-
app.assets = app.assets.index
38-
end
3936
end
4037

41-
protected
42-
def asset_environment(app)
43-
require "sprockets"
44-
45-
assets = app.config.assets
46-
47-
env = Sprockets::Environment.new(app.root.to_s)
38+
# We need to configure this after initialization to ensure we collect
39+
# paths from all engines. This hook is invoked exactly before routes
40+
# are compiled, and so that other Railties have an opportunity to
41+
# register compressors.
42+
config.after_initialize do |app|
43+
next unless app.assets
44+
config = app.config
4845

49-
env.static_root = File.join(app.root.join("public"), assets.prefix)
46+
config.assets.paths.each { |path| app.assets.append_path(path) }
5047

51-
if env.respond_to?(:append_path)
52-
assets.paths.each { |path| env.append_path(path) }
53-
else
54-
env.paths.concat assets.paths
48+
if config.assets.compress
49+
# temporarily hardcode default JS compressor to uglify. Soon, it will work
50+
# the same as SCSS, where a default plugin sets the default.
51+
unless config.assets.js_compressor == false
52+
app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) }
5553
end
5654

57-
env.logger = ::Rails.logger
58-
59-
if env.respond_to?(:cache) && assets.cache_store != false
60-
env.cache = ActiveSupport::Cache.lookup_store(assets.cache_store) || ::Rails.cache
55+
unless config.assets.css_compressor == false
56+
app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) }
6157
end
58+
end
6259

63-
if assets.compress
64-
# temporarily hardcode default JS compressor to uglify. Soon, it will work
65-
# the same as SCSS, where a default plugin sets the default.
66-
unless assets.js_compressor == false
67-
env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) }
68-
end
69-
70-
unless assets.css_compressor == false
71-
env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) }
72-
end
73-
end
60+
app.routes.prepend do
61+
mount app.assets => config.assets.prefix
62+
end
7463

75-
env
64+
if config.action_controller.perform_caching
65+
app.assets = app.assets.index
7666
end
67+
end
7768

69+
protected
7870
def expand_js_compressor(sym)
7971
case sym
8072
when :closure

0 commit comments

Comments
 (0)