Skip to content

Commit d71a2d8

Browse files
authored
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
1 parent 58dbb85 commit d71a2d8

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Sprockets
2+
module Rails
3+
# Rewrites source mapping urls with the digested paths and protect against semicolon appending with a dummy comment line
4+
class SourcemappingUrlProcessor
5+
REGEX = /\/\/# sourceMappingURL=(.*\.map)/
6+
7+
def self.call(input)
8+
env = input[:environment]
9+
context = env.context_class.new(input)
10+
data = input[:data].gsub(REGEX) do |_match|
11+
ensure_file_is_present = context.resolve($1)
12+
"//# sourceMappingURL=#{context.asset_path($1)}\n//!\n"
13+
rescue Sprockets::FileNotFound
14+
env.logger.warn "Removed sourceMappingURL comment for missing asset '#{$1}' from #{input[:filename]}"
15+
nil
16+
end
17+
18+
{ data: data }
19+
end
20+
end
21+
end
22+
end

lib/sprockets/railtie.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'sprockets'
77

88
require 'sprockets/rails/asset_url_processor'
9+
require 'sprockets/rails/sourcemapping_url_processor'
910
require 'sprockets/rails/context'
1011
require 'sprockets/rails/helper'
1112
require 'sprockets/rails/quiet_assets'
@@ -122,6 +123,10 @@ def configure(&block)
122123
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
123124
end
124125

126+
initializer :asset_sourcemap_url_processor do |app|
127+
Sprockets.register_postprocessor "application/javascript", ::Sprockets::Rails::SourcemappingUrlProcessor
128+
end
129+
125130
config.assets.version = ""
126131
config.assets.debug = false
127132
config.assets.compile = true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'minitest/autorun'
2+
require 'sprockets/railtie'
3+
4+
5+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
6+
class TestSourceMappingUrlProcessor < Minitest::Test
7+
def setup
8+
@env = Sprockets::Environment.new
9+
end
10+
11+
def test_successful
12+
@env.context_class.class_eval do
13+
def resolve(path, **kargs)
14+
"/yes"
15+
end
16+
17+
def asset_path(path, options = {})
18+
'mapped-HEXGOESHERE.js.map'
19+
end
20+
end
21+
22+
input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mapped.js.map", filename: 'mapped.js', metadata: {} }
23+
output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
24+
assert_equal({ data: "var mapped;\n//# sourceMappingURL=mapped-HEXGOESHERE.js.map\n//!\n" }, output)
25+
end
26+
27+
def test_missing
28+
@env.context_class.class_eval do
29+
def resolve(path, **kargs)
30+
raise Sprockets::FileNotFound
31+
end
32+
end
33+
34+
input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mappedNOT.js.map", filename: 'mapped.js', metadata: {} }
35+
output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
36+
assert_equal({ data: "var mapped;\n" }, output)
37+
end
38+
end

0 commit comments

Comments
 (0)