mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00

According to Zlib documentation, this is not a fatal error. In this case we attempt to deflate the asset three times before giving up. Sprockets will be replaced in the long term and this is an acceptable fix.
86 lines
2.6 KiB
Ruby
86 lines
2.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# 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
|
|
|
|
module FreedomPatches
|
|
module SprocketsPatches
|
|
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
|
|
|
|
if Rails.env.local?
|
|
Sprockets.register_bundle_metadata_reducer "application/javascript",
|
|
:data,
|
|
proc { +"" },
|
|
method(:concat_javascript_sources)
|
|
end
|
|
end
|
|
end
|
|
|
|
if Rails.env.local?
|
|
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
|
|
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,
|
|
)
|
|
|
|
# Skip sprockets fingerprinting for some assets
|
|
Sprockets::Asset.prepend(
|
|
Module.new do
|
|
def digest_path
|
|
# Webpack chunks are already named based on their contents
|
|
return logical_path if logical_path.start_with?("chunk.")
|
|
super
|
|
end
|
|
end,
|
|
)
|
|
|
|
# Sprockets::Cache::FileStore raises `Zlib::BufError: buffer error` sometimes.
|
|
# According to zlib documentation, Z_BUF_ERROR is not fatal and can be retried.
|
|
# https://www.zlib.net/zlib_faq.html#faq05
|
|
Sprockets::Cache::FileStore.prepend(
|
|
Module.new do
|
|
def set(key, value)
|
|
attempts = 3
|
|
|
|
begin
|
|
attempts -= 1
|
|
super
|
|
rescue Zlib::BufError
|
|
if attempts > 0
|
|
puts "Zlib::BufError while deflating #{key}, retrying #{attempts} more times"
|
|
retry
|
|
else
|
|
raise
|
|
end
|
|
end
|
|
end
|
|
end,
|
|
)
|