Skip to content

Commit 67c74ec

Browse files
authored
Add assets.resolve_assets_in_css_urls configuration option to allow disabling AssetUrlProcessor (#489)
* Allow users to opt out of `Sprockets::Rails::AssetUrlProcessor` We're introducing a new configuration option `assets.rewrite_css_urls` to allow users to opt out of the new `AssetUrlProcessor` (by configuring that option to `false`). * Rename `rewrite_css_urls` option to `resolve_assets_in_css_urls` * Reword comment describing `AssetUrlProcessor` to use language that mirrors the new `resolve_assets_in_css_urls` configuration option * Document the new `resolve_assets_in_css_urls` configuration option
1 parent 26d6583 commit 67c74ec

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ config.assets.configure do |env|
108108
end
109109
```
110110

111+
**`config.assets.resolve_assets_in_css_urls`**
112+
113+
When this option is enabled, sprockets-rails will register a CSS postprocessor to resolve assets referenced in [`url()`](https://developer.mozilla.org/en-US/docs/Web/CSS/url()) function calls and replace them with the digested paths. Defaults to `true`.
114+
111115
**`config.assets.resolve_with`**
112116

113117
A list of `:environment` and `:manifest` symbols that defines the order that

lib/sprockets/rails/asset_url_processor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Sprockets
22
module Rails
3-
# Rewrites urls in CSS files with the digested paths
3+
# Resolve assets referenced in CSS `url()` calls and replace them with the digested paths
44
class AssetUrlProcessor
55
REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?<relativeToCurrentDir>\.\/)?(?<path>[^"'\s)]+)\s*["']?\)/
66
def self.call(input)

lib/sprockets/railtie.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ def configure(&block)
9797
end
9898

9999
config.assets = OrderedOptions.new
100-
config.assets._blocks = []
101-
config.assets.paths = []
102-
config.assets.precompile = []
103-
config.assets.prefix = "/assets"
104-
config.assets.manifest = nil
105-
config.assets.quiet = false
100+
config.assets._blocks = []
101+
config.assets.paths = []
102+
config.assets.precompile = []
103+
config.assets.prefix = "/assets"
104+
config.assets.manifest = nil
105+
config.assets.quiet = false
106+
config.assets.resolve_assets_in_css_urls = true
106107

107108
initializer :set_default_precompile do |app|
108109
if using_sprockets4?
@@ -120,7 +121,9 @@ def configure(&block)
120121
end
121122

122123
initializer :asset_url_processor do |app|
123-
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
124+
if app.config.assets.resolve_assets_in_css_urls
125+
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
126+
end
124127
end
125128

126129
initializer :asset_sourcemap_url_processor do |app|

test/test_railtie.rb

+17
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,23 @@ def test_quiet_assets_inserts_middleware
423423
assert middleware.each_cons(2).include?([Sprockets::Rails::QuietAssets, Rails::Rack::Logger])
424424
end
425425

426+
def test_resolve_assets_in_css_urls_defaults_to_true
427+
app.initialize!
428+
429+
assert_equal true, app.config.assets.resolve_assets_in_css_urls
430+
assert_includes Sprockets.postprocessors['text/css'], Sprockets::Rails::AssetUrlProcessor
431+
end
432+
433+
def test_resolve_assets_in_css_urls_when_false_avoids_registering_postprocessor
434+
app.configure do
435+
config.assets.resolve_assets_in_css_urls = false
436+
end
437+
app.initialize!
438+
439+
assert_equal false, app.config.assets.resolve_assets_in_css_urls
440+
refute_includes Sprockets.postprocessors['text/css'], Sprockets::Rails::AssetUrlProcessor
441+
end
442+
426443
private
427444
def action_view
428445
ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil)

0 commit comments

Comments
 (0)