2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-13 04:25:03 -04:00
|
|
|
# This contains two patches to make sprockets more tolerable in dev
|
|
|
|
#
|
|
|
|
# 1. Stop computing asset paths which triggers sprockets to do mountains of work
|
|
|
|
# All our assets in dev are in the /assets folder anyway
|
|
|
|
#
|
|
|
|
# 2. Stop using a concatenator that does tons of work checking for semicolons when
|
|
|
|
# when rebuilding an asset
|
|
|
|
|
2022-04-05 06:08:28 -04:00
|
|
|
module FreedomPatches
|
|
|
|
module SprocketsPatches
|
2016-05-13 04:25:03 -04:00
|
|
|
def self.concat_javascript_sources(buf, source)
|
|
|
|
if buf.bytesize > 0
|
|
|
|
# CODE REMOVED HERE
|
|
|
|
buf << ";" # unless string_end_with_semicolon?(buf)
|
|
|
|
buf << "\n" # unless buf.end_with?("\n")
|
|
|
|
end
|
|
|
|
buf << source
|
|
|
|
end
|
|
|
|
|
2022-04-05 06:08:28 -04:00
|
|
|
if Rails.env.development? || Rails.env.test?
|
|
|
|
Sprockets.register_bundle_metadata_reducer "application/javascript",
|
|
|
|
:data,
|
|
|
|
proc { +"" },
|
|
|
|
method(:concat_javascript_sources)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-13 04:25:03 -04:00
|
|
|
|
2022-04-05 06:08:28 -04:00
|
|
|
if Rails.env.development? || Rails.env.test?
|
|
|
|
ActiveSupport.on_load(:action_view) do
|
|
|
|
def compute_asset_path(source, _options = {})
|
|
|
|
"/assets/#{source}"
|
|
|
|
end
|
|
|
|
alias_method :public_compute_asset_path, :compute_asset_path
|
|
|
|
end
|
2016-05-13 04:25:03 -04:00
|
|
|
end
|
2023-04-21 14:35:00 -04:00
|
|
|
|
|
|
|
# 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,
|
|
|
|
)
|
2023-06-09 06:14:11 -04:00
|
|
|
|
2023-10-26 12:29:53 -04:00
|
|
|
# Skip sprockets fingerprinting for some assets
|
2023-06-09 06:14:11 -04:00
|
|
|
Sprockets::Asset.prepend(
|
|
|
|
Module.new do
|
|
|
|
def digest_path
|
2023-10-26 12:29:53 -04:00
|
|
|
# Webpack chunks are already named based on their contents
|
|
|
|
return logical_path if logical_path.start_with?("chunk.")
|
2023-06-09 06:14:11 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
)
|