Skip to content

Commit 8b0ff49

Browse files
committed
Update asset digests when sourcemaps change
When sourcemaps change, their digest will change, and the generated URL in the `# sourceMappingURL` comment will change. Therefore, it's important that the digest of the original asset also changes, so that caches can be invalidated correctly. This change makes use of the existing `#referenced_by` API in the SourceMappingUrls compiler, and implements the tests in a similar way to 59406ab.
1 parent 68bb8c5 commit 8b0ff49

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lib/propshaft/compiler/source_mapping_urls.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ def compile(asset, input)
99
input.gsub(SOURCE_MAPPING_PATTERN) { source_mapping_url(asset.logical_path, asset_path($2, asset.logical_path), $1, $3) }
1010
end
1111

12+
def referenced_by(asset, references: Set.new)
13+
asset.content.scan(SOURCE_MAPPING_PATTERN).each do |_, source_mapping_url, _|
14+
sourcemap_asset = load_path.find(asset_path(source_mapping_url, asset.logical_path))
15+
references << sourcemap_asset if sourcemap_asset
16+
end
17+
references
18+
end
19+
1220
private
1321
def asset_path(source_mapping_url, logical_path)
1422
source_mapping_url.gsub!(/^(.+\/)?#{url_prefix}\//, "")

test/propshaft/compiler/source_mapping_urls_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
7272
compile_asset(find_asset("source.js", fixture_path: "mapped"))
7373
end
7474

75+
test "changes to sourcemap are reflected in parent digest" do
76+
root_path = Pathname.new("#{__dir__}/../../fixtures/assets/mapped")
77+
assembly = Propshaft::Assembly.new(ActiveSupport::OrderedOptions.new.tap { |config|
78+
config.paths = [ root_path ]
79+
config.compilers = [[ "text/javascript", Propshaft::Compiler::SourceMappingUrls ]]
80+
})
81+
82+
digest = find_asset("source.js", fixture_path: "mapped", load_path: assembly.load_path).digest
83+
84+
open_asset_with_reset("mapped/source.js.map") do |sourcemap_file|
85+
sourcemap_file.write "changes!"
86+
sourcemap_file.flush
87+
88+
new_digest = find_asset("source.js", fixture_path: "mapped", load_path: assembly.load_path).digest
89+
assert_not_equal digest, new_digest
90+
end
91+
end
92+
7593
private
7694
def compile_asset(asset)
7795

@@ -81,6 +99,15 @@ def compile_asset(asset)
8199

82100
assembly.compilers.compile(asset)
83101
end
102+
103+
def open_asset_with_reset(logical_path)
104+
dependency_path = Pathname.new("#{__dir__}/../../fixtures/assets/#{logical_path}")
105+
existing_dependency_content = File.read(dependency_path)
106+
107+
File.open(dependency_path, "a") { |f| yield f }
108+
ensure
109+
File.write(dependency_path, existing_dependency_content)
110+
end
84111
end
85112

86113
# //# sourceMappingURL=/assets/sourceMappingURL-already-prefixed.js-[a-z0-9]{40}.map

test/test_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
class ActiveSupport::TestCase
1313
private
14-
def find_asset(logical_path, fixture_path:)
14+
def find_asset(logical_path, fixture_path:, load_path: nil)
1515
root_path = Pathname.new("#{__dir__}/fixtures/assets/#{fixture_path}")
1616
path = root_path.join(logical_path)
17-
load_path = Propshaft::LoadPath.new([ root_path ], compilers: Propshaft::Compilers.new(nil))
17+
load_path ||= Propshaft::LoadPath.new([ root_path ], compilers: Propshaft::Compilers.new(nil))
1818

1919
Propshaft::Asset.new(path, logical_path: logical_path, load_path: load_path)
2020
end

0 commit comments

Comments
 (0)