From fa7cabf5c306b29bdc60457f6be5337c06d3f80e Mon Sep 17 00:00:00 2001 From: dima Date: Fri, 18 Oct 2024 16:32:59 +0100 Subject: [PATCH 1/3] Make source maps work for assets in subdirectories --- lib/propshaft/compiler/source_mapping_urls.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/propshaft/compiler/source_mapping_urls.rb b/lib/propshaft/compiler/source_mapping_urls.rb index d15e862e..057de243 100644 --- a/lib/propshaft/compiler/source_mapping_urls.rb +++ b/lib/propshaft/compiler/source_mapping_urls.rb @@ -11,7 +11,7 @@ def compile(asset, input) private def asset_path(source_mapping_url, logical_path) - source_mapping_url.gsub!(/^(.+\/)?#{url_prefix}\//, "") + source_mapping_url = ::File.basename(source_mapping_url) if logical_path.dirname.to_s == "." source_mapping_url From 278116798aefd76cdd7345f7d4b5f2a0683bc7ab Mon Sep 17 00:00:00 2001 From: dima Date: Fri, 18 Oct 2024 17:08:18 +0100 Subject: [PATCH 2/3] Add tests for nested asset where sourceMappingURL contains the subdirectory --- ...ourceMappingURL-already-prefixed-nested-with-subdirectory.js | 1 + ...eMappingURL-already-prefixed-nested-with-subdirectory.js.map | 0 test/propshaft/compiler/source_mapping_urls_test.rb | 2 ++ 3 files changed, 3 insertions(+) create mode 100644 test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js create mode 100644 test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js.map diff --git a/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js new file mode 100644 index 00000000..82bc61b8 --- /dev/null +++ b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js @@ -0,0 +1 @@ +var fun; //# sourceMappingURL=/assets/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js.map diff --git a/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js.map b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js.map new file mode 100644 index 00000000..e69de29b diff --git a/test/propshaft/compiler/source_mapping_urls_test.rb b/test/propshaft/compiler/source_mapping_urls_test.rb index ca06d20a..f226b948 100644 --- a/test/propshaft/compiler/source_mapping_urls_test.rb +++ b/test/propshaft/compiler/source_mapping_urls_test.rb @@ -58,6 +58,8 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase compile_asset(find_asset("sourceMappingURL-already-prefixed.js", fixture_path: "mapped")) assert_match %r{//# sourceMappingURL=/assets/nested/sourceMappingURL-already-prefixed-nested-[a-z0-9]{8}\.js\.map}, compile_asset(find_asset("nested/sourceMappingURL-already-prefixed-nested.js", fixture_path: "mapped")) + assert_match %r{//# sourceMappingURL=/assets/nested/sourceMappingURL-already-prefixed-nested-with-subdirectory-[a-z0-9]{8}\.js\.map}, + compile_asset(find_asset("nested/sourceMappingURL-already-prefixed-nested-with-subdirectory.js", fixture_path: "mapped")) end test "sourceMapURL is already prefixed with an incorrect url_prefix" do From c583d9c49486621e594bc58efd2c3acc677d25ca Mon Sep 17 00:00:00 2001 From: Theodor Tonum Date: Mon, 4 Nov 2024 09:37:55 +0100 Subject: [PATCH 3/3] Split source map paths as absolute/relative --- lib/propshaft/compiler/source_mapping_urls.rb | 7 ++++--- .../nested/sourceMappingURL-already-prefixed-nested.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/propshaft/compiler/source_mapping_urls.rb b/lib/propshaft/compiler/source_mapping_urls.rb index 057de243..d8ddbefa 100644 --- a/lib/propshaft/compiler/source_mapping_urls.rb +++ b/lib/propshaft/compiler/source_mapping_urls.rb @@ -11,10 +11,11 @@ def compile(asset, input) private def asset_path(source_mapping_url, logical_path) - source_mapping_url = ::File.basename(source_mapping_url) + source_mapping_url.gsub!(/^(.+\/)?#{url_prefix}\//, "/") + source_mapping_path = Pathname.new(source_mapping_url) - if logical_path.dirname.to_s == "." - source_mapping_url + if source_mapping_path.absolute? + source_mapping_url.sub(/^\//, "") else logical_path.dirname.join(source_mapping_url).to_s end diff --git a/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js index bd54fcb9..2b1c85dc 100644 --- a/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js +++ b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js @@ -1 +1 @@ -var fun; //# sourceMappingURL=/assets/sourceMappingURL-already-prefixed-nested.js.map +var fun; //# sourceMappingURL=/assets/nested/sourceMappingURL-already-prefixed-nested.js.map