Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sourceMappingURL in compressed/minified files #142

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\*/)?(?:\r?\n)*)\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_begin, comment_end)
if asset = assembly.load_path.find(resolved_path)
"#{comment}# sourceMappingURL=#{url_prefix}/#{asset.digested_path}"
"#{comment_begin}# 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_begin}#{comment_end}"
end
end
end
1 change: 1 addition & 0 deletions test/fixtures/assets/mapped/compressed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.class{color:green}/*# sourceMappingURL=compressed.css.map */
Empty file.
2 changes: 2 additions & 0 deletions test/fixtures/assets/mapped/minified.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
12 changes: 11 additions & 1 deletion test/propshaft/compiler/source_mapping_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
compile_asset(find_asset("nested/another-source.js", fixture_path: "mapped"))
end

test "matching source map in css compressed mode" do
assert_match %r{/\*# sourceMappingURL=/assets/compressed.css-[a-z0-9]{40}\.map},
compile_asset(find_asset("compressed.css", fixture_path: "mapped"))
end

test "matching source map in js minified mode" do
assert_match %r{//# sourceMappingURL=/assets/minified.js-[a-z0-9]{40}\.map},
compile_asset(find_asset("minified.js", fixture_path: "mapped"))
end

test "missing source map" do
assert_no_match %r{sourceMappingURL},
compile_asset(find_asset("sourceless.js", fixture_path: "mapped"))
Expand All @@ -42,7 +52,7 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
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
test "sourceMappingURL not at the end of the source 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
Expand Down