From a043d167bc99ab51a6c2b834526c4fec3ff9a6be Mon Sep 17 00:00:00 2001 From: Tim Guibord Date: Mon, 22 Jan 2024 15:13:06 -0500 Subject: [PATCH] Handle sourceMappingURL already prefixed with asset path (#170) --- lib/propshaft/compiler/source_mapping_urls.rb | 2 ++ .../sourceMappingURL-already-prefixed-nested.js | 1 + .../sourceMappingURL-already-prefixed-nested.js.map | 0 .../sourceMappingURL-already-prefixed-invalid.js | 1 + .../sourceMappingURL-already-prefixed-invalid.js.map | 0 .../mapped/sourceMappingURL-already-prefixed.js | 1 + .../mapped/sourceMappingURL-already-prefixed.js.map | 0 test/propshaft/compiler/source_mapping_urls_test.rb | 12 ++++++++++++ 8 files changed, 17 insertions(+) create mode 100644 test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js create mode 100644 test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js.map create mode 100644 test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js create mode 100644 test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js.map create mode 100644 test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js create mode 100644 test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js.map diff --git a/lib/propshaft/compiler/source_mapping_urls.rb b/lib/propshaft/compiler/source_mapping_urls.rb index a0b678d..bad3f0e 100644 --- a/lib/propshaft/compiler/source_mapping_urls.rb +++ b/lib/propshaft/compiler/source_mapping_urls.rb @@ -11,6 +11,8 @@ def compile(logical_path, input) private def asset_path(source_mapping_url, logical_path) + source_mapping_url.gsub!(/^(.+\/)?#{url_prefix}\//, "") + if logical_path.dirname.to_s == "." source_mapping_url else diff --git a/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js new file mode 100644 index 0000000..bd54fcb --- /dev/null +++ b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js @@ -0,0 +1 @@ +var fun; //# sourceMappingURL=/assets/sourceMappingURL-already-prefixed-nested.js.map diff --git a/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js.map b/test/fixtures/assets/mapped/nested/sourceMappingURL-already-prefixed-nested.js.map new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js b/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js new file mode 100644 index 0000000..a52b8dd --- /dev/null +++ b/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js @@ -0,0 +1 @@ +var fun; //# sourceMappingURL=thisisinvalidassets/sourceMappingURL-already-prefixed-invalid.js.map diff --git a/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js.map b/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed-invalid.js.map new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js b/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js new file mode 100644 index 0000000..5bdf328 --- /dev/null +++ b/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js @@ -0,0 +1 @@ +var fun; //# sourceMappingURL=/assets/sourceMappingURL-already-prefixed.js.map diff --git a/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js.map b/test/fixtures/assets/mapped/sourceMappingURL-already-prefixed.js.map new file mode 100644 index 0000000..e69de29 diff --git a/test/propshaft/compiler/source_mapping_urls_test.rb b/test/propshaft/compiler/source_mapping_urls_test.rb index c881f4d..8b2e0b8 100644 --- a/test/propshaft/compiler/source_mapping_urls_test.rb +++ b/test/propshaft/compiler/source_mapping_urls_test.rb @@ -53,6 +53,18 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase compile_asset(find_asset("sourceMappingURL-outside-comment.css", fixture_path: "mapped")) end + test "sourceMapURL is already prefixed with url_prefix" do + assert_match %r{//# sourceMappingURL=/assets/sourceMappingURL-already-prefixed.js-[a-z0-9]{40}\.map}, + compile_asset(find_asset("sourceMappingURL-already-prefixed.js", fixture_path: "mapped")) + assert_match %r{//# sourceMappingURL=/assets/nested/sourceMappingURL-already-prefixed-nested.js-[a-z0-9]{40}\.map}, + compile_asset(find_asset("nested/sourceMappingURL-already-prefixed-nested.js", fixture_path: "mapped")) + end + + test "sourceMapURL is already prefixed with an incorrect url_prefix" do + refute_match %r{//# sourceMappingURL=thisisinvalidassets/sourceMappingURL-already-prefixed-invalid.js-[a-z0-9]{40}\.map}, + compile_asset(find_asset("sourceMappingURL-already-prefixed-invalid.js", fixture_path: "mapped")) + end + test "relative url root" do @options.relative_url_root = "/url-root"