Skip to content

Commit

Permalink
handle inline sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklozon committed Feb 21, 2024
1 parent 065cbe8 commit 4b578f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/sprockets/rails/sourcemapping_url_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ 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)/
FILE_REGEX = /\/\/# sourceMappingURL=(.*\.map)/
INLINE_REGEX = /\/\/# sourceMappingURL=(data:.*)$/

class << self
def call(input)
env = input[:environment]
context = env.context_class.new(input)
data = input[:data].gsub(REGEX) do |_match|
data = input[:data].gsub(FILE_REGEX) do |_match|
sourcemap_logical_path = combine_sourcemap_logical_path(sourcefile: input[:name], sourcemap: $1)

begin
resolved_sourcemap_comment(sourcemap_logical_path, context: context)
resolved_sourcemap_file_comment(sourcemap_logical_path, context: context)
rescue Sprockets::FileNotFound
removed_sourcemap_comment(sourcemap_logical_path, filename: input[:filename], env: env)
removed_sourcemap_file_comment(sourcemap_logical_path, filename: input[:filename], env: env)
end
end
data = data.gsub(INLINE_REGEX) do |_match|
"//# sourceMappingURL=#{$1}\n//!\n"
end

{ data: data }
end
Expand All @@ -30,7 +34,7 @@ def combine_sourcemap_logical_path(sourcefile:, sourcemap:)
end
end

def resolved_sourcemap_comment(sourcemap_logical_path, context:)
def resolved_sourcemap_file_comment(sourcemap_logical_path, context:)
"//# sourceMappingURL=#{sourcemap_asset_path(sourcemap_logical_path, context: context)}\n//!\n"
end

Expand All @@ -44,7 +48,7 @@ def sourcemap_asset_path(sourcemap_logical_path, context:)
end
end

def removed_sourcemap_comment(sourcemap_logical_path, filename:, env:)
def removed_sourcemap_file_comment(sourcemap_logical_path, filename:, env:)
env.logger.warn "Removed sourceMappingURL comment for missing asset '#{sourcemap_logical_path}' from #{filename}"
nil
end
Expand Down
12 changes: 12 additions & 0 deletions test/test_sourcemapping_url_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ def resolve(path, **kargs)
output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
assert_equal({ data: "var mapped;\n" }, output)
end

def test_inline_successful
@env.context_class.class_eval do
def resolve(path, **kargs)
"/assets/mapped.js.map"
end
end

input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=data:application/json;base64,abc123=", name: 'mapped', filename: 'mapped.js', metadata: {} }
output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
assert_equal({ data: "var mapped;\n//# sourceMappingURL=data:application/json;base64,abc123=\n//!\n" }, output)
end
end

0 comments on commit 4b578f8

Please sign in to comment.