-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process source mapping URLs be set by transpilers (#479)
* Process source mapping URLs be set by transpilers Rewrites source mapping urls with the digested paths and protect against semicolon appending with a dummy comment line. * Correct file name * Correct indention * Ensure files that can't resolve raise * Test source mapping url processor * Warn in the log about removing the sourceMappingURL on missing assets
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Sprockets | ||
module Rails | ||
# Rewrites source mapping urls with the digested paths and protect against semicolon appending with a dummy comment line | ||
class SourcemappingUrlProcessor | ||
REGEX = /\/\/# sourceMappingURL=(.*\.map)/ | ||
|
||
def self.call(input) | ||
env = input[:environment] | ||
context = env.context_class.new(input) | ||
data = input[:data].gsub(REGEX) do |_match| | ||
ensure_file_is_present = context.resolve($1) | ||
"//# sourceMappingURL=#{context.asset_path($1)}\n//!\n" | ||
rescue Sprockets::FileNotFound | ||
env.logger.warn "Removed sourceMappingURL comment for missing asset '#{$1}' from #{input[:filename]}" | ||
nil | ||
end | ||
|
||
{ data: data } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'minitest/autorun' | ||
require 'sprockets/railtie' | ||
|
||
|
||
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | ||
class TestSourceMappingUrlProcessor < Minitest::Test | ||
def setup | ||
@env = Sprockets::Environment.new | ||
end | ||
|
||
def test_successful | ||
@env.context_class.class_eval do | ||
def resolve(path, **kargs) | ||
"/yes" | ||
end | ||
|
||
def asset_path(path, options = {}) | ||
'mapped-HEXGOESHERE.js.map' | ||
end | ||
end | ||
|
||
input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mapped.js.map", filename: 'mapped.js', metadata: {} } | ||
output = Sprockets::Rails::SourcemappingUrlProcessor.call(input) | ||
assert_equal({ data: "var mapped;\n//# sourceMappingURL=mapped-HEXGOESHERE.js.map\n//!\n" }, output) | ||
end | ||
|
||
def test_missing | ||
@env.context_class.class_eval do | ||
def resolve(path, **kargs) | ||
raise Sprockets::FileNotFound | ||
end | ||
end | ||
|
||
input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mappedNOT.js.map", filename: 'mapped.js', metadata: {} } | ||
output = Sprockets::Rails::SourcemappingUrlProcessor.call(input) | ||
assert_equal({ data: "var mapped;\n" }, output) | ||
end | ||
end |