Skip to content

Commit 87a0f8f

Browse files
committed
Set compressors after the configure blocks
Sprockets only allow to set compressors to registeres compressors. If any new compressor is registered in the configure block it will will blow because we were setting the compressor before it being registered.
1 parent 01983b7 commit 87a0f8f

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/sprockets/railtie.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ def configure(&block)
7979
config.assets.paths.each { |path| env.append_path(path) }
8080
end
8181

82-
config.assets.configure do |env|
83-
env.js_compressor = config.assets.js_compressor
84-
env.css_compressor = config.assets.css_compressor
85-
end
86-
8782
config.assets.configure do |env|
8883
env.context_class.send :include, ::Sprockets::Rails::Context
8984
env.context_class.assets_prefix = config.assets.prefix
@@ -123,16 +118,23 @@ def build_environment(app, initialized = nil)
123118

124119
env = Sprockets::Environment.new(app.root.to_s)
125120

121+
config = app.config
122+
126123
# Run app.assets.configure blocks
127-
app.config.assets._blocks.each do |block|
124+
config.assets._blocks.each do |block|
128125
block.call(env)
129126
end
130127

128+
# Set compressors after the configure blocks since they can
129+
# define new compressors and we only accept existent compressors.
130+
env.js_compressor = config.assets.js_compressor
131+
env.css_compressor = config.assets.css_compressor
132+
131133
# No more configuration changes at this point.
132134
# With cache classes on, Sprockets won't check the FS when files
133135
# change. Preferable in production when the FS only changes on
134136
# deploys when the app restarts.
135-
if app.config.cache_classes
137+
if config.cache_classes
136138
env = env.cached
137139
end
138140

test/test_railtie.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ def test_compressors
138138
assert_equal Sprockets::SassCompressor.name, env.css_compressor.name
139139
end
140140

141+
def test_custom_compressors
142+
compressor = Class.new
143+
app.configure do
144+
config.assets.configure do |env|
145+
env.register_compressor "application/javascript", :test_js, compressor
146+
env.register_compressor "text/css", :test_css, compressor
147+
end
148+
config.assets.js_compressor = :test_js
149+
config.assets.css_compressor = :test_css
150+
end
151+
app.initialize!
152+
153+
assert env = app.assets
154+
assert_equal compressor, env.js_compressor
155+
assert_equal compressor, env.css_compressor
156+
end
157+
141158
def test_version
142159
app.configure do
143160
config.assets.version = 'v2'

0 commit comments

Comments
 (0)