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

Process source mapping URLs be set by transpilers #479

Merged
merged 6 commits into from
Nov 15, 2021
Merged
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
22 changes: 22 additions & 0 deletions lib/sprockets/rails/sourcemapping_url_processor.rb
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
5 changes: 5 additions & 0 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
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
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