Skip to content

Commit

Permalink
Source maps do not have to be on their own line (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
pond authored Sep 20, 2023
1 parent f2b7a39 commit 96ddc84
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/propshaft/compiler/source_mapping_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions test/fixtures/assets/mapped/sourceMappingURL-not-at-end.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.class {
/*# sourceMappingURL=sourceMappingURL-not-at-end.css.map */
color: green;
}
7 changes: 4 additions & 3 deletions test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.class {
color: green; /*# sourceMappingURL=sourceMappingURL-not-at-start.css.map */
}
.class{color:green;}/*# sourceMappingURL=sourceMappingURL-not-at-start.css.map */



Empty file.
2 changes: 2 additions & 0 deletions test/fixtures/assets/mapped/sourceMappingURL-not-at-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var fun; //# sourceMappingURL=sourceMappingURL-not-at-start.js.map

Empty file.
16 changes: 11 additions & 5 deletions test/propshaft/compiler/source_mapping_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 96ddc84

Please sign in to comment.