Skip to content
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
7 changes: 4 additions & 3 deletions lib/propshaft/compilers/css_asset_urls.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

class Propshaft::Compilers::CssAssetUrls
attr_reader :assembly
attr_reader :assembly, :url_prefix

ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|%23|data|http|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/

def initialize(assembly)
@assembly = assembly
@assembly = assembly
@url_prefix = File.join(assembly.config.host.to_s, assembly.config.prefix.to_s).chomp("/")
end

def compile(logical_path, input)
Expand All @@ -26,7 +27,7 @@ def resolve_path(directory, filename)

def asset_url(resolved_path, logical_path, fingerprint, pattern)
if asset = assembly.load_path.find(resolved_path)
%[url("#{assembly.config.prefix}/#{asset.digested_path}#{fingerprint}")]
%[url("#{url_prefix}/#{asset.digested_path}#{fingerprint}")]
else
Propshaft.logger.warn "Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}"
%[url("#{pattern}")]
Expand Down
2 changes: 2 additions & 0 deletions lib/propshaft/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Railtie < ::Rails::Railtie
]
config.assets.sweep_cache = Rails.env.development?
config.assets.server = Rails.env.development? || Rails.env.test?
config.assets.host = nil

# Register propshaft initializer to copy the assets path in all the Rails Engines.
# This makes possible for us to keep all `assets` config in this Railtie, but still
Expand All @@ -30,6 +31,7 @@ class Railtie < ::Rails::Railtie
end

config.after_initialize do |app|
config.assets.host = app.config.asset_host
config.assets.output_path ||=
Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix))

Expand Down
17 changes: 12 additions & 5 deletions test/propshaft/compilers/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase
setup do
@assembly = Propshaft::Assembly.new(ActiveSupport::OrderedOptions.new.tap { |config|
@options = ActiveSupport::OrderedOptions.new.tap { |config|
config.paths = [ Pathname.new("#{__dir__}/../../fixtures/assets/vendor") ]
config.output_path = Pathname.new("#{__dir__}/../../fixtures/output")
config.prefix = "/assets"
})

@assembly.compilers.register "text/css", Propshaft::Compilers::CssAssetUrls
}
end

test "basic" do
Expand Down Expand Up @@ -120,14 +118,23 @@ class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase
assert_match(/{ background: url\("file-not-found.jpg"\); }/, compiled)
end

test "host" do
@options.host = "https://example.com"

compiled = compile_asset_with_content(%({ background: url(file.jpg); }))
assert_match(/{ background: url\("https:\/\/example.com\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
end

private
def compile_asset_with_content(content)
root_path = Pathname.new("#{__dir__}/../../fixtures/assets/vendor")
logical_path = "foobar/source/test.css"

asset = Propshaft::Asset.new(root_path.join(logical_path), logical_path: logical_path)
asset.stub :content, content do
@assembly.compilers.compile(asset)
assembly = Propshaft::Assembly.new(@options)
assembly.compilers.register "text/css", Propshaft::Compilers::CssAssetUrls
assembly.compilers.compile(asset)
end
end
end