2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-10-29 12:24:51 +08:00
|
|
|
# barber patches to re-route raw compilation via ember compat handlebars
|
|
|
|
|
2016-04-18 10:47:52 +08:00
|
|
|
class Barber::Precompiler
|
|
|
|
def sources
|
2022-07-04 21:05:03 +08:00
|
|
|
[File.open("#{Rails.root}/app/assets/javascripts/node_modules/handlebars/dist/handlebars.js"),
|
2016-07-01 05:10:08 +08:00
|
|
|
precompiler]
|
2016-04-18 10:47:52 +08:00
|
|
|
end
|
2014-10-29 12:24:51 +08:00
|
|
|
|
2016-04-18 10:47:52 +08:00
|
|
|
def precompiler
|
2016-07-01 05:10:08 +08:00
|
|
|
if !@precompiler
|
2022-08-09 18:53:24 +08:00
|
|
|
loader = File.read("#{Rails.root}/app/assets/javascripts/mini-loader.js")
|
2020-04-30 00:18:21 +08:00
|
|
|
source = File.read("#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js")
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2022-08-09 18:53:24 +08:00
|
|
|
transpiled = DiscourseJsProcessor.transpile(source, "#{Rails.root}/app/assets/javascripts/", "discourse-common/lib/raw-handlebars")
|
2016-07-01 05:10:08 +08:00
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
@precompiler = StringIO.new <<~JS
|
2022-08-09 18:53:24 +08:00
|
|
|
let __RawHandlebars;
|
|
|
|
|
|
|
|
(function(){
|
|
|
|
#{loader}
|
|
|
|
define("handlebars", ["exports"], function(exports){ exports.default = Handlebars; })
|
|
|
|
#{transpiled}
|
|
|
|
__RawHandlebars = require("discourse-common/lib/raw-handlebars").default;
|
|
|
|
})()
|
2019-11-01 23:01:16 +08:00
|
|
|
|
|
|
|
Barber = {
|
|
|
|
precompile: function(string) {
|
|
|
|
return __RawHandlebars.precompile(string, false).toString();
|
|
|
|
}
|
|
|
|
};
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
JS
|
2022-08-09 18:53:24 +08:00
|
|
|
|
2016-07-01 05:10:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@precompiler
|
2016-04-18 10:47:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Discourse
|
|
|
|
module Ember
|
|
|
|
module Handlebars
|
|
|
|
module Helper
|
2021-03-02 00:46:50 +08:00
|
|
|
def precompile_handlebars(string, input = nil)
|
2017-07-06 02:14:30 +08:00
|
|
|
"requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
|
2016-04-18 10:47:52 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 00:46:50 +08:00
|
|
|
def compile_handlebars(string, input = nil)
|
2017-07-06 02:14:30 +08:00
|
|
|
"requirejs('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
|
2016-04-18 10:47:52 +08:00
|
|
|
end
|
|
|
|
end
|
2014-10-29 12:24:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Ember::Handlebars::Template
|
2021-03-02 00:46:50 +08:00
|
|
|
prepend Discourse::Ember::Handlebars::Helper
|
2016-04-18 10:47:52 +08:00
|
|
|
|
2021-03-02 00:46:50 +08:00
|
|
|
def path_for(module_name, config)
|
|
|
|
# We need this for backward-compatibility reasons.
|
|
|
|
# Plugins may not have an app subdirectory.
|
|
|
|
template_path(module_name, config).inspect.gsub('discourse/templates/', '')
|
2016-10-26 05:53:03 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 00:46:50 +08:00
|
|
|
def global_template_target(namespace, module_name, config)
|
|
|
|
"#{namespace}[#{path_for(module_name, config)}]"
|
2016-10-26 05:53:03 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 00:46:50 +08:00
|
|
|
def call(input)
|
|
|
|
data = input[:data]
|
|
|
|
filename = input[:filename]
|
|
|
|
|
|
|
|
raw = handlebars?(filename)
|
|
|
|
|
|
|
|
if raw
|
|
|
|
template = data
|
|
|
|
else
|
|
|
|
template = mustache_to_handlebars(filename, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
template_name = input[:name]
|
2020-04-23 21:07:54 +08:00
|
|
|
|
2021-03-02 00:46:50 +08:00
|
|
|
module_name =
|
|
|
|
case config.output_type
|
|
|
|
when :amd
|
|
|
|
amd_template_target(config.amd_namespace, template_name)
|
|
|
|
when :global
|
|
|
|
template_path(template_name, config)
|
|
|
|
else
|
|
|
|
raise "Unsupported `output_type`: #{config.output_type}"
|
|
|
|
end
|
|
|
|
|
|
|
|
meta = meta_supported? ? { moduleName: module_name } : false
|
|
|
|
|
|
|
|
if config.precompile
|
|
|
|
if raw
|
|
|
|
template = precompile_handlebars(template, input)
|
|
|
|
else
|
|
|
|
template = precompile_ember_handlebars(template, config.ember_template, input, meta)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if raw
|
|
|
|
template = compile_handlebars(data)
|
|
|
|
else
|
|
|
|
template = compile_ember_handlebars(template, config.ember_template, meta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case config.output_type
|
|
|
|
when :amd
|
|
|
|
"define('#{module_name}', ['exports'], function(__exports__){ __exports__['default'] = #{template} });"
|
|
|
|
when :global
|
|
|
|
if raw
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
return <<~JS
|
2021-03-02 00:46:50 +08:00
|
|
|
var __t = #{template};
|
|
|
|
requirejs('discourse-common/lib/raw-templates').addRawTemplate(#{path_for(template_name, config)}, __t);
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
JS
|
2021-03-02 00:46:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
target = global_template_target('Ember.TEMPLATES', template_name, config)
|
|
|
|
"#{target} = #{template}\n"
|
|
|
|
else
|
|
|
|
raise "Unsupported `output_type`: #{config.output_type}"
|
|
|
|
end
|
2016-10-26 05:53:03 +08:00
|
|
|
end
|
|
|
|
|
2016-04-18 10:47:52 +08:00
|
|
|
# FIXME: Previously, ember-handlebars-templates uses the logical path which incorrectly
|
|
|
|
# returned paths with the `.raw` extension and our code is depending on the `.raw`
|
|
|
|
# to find the right template to use.
|
|
|
|
def actual_name(input)
|
|
|
|
actual_name = input[:name]
|
|
|
|
input[:filename].include?('.raw') ? "#{actual_name}.raw" : actual_name
|
2014-10-29 12:24:51 +08:00
|
|
|
end
|
2020-02-12 03:38:12 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def handlebars?(filename)
|
|
|
|
filename.to_s =~ /\.raw\.(handlebars|hjs|hbs)/ || filename.to_s.ends_with?(".hbr") || filename.to_s.ends_with?(".hbr.erb")
|
|
|
|
end
|
2014-10-29 12:24:51 +08:00
|
|
|
end
|