2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
class ThemeJavascriptCompiler
|
|
|
|
|
|
|
|
module PrecompilerExtension
|
|
|
|
def initialize(theme_id)
|
|
|
|
super()
|
|
|
|
@theme_id = theme_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def discourse_node_manipulator
|
|
|
|
<<~JS
|
|
|
|
function manipulateNode(node) {
|
2019-07-16 06:45:15 -04:00
|
|
|
// Magically add theme id as the first param for each of these helpers)
|
2019-02-06 16:09:21 -05:00
|
|
|
if (node.path.parts && ["theme-i18n", "theme-prefix", "theme-setting"].includes(node.path.parts[0])) {
|
2019-02-08 07:54:00 -05:00
|
|
|
if(node.params.length === 1){
|
|
|
|
node.params.unshift({
|
|
|
|
type: "NumberLiteral",
|
|
|
|
value: #{@theme_id},
|
2022-07-18 05:18:16 -04:00
|
|
|
original: #{@theme_id},
|
|
|
|
loc: { start: {}, end: {} }
|
2019-02-08 07:54:00 -05:00
|
|
|
})
|
|
|
|
}
|
2019-01-17 06:46:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
|
|
|
[super, discourse_node_manipulator, discourse_extension].join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RawTemplatePrecompiler < Barber::Precompiler
|
|
|
|
include PrecompilerExtension
|
|
|
|
|
|
|
|
def discourse_extension
|
|
|
|
<<~JS
|
|
|
|
let _superCompile = Handlebars.Compiler.prototype.compile;
|
|
|
|
Handlebars.Compiler.prototype.compile = function(program, options) {
|
|
|
|
[
|
2022-07-18 05:10:23 -04:00
|
|
|
"SubExpression",
|
|
|
|
"MustacheStatement"
|
2019-01-17 06:46:11 -05:00
|
|
|
].forEach((pass) => {
|
|
|
|
let visitor = new Handlebars.Visitor();
|
|
|
|
visitor.mutating = true;
|
2022-07-18 05:10:23 -04:00
|
|
|
visitor[pass] = manipulateNode;
|
2019-01-17 06:46:11 -05:00
|
|
|
visitor.accept(program);
|
|
|
|
})
|
|
|
|
|
|
|
|
return _superCompile.apply(this, arguments);
|
|
|
|
};
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EmberTemplatePrecompiler < Barber::Ember::Precompiler
|
|
|
|
include PrecompilerExtension
|
|
|
|
|
|
|
|
def discourse_extension
|
|
|
|
<<~JS
|
2022-07-18 05:18:16 -04:00
|
|
|
module.exports.registerPlugin('ast', function() {
|
2019-07-16 06:45:15 -04:00
|
|
|
return {
|
|
|
|
name: 'theme-template-manipulator',
|
|
|
|
visitor: {
|
|
|
|
SubExpression: manipulateNode,
|
2022-07-18 05:10:23 -04:00
|
|
|
MustacheStatement: manipulateNode
|
2019-07-16 06:45:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-01-17 06:46:11 -05:00
|
|
|
JS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class CompileError < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :content
|
|
|
|
|
2019-05-24 10:25:55 -04:00
|
|
|
def initialize(theme_id, theme_name)
|
2019-01-17 06:46:11 -05:00
|
|
|
@theme_id = theme_id
|
2019-05-02 18:17:27 -04:00
|
|
|
@content = +""
|
2019-05-24 10:25:55 -04:00
|
|
|
@theme_name = theme_name
|
2019-01-17 06:46:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepend_settings(settings_hash)
|
|
|
|
@content.prepend <<~JS
|
|
|
|
(function() {
|
2021-04-12 08:02:58 -04:00
|
|
|
if ('require' in window) {
|
|
|
|
require("discourse/lib/theme-settings-store").registerSettings(#{@theme_id}, #{settings_hash.to_json});
|
2019-01-17 06:46:11 -05:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO Error handling for handlebars templates
|
|
|
|
def append_ember_template(name, hbs_template)
|
2021-04-12 08:02:58 -04:00
|
|
|
if !name.start_with?("javascripts/")
|
|
|
|
prefix = "javascripts"
|
|
|
|
prefix += "/" if !name.start_with?("/")
|
|
|
|
name = prefix + name
|
|
|
|
end
|
2019-01-17 06:46:11 -05:00
|
|
|
name = name.inspect
|
|
|
|
compiled = EmberTemplatePrecompiler.new(@theme_id).compile(hbs_template)
|
2021-04-12 08:02:58 -04:00
|
|
|
# the `'Ember' in window` check is needed for no_ember pages
|
2019-01-17 06:46:11 -05:00
|
|
|
content << <<~JS
|
|
|
|
(function() {
|
|
|
|
if ('Ember' in window) {
|
|
|
|
Ember.TEMPLATES[#{name}] = Ember.HTMLBars.template(#{compiled});
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
JS
|
|
|
|
rescue Barber::PrecompilerError => e
|
|
|
|
raise CompileError.new e.instance_variable_get(:@error) # e.message contains the entire template, which could be very long
|
|
|
|
end
|
|
|
|
|
2020-03-06 11:35:52 -05:00
|
|
|
def raw_template_name(name)
|
|
|
|
name = name.sub(/\.(raw|hbr)$/, '')
|
|
|
|
name.inspect
|
|
|
|
end
|
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
def append_raw_template(name, hbs_template)
|
|
|
|
compiled = RawTemplatePrecompiler.new(@theme_id).compile(hbs_template)
|
|
|
|
@content << <<~JS
|
|
|
|
(function() {
|
2020-05-05 12:15:03 -04: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 06:46:11 -05:00
|
|
|
})();
|
|
|
|
JS
|
|
|
|
rescue Barber::PrecompilerError => e
|
|
|
|
raise CompileError.new e.instance_variable_get(:@error) # e.message contains the entire template, which could be very long
|
|
|
|
end
|
|
|
|
|
|
|
|
def append_raw_script(script)
|
|
|
|
@content << script + "\n"
|
|
|
|
end
|
|
|
|
|
2019-11-05 06:54:12 -05:00
|
|
|
def append_module(script, name, include_variables: true)
|
2021-04-12 08:02:58 -04:00
|
|
|
name = "discourse/theme-#{@theme_id}/#{name.gsub(/^discourse\//, '')}"
|
|
|
|
script = "#{theme_settings}#{script}" if include_variables
|
2020-03-11 09:43:55 -04:00
|
|
|
transpiler = DiscourseJsProcessor::Transpiler.new
|
2021-04-12 08:02:58 -04:00
|
|
|
@content << <<~JS
|
|
|
|
if ('define' in window) {
|
|
|
|
#{transpiler.perform(script, "", name).strip}
|
|
|
|
}
|
|
|
|
JS
|
2019-06-03 05:41:00 -04:00
|
|
|
rescue MiniRacer::RuntimeError => ex
|
|
|
|
raise CompileError.new ex.message
|
|
|
|
end
|
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
def append_js_error(message)
|
|
|
|
@content << "console.error('Theme Transpilation Error:', #{message.inspect});"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-12 08:02:58 -04:00
|
|
|
def theme_settings
|
2019-06-03 05:41:00 -04:00
|
|
|
<<~JS
|
2021-04-12 08:02:58 -04:00
|
|
|
const settings = require("discourse/lib/theme-settings-store")
|
2019-06-03 05:41:00 -04:00
|
|
|
.getObjectForTheme(#{@theme_id});
|
|
|
|
const themePrefix = (key) => `theme_translations.#{@theme_id}.${key}`;
|
|
|
|
JS
|
|
|
|
end
|
2019-01-17 06:46:11 -05:00
|
|
|
end
|