Skip to content

Commit

Permalink
Process source mapping URLs be set by transpilers (#479)
Browse files Browse the repository at this point in the history
* 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
dhh committed Nov 15, 2021
1 parent 58dbb85 commit d71a2d8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/sprockets/rails/sourcemapping_url_processor.rb
@@ -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
5 changes: 5 additions & 0 deletions lib/sprockets/railtie.rb
Expand Up @@ -6,6 +6,7 @@
require 'sprockets'

require 'sprockets/rails/asset_url_processor'
require 'sprockets/rails/sourcemapping_url_processor'
require 'sprockets/rails/context'
require 'sprockets/rails/helper'
require 'sprockets/rails/quiet_assets'
Expand Down Expand Up @@ -122,6 +123,10 @@ def configure(&block)
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
end

initializer :asset_sourcemap_url_processor do |app|
Sprockets.register_postprocessor "application/javascript", ::Sprockets::Rails::SourcemappingUrlProcessor
end

config.assets.version = ""
config.assets.debug = false
config.assets.compile = true
Expand Down
38 changes: 38 additions & 0 deletions test/test_sourcemapping_url_processor.rb
@@ -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

0 comments on commit d71a2d8

Please sign in to comment.