Skip to content

Commit 9e80c01

Browse files
committed
skip resolve warning when present in public folder
1 parent 68bb8c5 commit 9e80c01

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

lib/propshaft/compiler/css_asset_urls.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ def asset_url(resolved_path, logical_path, fingerprint, pattern)
3737
if asset = load_path.find(resolved_path)
3838
%[url("#{url_prefix}/#{asset.digested_path}#{fingerprint}")]
3939
else
40-
Propshaft.logger.warn "Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}"
40+
unless public_file?(resolved_path)
41+
warn "Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}"
42+
end
4143
%[url("#{pattern}")]
4244
end
4345
end
46+
47+
def public_file?(filename)
48+
config.public_path && File.exist?(File.join(config.public_path, filename))
49+
end
4450
end

lib/propshaft/railtie.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class Railtie < ::Rails::Railtie
3838
config.assets.file_watcher ||= app.config.file_watcher
3939

4040
config.assets.relative_url_root ||= app.config.relative_url_root
41+
config.assets.public_path ||= app.config.paths["public"].first
4142
config.assets.output_path ||=
42-
Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix))
43+
Pathname.new(File.join(config.assets.public_path, app.config.assets.prefix))
4344
config.assets.manifest_path ||= config.assets.output_path.join(".manifest.json")
4445

4546
ActiveSupport.on_load(:action_view) do

test/fixtures/public/public-file.jpg

Loading

test/propshaft/compiler/css_asset_urls_test.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase
1313
}
1414
end
1515

16+
def capture_err
17+
_, err = capture_io do
18+
yield
19+
end
20+
err.strip
21+
end
22+
1623
test "basic" do
1724
compiled = compile_asset_with_content(%({ background: url(file.jpg); }))
1825
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
@@ -120,8 +127,11 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase
120127
end
121128

122129
test "missing asset" do
123-
compiled = compile_asset_with_content(%({ background: url("file-not-found.jpg"); }))
124-
assert_match(/{ background: url\("file-not-found.jpg"\); }/, compiled)
130+
err = capture_err do
131+
compiled = compile_asset_with_content(%({ background: url("file-not-found.jpg"); }))
132+
assert_match(/{ background: url\("file-not-found.jpg"\); }/, compiled)
133+
end
134+
assert_equal "Unable to resolve 'file-not-found.jpg' for missing asset 'foobar/source/file-not-found.jpg' in foobar/source/test.css", err.strip
125135
end
126136

127137
test "relative url root" do
@@ -131,6 +141,15 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase
131141
assert_match(/{ background: url\("\/url-root\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
132142
end
133143

144+
test "skips warnings for files already present in public folder" do
145+
@options.public_path = Pathname.new("#{__dir__}/../../fixtures/public").to_s
146+
err = capture_err do
147+
compiled = compile_asset_with_content(%({ background: url(/public-file.jpg); }))
148+
assert_match(/{ background: url\("\/public-file.jpg"\); }/, compiled)
149+
end
150+
assert_equal "", err
151+
end
152+
134153
private
135154
def compile_asset_with_content(content)
136155
root_path = Pathname.new("#{__dir__}/../../fixtures/assets/vendor")

0 commit comments

Comments
 (0)