diff --git a/lib/propshaft/compiler/source_mapping_urls.rb b/lib/propshaft/compiler/source_mapping_urls.rb index de867d8..0a32d90 100644 --- a/lib/propshaft/compiler/source_mapping_urls.rb +++ b/lib/propshaft/compiler/source_mapping_urls.rb @@ -3,10 +3,10 @@ require "propshaft/compiler" class Propshaft::Compiler::SourceMappingUrls < Propshaft::Compiler - SOURCE_MAPPING_PATTERN = %r{^(//|/\*)# sourceMappingURL=(.+\.map)} + SOURCE_MAPPING_PATTERN = %r{(//|/\*)# sourceMappingURL=(.+\.map)(\s*?\*\/)?\s*?\Z} def compile(logical_path, input) - input.gsub(SOURCE_MAPPING_PATTERN) { source_mapping_url(asset_path($2, logical_path), $1) } + input.gsub(SOURCE_MAPPING_PATTERN) { source_mapping_url(asset_path($2, logical_path), $1, $3) } end private @@ -18,12 +18,12 @@ def asset_path(source_mapping_url, logical_path) end end - def source_mapping_url(resolved_path, comment) + def source_mapping_url(resolved_path, comment_start, comment_end) if asset = assembly.load_path.find(resolved_path) - "#{comment}# sourceMappingURL=#{url_prefix}/#{asset.digested_path}" + "#{comment_start}# sourceMappingURL=#{url_prefix}/#{asset.digested_path}#{comment_end}" else Propshaft.logger.warn "Removed sourceMappingURL comment for missing asset '#{resolved_path}' from #{resolved_path}" - comment + "#{comment_start}#{comment_end}" end end end diff --git a/test/fixtures/assets/mapped/sourceMappingURL-not-at-end.css b/test/fixtures/assets/mapped/sourceMappingURL-not-at-end.css new file mode 100644 index 0000000..96d8302 --- /dev/null +++ b/test/fixtures/assets/mapped/sourceMappingURL-not-at-end.css @@ -0,0 +1,4 @@ +.class { +/*# sourceMappingURL=sourceMappingURL-not-at-end.css.map */ + color: green; +} diff --git a/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css b/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css index db04e8b..ac555c1 100644 --- a/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css +++ b/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css @@ -1,3 +1,4 @@ -.class { - color: green; /*# sourceMappingURL=sourceMappingURL-not-at-start.css.map */ -} +.class{color:green;}/*# sourceMappingURL=sourceMappingURL-not-at-start.css.map */ + + + diff --git a/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css.map b/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css.map new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.js b/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.js new file mode 100644 index 0000000..d9c8fb9 --- /dev/null +++ b/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.js @@ -0,0 +1,2 @@ +var fun; //# sourceMappingURL=sourceMappingURL-not-at-start.js.map + diff --git a/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.js.map b/test/fixtures/assets/mapped/sourceMappingURL-not-at-start.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 f3dc551..1b10e4b 100644 --- a/test/propshaft/compiler/source_mapping_urls_test.rb +++ b/test/propshaft/compiler/source_mapping_urls_test.rb @@ -37,16 +37,22 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase compile_asset(find_asset("sourceless.css", fixture_path: "mapped")) end + test "sourceMappingURL not at the beginning of the line, but at end of file, is processed" do + assert_match %r{//# sourceMappingURL=/assets/sourceMappingURL-not-at-start.js-[a-z0-9]{40}\.map}, + compile_asset(find_asset("sourceMappingURL-not-at-start.js", fixture_path: "mapped")) + assert_match %r{/\*# sourceMappingURL=/assets/sourceMappingURL-not-at-start.css-[a-z0-9]{40}\.map \*/}, + compile_asset(find_asset("sourceMappingURL-not-at-start.css", fixture_path: "mapped")) + end + + test "sourceMappingURL not at end of file should be left alone" do + assert_match %r{sourceMappingURL=sourceMappingURL-not-at-end.css.map}, + compile_asset(find_asset("sourceMappingURL-not-at-end.css", fixture_path: "mapped")) + end test "sourceMappingURL outside of a comment should be left alone" do assert_match %r{sourceMappingURL=sourceMappingURL-outside-comment.css.map}, compile_asset(find_asset("sourceMappingURL-outside-comment.css", fixture_path: "mapped")) end - test "sourceMappingURL not at the beginning of the line should be left alone" do - assert_match %r{sourceMappingURL=sourceMappingURL-not-at-start.css.map}, - compile_asset(find_asset("sourceMappingURL-not-at-start.css", fixture_path: "mapped")) - end - test "relative url root" do @options.relative_url_root = "/url-root"