diff --git a/lib/sprockets/rails/sourcemapping_url_processor.rb b/lib/sprockets/rails/sourcemapping_url_processor.rb index fe8a29cc..25745bfa 100644 --- a/lib/sprockets/rails/sourcemapping_url_processor.rb +++ b/lib/sprockets/rails/sourcemapping_url_processor.rb @@ -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 @@ -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 @@ -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 diff --git a/test/test_sourcemapping_url_processor.rb b/test/test_sourcemapping_url_processor.rb index ead7c33a..9882c111 100644 --- a/test/test_sourcemapping_url_processor.rb +++ b/test/test_sourcemapping_url_processor.rb @@ -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