-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process css files so that they get digested paths for asset files
This is required so that we can use cssbundling-rails and reference images that will receive digested paths
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Sprockets | ||
module Rails | ||
# Rewrites urls in CSS files with the digested paths | ||
class AssetUrlProcessor | ||
REGEX = /url\(\s*["']?(?!(?:\#|data|http))([^"'\s)]+)\s*["']?\)/ | ||
|
||
def self.call(input) | ||
context = input[:environment].context_class.new(input) | ||
data = input[:data].gsub(REGEX) do |_match| | ||
"url(#{context.asset_path($1)})" | ||
end | ||
{ data: data } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'minitest/autorun' | ||
require 'sprockets/railtie' | ||
|
||
|
||
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | ||
class TestAssetUrlProcessor < Minitest::Test | ||
def setup | ||
@env = Sprockets::Environment.new | ||
@env.context_class.class_eval do | ||
def asset_path(path, options = {}) | ||
'image-hexcodegoeshere.png' | ||
end | ||
end | ||
end | ||
|
||
def test_digest_with_single_quote | ||
input = { environment: @env, data: "url('image.png')", filename: 'url2.css', metadata: {} } | ||
output = Sprockets::Rails::AssetUrlProcessor.call(input) | ||
assert_equal({ data: "url(image-hexcodegoeshere.png)" }, output) | ||
end | ||
|
||
def test_digest_with_double_quote | ||
input = { environment: @env, data: 'url("image.png")', filename: 'url2.css', metadata: {} } | ||
output = Sprockets::Rails::AssetUrlProcessor.call(input) | ||
assert_equal({ data: "url(image-hexcodegoeshere.png)" }, output) | ||
end | ||
end |