mirror of
https://github.com/discourse/discourse.git
synced 2026-08-07 13:19:19 +08:00
The content and hash of rollup chunks may vary when various internals/dependencies change. Previously, we didn't include the rollup hash on entrypoint filenames - we just relied on the Discourse-controlled input hash. We did include the rollup hash for chunks. That meant we could end up in a situation where the chunk filenames have changed, but cached entrypoints still reference the old chunk filenames. Also adds a guard to ensure we don't double-upload `.gz` assets to S3.
76 lines
1.6 KiB
Ruby
Vendored
76 lines
1.6 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
class ThemeJavascriptCompiler
|
|
class CompileError < StandardError
|
|
end
|
|
|
|
@@terser_disabled = false
|
|
def self.disable_terser!
|
|
raise "Tests only" if !Rails.env.test?
|
|
@@terser_disabled = true
|
|
end
|
|
|
|
def self.enable_terser!
|
|
raise "Tests only" if !Rails.env.test?
|
|
@@terser_disabled = false
|
|
end
|
|
|
|
def initialize(theme_id, theme_name, minify: true)
|
|
@theme_id = theme_id
|
|
@input_tree = {}
|
|
@theme_name = theme_name
|
|
@minify = minify
|
|
end
|
|
|
|
def compile!
|
|
if !@compiled
|
|
@compiled = true
|
|
@input_tree =
|
|
@input_tree.to_h do |k, v|
|
|
if k.end_with?(".js.es6")
|
|
[k.sub(/\.js\.es6$/, ".js"), AssetProcessor.append_es6_deprecation(v, k)]
|
|
else
|
|
[k, v]
|
|
end
|
|
end
|
|
@input_tree.freeze
|
|
|
|
output =
|
|
AssetProcessor.new.rollup(
|
|
@input_tree,
|
|
{
|
|
themeId: @theme_id,
|
|
minify: @minify && !@@terser_disabled,
|
|
entrypoints: {
|
|
main: {
|
|
modules: @input_tree.keys,
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
main = output.values.find { |chunk| chunk["name"] == "main" }
|
|
@content = main["code"]
|
|
@source_map = main["map"]
|
|
end
|
|
[@content, @source_map]
|
|
rescue AssetProcessor::TranspileError => e
|
|
message = "[THEME #{@theme_id} '#{@theme_name}'] Compile error: #{e.message}"
|
|
@content = "throw new Error(#{message.to_json});\n"
|
|
[@content, @source_map]
|
|
end
|
|
|
|
def content
|
|
compile!
|
|
@content
|
|
end
|
|
|
|
def source_map
|
|
compile!
|
|
@source_map
|
|
end
|
|
|
|
def append_tree(tree)
|
|
@input_tree.merge!(tree)
|
|
end
|
|
end
|