2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-17 19:46:11 +08:00
|
|
|
class ThemeJavascriptCompiler
|
|
|
|
|
2022-10-19 17:49:01 +08:00
|
|
|
COLOCATED_CONNECTOR_REGEX = /\A(?<prefix>.*\/?)connectors\/(?<outlet>[^\/]+)\/(?<name>[^\/\.]+)\.(?<extension>.+)\z/
|
2019-01-17 19:46:11 +08:00
|
|
|
|
|
|
|
class CompileError < StandardError
|
|
|
|
end
|
|
|
|
|
2022-10-19 01:20:10 +08:00
|
|
|
@@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
|
2019-01-17 19:46:11 +08:00
|
|
|
|
2019-05-24 22:25:55 +08:00
|
|
|
def initialize(theme_id, theme_name)
|
2019-01-17 19:46:11 +08:00
|
|
|
@theme_id = theme_id
|
2022-10-19 01:20:10 +08:00
|
|
|
@output_tree = []
|
2019-05-24 22:25:55 +08:00
|
|
|
@theme_name = theme_name
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|
|
|
|
|
2022-10-19 01:20:10 +08:00
|
|
|
def compile!
|
|
|
|
if !@compiled
|
|
|
|
@compiled = true
|
|
|
|
@output_tree.freeze
|
|
|
|
output = if !has_content?
|
|
|
|
{ "code" => "" }
|
|
|
|
elsif @@terser_disabled
|
|
|
|
{ "code" => raw_content }
|
|
|
|
else
|
|
|
|
DiscourseJsProcessor::Transpiler.new.terser(@output_tree.to_h, terser_config)
|
|
|
|
end
|
|
|
|
@content = output["code"]
|
|
|
|
@source_map = output["map"]
|
|
|
|
end
|
|
|
|
[@content, @source_map]
|
|
|
|
rescue DiscourseJsProcessor::TranspileError => e
|
|
|
|
message = "[THEME #{@theme_id} '#{@theme_name}'] Compile error: #{e.message}"
|
|
|
|
@content = "console.error(#{message.to_json});\n"
|
|
|
|
[@content, @source_map]
|
|
|
|
end
|
|
|
|
|
|
|
|
def terser_config
|
|
|
|
# Based on https://github.com/ember-cli/ember-cli-terser/blob/28df3d90a5/index.js#L12-L26
|
|
|
|
{
|
|
|
|
sourceMap: { includeSources: true, root: "theme-#{@theme_id}/" },
|
|
|
|
compress: {
|
|
|
|
negate_iife: false,
|
|
|
|
sequences: 30,
|
2022-11-01 21:56:33 +08:00
|
|
|
drop_debugger: false,
|
2022-10-19 01:20:10 +08:00
|
|
|
},
|
|
|
|
output: {
|
|
|
|
semicolons: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
compile!
|
|
|
|
@content
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_map
|
|
|
|
compile!
|
|
|
|
@source_map
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_content
|
|
|
|
@output_tree.map { |filename, source| source }.join("")
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_content?
|
|
|
|
@output_tree.present?
|
|
|
|
end
|
|
|
|
|
2019-01-17 19:46:11 +08:00
|
|
|
def prepend_settings(settings_hash)
|
2022-10-19 01:20:10 +08:00
|
|
|
@output_tree.prepend ['settings.js', <<~JS]
|
2019-01-17 19:46:11 +08:00
|
|
|
(function() {
|
2021-04-12 20:02:58 +08:00
|
|
|
if ('require' in window) {
|
|
|
|
require("discourse/lib/theme-settings-store").registerSettings(#{@theme_id}, #{settings_hash.to_json});
|
2019-01-17 19:46:11 +08:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
2022-10-17 22:04:04 +08:00
|
|
|
def append_tree(tree, for_tests: false)
|
|
|
|
# Replace legacy extensions
|
|
|
|
tree.transform_keys! do |filename|
|
|
|
|
if filename.ends_with? ".js.es6"
|
|
|
|
filename.sub(/\.js\.es6\z/, ".js")
|
|
|
|
elsif filename.ends_with? ".raw.hbs"
|
|
|
|
filename.sub(/\.raw\.hbs\z/, ".hbr")
|
|
|
|
else
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-19 17:49:01 +08:00
|
|
|
# Some themes are colocating connector JS under `/connectors`. Move template to /templates to avoid module name clash
|
|
|
|
tree.transform_keys! do |filename|
|
|
|
|
match = COLOCATED_CONNECTOR_REGEX.match(filename)
|
|
|
|
next filename if !match
|
|
|
|
|
|
|
|
is_template = match[:extension] == "hbs"
|
|
|
|
is_in_templates_directory = match[:prefix].split("/").last == "templates"
|
|
|
|
|
|
|
|
if is_template && !is_in_templates_directory
|
|
|
|
"#{match[:prefix]}templates/connectors/#{match[:outlet]}/#{match[:name]}.#{match[:extension]}"
|
|
|
|
elsif !is_template && is_in_templates_directory
|
|
|
|
"#{match[:prefix].chomp('templates/')}connectors/#{match[:outlet]}/#{match[:name]}.#{match[:extension]}"
|
|
|
|
else
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-17 21:47:16 +08:00
|
|
|
# Handle colocated components
|
|
|
|
tree.dup.each_pair do |filename, content|
|
2022-12-07 22:24:03 +08:00
|
|
|
is_component_template = filename.end_with?(".hbs") && filename.start_with?("discourse/components/", "admin/components/")
|
2022-10-17 21:47:16 +08:00
|
|
|
next if !is_component_template
|
|
|
|
template_contents = content
|
|
|
|
|
|
|
|
hbs_invocation_options = {
|
|
|
|
moduleName: filename,
|
|
|
|
parseOptions: {
|
|
|
|
srcName: filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hbs_invocation = "hbs(#{template_contents.to_json}, #{hbs_invocation_options.to_json})"
|
|
|
|
|
|
|
|
prefix = <<~JS
|
|
|
|
import { hbs } from 'ember-cli-htmlbars';
|
|
|
|
const __COLOCATED_TEMPLATE__ = #{hbs_invocation};
|
|
|
|
JS
|
|
|
|
|
|
|
|
js_filename = filename.sub(/\.hbs\z/, ".js")
|
|
|
|
js_contents = tree[js_filename] # May be nil for template-only component
|
|
|
|
if js_contents && !js_contents.include?("export default")
|
|
|
|
message = "#{filename} does not contain a `default export`. Did you forget to export the component class?"
|
|
|
|
js_contents += "throw new Error(#{message.to_json});"
|
|
|
|
end
|
|
|
|
|
|
|
|
if js_contents.nil?
|
|
|
|
# No backing class, use template-only
|
|
|
|
js_contents = <<~JS
|
|
|
|
import templateOnly from '@ember/component/template-only';
|
|
|
|
export default templateOnly();
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
|
|
|
js_contents = prefix + js_contents
|
|
|
|
|
|
|
|
tree[js_filename] = js_contents
|
|
|
|
tree.delete(filename)
|
|
|
|
end
|
|
|
|
|
2022-10-17 22:04:04 +08:00
|
|
|
# Transpile and write to output
|
|
|
|
tree.each_pair do |filename, content|
|
|
|
|
module_name, extension = filename.split(".", 2)
|
|
|
|
module_name = "test/#{module_name}" if for_tests
|
|
|
|
if extension == "js"
|
|
|
|
append_module(content, module_name)
|
|
|
|
elsif extension == "hbs"
|
|
|
|
append_ember_template(module_name, content)
|
|
|
|
elsif extension == "hbr"
|
|
|
|
append_raw_template(module_name.sub("discourse/templates/", ""), content)
|
|
|
|
else
|
2022-10-19 01:20:10 +08:00
|
|
|
append_js_error(filename, "unknown file extension '#{extension}' (#{filename})")
|
2022-10-17 22:04:04 +08:00
|
|
|
end
|
|
|
|
rescue CompileError => e
|
2022-10-19 01:20:10 +08:00
|
|
|
append_js_error filename, "#{e.message} (#{filename})"
|
2022-10-17 22:04:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-17 19:46:11 +08:00
|
|
|
def append_ember_template(name, hbs_template)
|
2022-10-19 01:20:10 +08:00
|
|
|
module_name = name
|
|
|
|
module_name = "/#{module_name}" if !module_name.start_with?("/")
|
|
|
|
module_name = "discourse/theme-#{@theme_id}#{module_name}"
|
2022-09-01 18:50:46 +08:00
|
|
|
|
|
|
|
# Mimics the ember-cli implementation
|
|
|
|
# https://github.com/ember-cli/ember-cli-htmlbars/blob/d5aa14b3/lib/template-compiler-plugin.js#L18-L26
|
|
|
|
script = <<~JS
|
|
|
|
import { hbs } from 'ember-cli-htmlbars';
|
|
|
|
export default hbs(#{hbs_template.to_json}, { moduleName: #{module_name.to_json} });
|
|
|
|
JS
|
|
|
|
|
|
|
|
template_module = DiscourseJsProcessor.transpile(script, "", module_name, theme_id: @theme_id)
|
2022-10-19 01:20:10 +08:00
|
|
|
@output_tree << ["#{name}.js", <<~JS]
|
2022-09-01 18:50:46 +08:00
|
|
|
if ('define' in window) {
|
|
|
|
#{template_module}
|
|
|
|
}
|
2019-01-17 19:46:11 +08:00
|
|
|
JS
|
2022-09-01 18:50:46 +08:00
|
|
|
rescue MiniRacer::RuntimeError, DiscourseJsProcessor::TranspileError => ex
|
|
|
|
raise CompileError.new ex.message
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|
|
|
|
|
2020-03-07 00:35:52 +08:00
|
|
|
def raw_template_name(name)
|
|
|
|
name = name.sub(/\.(raw|hbr)$/, '')
|
|
|
|
name.inspect
|
|
|
|
end
|
|
|
|
|
2019-01-17 19:46:11 +08:00
|
|
|
def append_raw_template(name, hbs_template)
|
2022-09-01 18:50:46 +08:00
|
|
|
compiled = DiscourseJsProcessor::Transpiler.new.compile_raw_template(hbs_template, theme_id: @theme_id)
|
2022-10-19 01:20:10 +08:00
|
|
|
source_for_comment = hbs_template.gsub("*/", '*\/').indent(4, ' ')
|
|
|
|
@output_tree << ["#{name}.js", <<~JS]
|
2019-01-17 19:46:11 +08:00
|
|
|
(function() {
|
2022-10-19 01:20:10 +08:00
|
|
|
/*
|
|
|
|
#{source_for_comment}
|
|
|
|
*/
|
2020-05-06 00:15:03 +08:00
|
|
|
const addRawTemplate = requirejs('discourse-common/lib/raw-templates').addRawTemplate;
|
|
|
|
const template = requirejs('discourse-common/lib/raw-handlebars').template(#{compiled});
|
|
|
|
addRawTemplate(#{raw_template_name(name)}, template);
|
2019-01-17 19:46:11 +08:00
|
|
|
})();
|
|
|
|
JS
|
2022-09-01 18:50:46 +08:00
|
|
|
rescue MiniRacer::RuntimeError, DiscourseJsProcessor::TranspileError => ex
|
|
|
|
raise CompileError.new ex.message
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|
|
|
|
|
2022-10-19 01:20:10 +08:00
|
|
|
def append_raw_script(filename, script)
|
|
|
|
@output_tree << [filename, script + "\n"]
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|
|
|
|
|
2019-11-05 19:54:12 +08:00
|
|
|
def append_module(script, name, include_variables: true)
|
2022-10-19 01:20:10 +08:00
|
|
|
original_filename = name
|
2021-04-12 20:02:58 +08:00
|
|
|
name = "discourse/theme-#{@theme_id}/#{name.gsub(/^discourse\//, '')}"
|
2022-09-01 18:50:46 +08:00
|
|
|
|
2021-04-12 20:02:58 +08:00
|
|
|
script = "#{theme_settings}#{script}" if include_variables
|
2020-03-11 21:43:55 +08:00
|
|
|
transpiler = DiscourseJsProcessor::Transpiler.new
|
2022-10-19 01:20:10 +08:00
|
|
|
@output_tree << ["#{original_filename}.js", <<~JS]
|
2021-04-12 20:02:58 +08:00
|
|
|
if ('define' in window) {
|
2022-10-22 02:05:34 +08:00
|
|
|
#{transpiler.perform(script, "", name, theme_id: @theme_id).strip}
|
2021-04-12 20:02:58 +08:00
|
|
|
}
|
|
|
|
JS
|
2022-09-01 18:50:46 +08:00
|
|
|
rescue MiniRacer::RuntimeError, DiscourseJsProcessor::TranspileError => ex
|
2019-06-03 17:41:00 +08:00
|
|
|
raise CompileError.new ex.message
|
|
|
|
end
|
|
|
|
|
2022-10-19 01:20:10 +08:00
|
|
|
def append_js_error(filename, message)
|
2022-10-17 22:04:04 +08:00
|
|
|
message = "[THEME #{@theme_id} '#{@theme_name}'] Compile error: #{message}"
|
2022-10-19 01:20:10 +08:00
|
|
|
append_raw_script filename, "console.error(#{message.to_json});"
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-12 20:02:58 +08:00
|
|
|
def theme_settings
|
2019-06-03 17:41:00 +08:00
|
|
|
<<~JS
|
2021-04-12 20:02:58 +08:00
|
|
|
const settings = require("discourse/lib/theme-settings-store")
|
2019-06-03 17:41:00 +08:00
|
|
|
.getObjectForTheme(#{@theme_id});
|
|
|
|
const themePrefix = (key) => `theme_translations.#{@theme_id}.${key}`;
|
|
|
|
JS
|
|
|
|
end
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|