DEV: Patch Sprockets::DirectiveProcessor to avoid extra newline (#21203)

By default, the Sprockets DirectiveProcessor introduces a newline between possible 'header' comments and the rest of the JS file. This causes sourcemaps to be offset by 1 line, and therefore breaks browser tooling. We know that Ember-Cli assets do not use Sprockets directives, so we can totally bypass the DirectiveProcessor for those files.

We're using v3 of Sprockets, which is no longer supported - upstreaming a fix will be difficult. Long term, we intend to move away from sprockets.
This commit is contained in:
David Taylor 2023-04-21 19:35:00 +01:00 committed by GitHub
parent 34ffc0065a
commit 56115977c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -36,3 +36,16 @@ if Rails.env.development? || Rails.env.test?
alias_method :public_compute_asset_path, :compute_asset_path
end
end
# By default, the Sprockets DirectiveProcessor introduces a newline between possible 'header' comments
# and the rest of the JS file. (https://github.com/rails/sprockets/blob/f4d3dae71e/lib/sprockets/directive_processor.rb#L121)
# This causes sourcemaps to be offset by 1 line, and therefore breaks browser tooling.
# We know that Ember-Cli assets do not use Sprockets directives, so we can totally bypass the DirectiveProcessor for those files.
Sprockets::DirectiveProcessor.prepend(
Module.new do
def process_source(source)
return source, [] if EmberCli.is_ember_cli_asset?(File.basename(@filename))
super
end
end,
)