Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assets.resolve_assets_in_css_urls configuration option to allow disabling AssetUrlProcessor #489

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ config.assets.configure do |env|
end
```

**`config.assets.resolve_assets_in_css_urls`**

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`.

**`config.assets.resolve_with`**

A list of `:environment` and `:manifest` symbols that defines the order that
Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/rails/asset_url_processor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Sprockets
module Rails
# Rewrites urls in CSS files with the digested paths
# Resolve assets referenced in CSS `url()` calls and replace them with the digested paths
class AssetUrlProcessor
REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?<relativeToCurrentDir>\.\/)?(?<path>[^"'\s)]+)\s*["']?\)/
def self.call(input)
Expand Down
17 changes: 10 additions & 7 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ def configure(&block)
end

config.assets = OrderedOptions.new
config.assets._blocks = []
config.assets.paths = []
config.assets.precompile = []
config.assets.prefix = "/assets"
config.assets.manifest = nil
config.assets.quiet = false
config.assets._blocks = []
config.assets.paths = []
config.assets.precompile = []
config.assets.prefix = "/assets"
config.assets.manifest = nil
config.assets.quiet = false
config.assets.resolve_assets_in_css_urls = true

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

initializer :asset_url_processor do |app|
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
if app.config.assets.resolve_assets_in_css_urls
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
end
end

initializer :asset_sourcemap_url_processor do |app|
Expand Down
17 changes: 17 additions & 0 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,23 @@ def test_quiet_assets_inserts_middleware
assert middleware.each_cons(2).include?([Sprockets::Rails::QuietAssets, Rails::Rack::Logger])
end

def test_resolve_assets_in_css_urls_defaults_to_true
app.initialize!

assert_equal true, app.config.assets.resolve_assets_in_css_urls
assert_includes Sprockets.postprocessors['text/css'], Sprockets::Rails::AssetUrlProcessor
end

def test_resolve_assets_in_css_urls_when_false_avoids_registering_postprocessor
app.configure do
config.assets.resolve_assets_in_css_urls = false
end
app.initialize!

assert_equal false, app.config.assets.resolve_assets_in_css_urls
refute_includes Sprockets.postprocessors['text/css'], Sprockets::Rails::AssetUrlProcessor
end

private
def action_view
ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil)
Expand Down