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

Handline Inline Sourcemaps #522

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:.*)$/
Copy link
Author

@nicklozon nicklozon Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically inline sourcemaps will look something like this: sourceMappingURL=data:application/json;base64,abc123= where abc123= is encoded data. The regex assumes anything starting with data: is an inline sourcemap, which I fell is a safe assumption from my experience.


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