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

Add rewriting of sourceMappingURL comment for css #498

Open
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/sprockets/rails/base_sourcemapping_url_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Sprockets
module Rails
class BaseSourcemappingUrlProcessor
class << self
def call(input)
env = input[:environment]
context = env.context_class.new(input)
data = input[:data].gsub(self::REGEX) do |_match|
sourcemap_logical_path = combine_sourcemap_logical_path(sourcefile: input[:name], sourcemap: $1)

begin
resolved_sourcemap_comment(sourcemap_logical_path, context: context)
rescue Sprockets::FileNotFound
removed_sourcemap_comment(sourcemap_logical_path, filename: input[:filename], env: env)
end
end

{ data: data }
end

private
def combine_sourcemap_logical_path(sourcefile:, sourcemap:)
if (parts = sourcefile.split("/")).many?
parts[0..-2].append(sourcemap).join("/")
else
sourcemap
end
end

def sourcemap_asset_path(sourcemap_logical_path, context:)
# FIXME: Work-around for bug where if the sourcemap is nested two levels deep, it'll resolve as the source file
# that's being mapped, rather than the map itself. So context.resolve("a/b/c.js.map") will return "c.js?"
if context.resolve(sourcemap_logical_path) =~ /\.map/
context.asset_path(sourcemap_logical_path)
else
raise Sprockets::FileNotFound, "Failed to resolve source map asset due to nesting depth"
end
end

def removed_sourcemap_comment(sourcemap_logical_path, filename:, env:)
env.logger.warn "Removed sourceMappingURL comment for missing asset '#{sourcemap_logical_path}' from #{filename}"
nil
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/sprockets/rails/css_sourcemapping_url_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Sprockets
module Rails
# Rewrites source mapping urls with the digested paths and protect against semicolon appending with a dummy comment line
class CssSourcemappingUrlProcessor < BaseSourcemappingUrlProcessor
REGEX = %r{/\*# sourceMappingURL=(.*\.map)\s*\*/}

class << self

private

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

end
end
end
end
38 changes: 1 addition & 37 deletions lib/sprockets/rails/sourcemapping_url_processor.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,17 @@
module Sprockets
module Rails
# Rewrites source mapping urls with the digested paths and protect against semicolon appending with a dummy comment line
class SourcemappingUrlProcessor
class SourcemappingUrlProcessor < BaseSourcemappingUrlProcessor
REGEX = /\/\/# sourceMappingURL=(.*\.map)/

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

begin
resolved_sourcemap_comment(sourcemap_logical_path, context: context)
rescue Sprockets::FileNotFound
removed_sourcemap_comment(sourcemap_logical_path, filename: input[:filename], env: env)
end
end

{ data: data }
end

private
def combine_sourcemap_logical_path(sourcefile:, sourcemap:)
if (parts = sourcefile.split("/")).many?
parts[0..-2].append(sourcemap).join("/")
else
sourcemap
end
end

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

def sourcemap_asset_path(sourcemap_logical_path, context:)
# FIXME: Work-around for bug where if the sourcemap is nested two levels deep, it'll resolve as the source file
# that's being mapped, rather than the map itself. So context.resolve("a/b/c.js.map") will return "c.js?"
if context.resolve(sourcemap_logical_path) =~ /\.map/
context.asset_path(sourcemap_logical_path)
else
raise Sprockets::FileNotFound, "Failed to resolve source map asset due to nesting depth"
end
end

def removed_sourcemap_comment(sourcemap_logical_path, filename:, env:)
env.logger.warn "Removed sourceMappingURL comment for missing asset '#{sourcemap_logical_path}' from #{filename}"
nil
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
require 'sprockets'

require 'sprockets/rails/asset_url_processor'
require 'sprockets/rails/base_sourcemapping_url_processor'
require 'sprockets/rails/sourcemapping_url_processor'
require 'sprockets/rails/css_sourcemapping_url_processor'
require 'sprockets/rails/context'
require 'sprockets/rails/helper'
require 'sprockets/rails/quiet_assets'
Expand Down Expand Up @@ -128,6 +130,7 @@ def configure(&block)

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

config.assets.version = ""
Expand Down
49 changes: 49 additions & 0 deletions test/test_css_sourcemapping_url_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'minitest/autorun'
require 'sprockets/railtie'

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
class TestCssSourceMappingUrlProcessor < Minitest::Test
def setup
@env = Sprockets::Environment.new
end

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

def asset_path(path, options = {})
"/assets/mapped-HEXGOESHERE.css.map"
end
end

input = { environment: @env, data: "div {\ndisplay: none;\n}\n/*# sourceMappingURL=mapped.css.map */", name: 'mapped', filename: 'mapped.css', metadata: {} }
output = Sprockets::Rails::CssSourcemappingUrlProcessor.call(input)
assert_equal({ data: "div {\ndisplay: none;\n}\n/*# sourceMappingURL=/assets/mapped-HEXGOESHERE.css.map */\n" }, output)
end

def test_resolving_erroneously_without_map_extension
@env.context_class.class_eval do
def resolve(path, **kargs)
"/assets/mapped.css"
end
end

input = { environment: @env, data: "div {\ndisplay: none;\n}\n/*# sourceMappingURL=mapped.css.map */", name: 'mapped', filename: 'mapped.css', metadata: {} }
output = Sprockets::Rails::CssSourcemappingUrlProcessor.call(input)
assert_equal({ data: "div {\ndisplay: none;\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: "div {\ndisplay: none;\n}\n/*# sourceMappingURL=mappedNOT.css.map */", name: 'mapped', filename: 'mapped.css', metadata: {} }
output = Sprockets::Rails::CssSourcemappingUrlProcessor.call(input)
assert_equal({ data: "div {\ndisplay: none;\n}\n" }, output)
end
end