mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-04-30 20:01:00 +08:00
77 lines
1.7 KiB
Ruby
77 lines
1.7 KiB
Ruby
# 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, settings = {}, minify: true)
|
|
@theme_id = theme_id
|
|
@input_tree = {}
|
|
@theme_name = theme_name
|
|
@minify = minify
|
|
@settings = settings
|
|
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,
|
|
settings: @settings,
|
|
minify: @minify && !@@terser_disabled,
|
|
entrypoints: {
|
|
main: {
|
|
modules: @input_tree.keys,
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
@content = output["main.js"]["code"]
|
|
@source_map = output["main.js"]["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
|