Skip to content

Instantly share code, notes, and snippets.

@nicbet
Last active October 17, 2021 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicbet/774fddcc8ecc7bed2575e0f08b20e96e to your computer and use it in GitHub Desktop.
Save nicbet/774fddcc8ecc7bed2575e0f08b20e96e to your computer and use it in GitHub Desktop.
Rails 7 reference assets with CSS Bundler
# config/initializers/asset_url_processor.rb
# See https://github.com/rails/cssbundling-rails/issues/22
class AssetUrlProcessor
def self.call(input)
context = input[:environment].context_class.new(input)
data = input[:data].gsub(/url\(\s*["']?(?!(?:\#|data|http))([^"'\s)]+)\s*["']?\)/) do |_match|
"url(#{context.asset_path($1)})"
end
{data: data}
end
end
Sprockets.register_postprocessor "text/css", AssetUrlProcessor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment