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

Warn on missing asset in compiler #31

Merged
merged 2 commits into from
Nov 25, 2021
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.
Jump to
Jump to file
Failed to load files.
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
Expand Up @@ -11,7 +11,7 @@ def initialize(assembly)
end

def compile(logical_path, input)
input.gsub(ASSET_URL_PATTERN) { asset_url resolve_path(logical_path.dirname, $1) }
input.gsub(ASSET_URL_PATTERN) { asset_url resolve_path(logical_path.dirname, $1), logical_path, $1 }
end

private
Expand All @@ -25,11 +25,12 @@ def resolve_path(directory, filename)
end
end

def asset_url(resolved_path)
def asset_url(resolved_path, logical_path, pattern)
if asset = assembly.load_path.find(resolved_path)
%[url("#{assembly.config.prefix}/#{asset.digested_path}")]
else
raise Propshaft::MissingAssetError.new(resolved_path)
Propshaft.logger.warn "Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}"
%[url("#{pattern}")]
end
end
end
5 changes: 2 additions & 3 deletions test/propshaft/compilers/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase
end

test "missing asset" do
assert_raise Propshaft::MissingAssetError do
compile_asset_with_content(%({ background: url(missing.jpg); }))
end
compiled = compile_asset_with_content(%({ background: url("file-not-found.jpg"); }))
assert_match /{ background: url\("file-not-found.jpg"\); }/, compiled
end

private
Expand Down