Skip to content

Commit

Permalink
Warn on missing asset in compiler (#31)
Browse files Browse the repository at this point in the history
* Warn on missing asset in compiler

* Clarify warning

Co-authored-by: David Heinemeier Hansson <david@basecamp.com>
  • Loading branch information
brenogazzola and dhh authored Nov 25, 2021
1 parent 5cd9716 commit 7f60c9d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
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

0 comments on commit 7f60c9d

Please sign in to comment.