2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-10-29 00:24:51 -04:00
|
|
|
# barber patches to re-route raw compilation via ember compat handlebars
|
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
class Barber::Precompiler
|
|
|
|
def sources
|
2022-07-04 09:05:03 -04:00
|
|
|
[File.open("#{Rails.root}/app/assets/javascripts/node_modules/handlebars/dist/handlebars.js"),
|
2016-06-30 17:10:08 -04:00
|
|
|
precompiler]
|
2016-04-17 22:47:52 -04:00
|
|
|
end
|
2014-10-29 00:24:51 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def precompiler
|
2016-06-30 17:10:08 -04:00
|
|
|
if !@precompiler
|
2022-10-18 09:53:06 -04:00
|
|
|
loader = File.read("#{Rails.root}/app/assets/javascripts/node_modules/loader.js/dist/loader/loader.js")
|
2020-04-29 12:18:21 -04:00
|
|
|
source = File.read("#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js")
|
2016-06-30 17:10:08 -04:00
|
|
|
|
2022-08-09 06:53:24 -04:00
|
|
|
transpiled = DiscourseJsProcessor.transpile(source, "#{Rails.root}/app/assets/javascripts/", "discourse-common/lib/raw-handlebars")
|
2016-06-30 17:10:08 -04: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-02-28 14:50:55 -05:00
|
|
|
@precompiler = StringIO.new <<~JS
|
2022-08-09 06:53:24 -04: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 11:01:16 -04: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-02-28 14:50:55 -05:00
|
|
|
JS
|
2022-08-09 06:53:24 -04:00
|
|
|
|
2016-06-30 17:10:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@precompiler
|
2016-04-17 22:47:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Discourse
|
|
|
|
module Ember
|
|
|
|
module Handlebars
|
|
|
|
module Helper
|
2021-03-01 11:46:50 -05:00
|
|
|
def precompile_handlebars(string, input = nil)
|
2017-07-05 14:14:30 -04:00
|
|
|
"requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
|
2016-04-17 22:47:52 -04:00
|
|
|
end
|
|
|
|
|
2021-03-01 11:46:50 -05:00
|
|
|
def compile_handlebars(string, input = nil)
|
2017-07-05 14:14:30 -04:00
|
|
|
"requirejs('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
|
2016-04-17 22:47:52 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-29 00:24:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Ember::Handlebars::Template
|
2021-03-01 11:46:50 -05:00
|
|
|
prepend Discourse::Ember::Handlebars::Helper
|
2016-04-17 22:47:52 -04:00
|
|
|
|
2021-03-01 11:46:50 -05: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-25 17:53:03 -04:00
|
|
|
end
|
|
|
|
|
2021-03-01 11:46:50 -05:00
|
|
|
def global_template_target(namespace, module_name, config)
|
|
|
|
"#{namespace}[#{path_for(module_name, config)}]"
|
2016-10-25 17:53:03 -04:00
|
|
|
end
|
|
|
|
|
2021-03-01 11:46:50 -05: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 09:07:54 -04:00
|
|
|
|
2021-03-01 11:46:50 -05: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-02-28 14:50:55 -05:00
|
|
|
return <<~JS
|
2021-03-01 11:46:50 -05: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-02-28 14:50:55 -05:00
|
|
|
JS
|
2021-03-01 11:46:50 -05: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-25 17:53:03 -04:00
|
|
|
end
|
|
|
|
|
2016-04-17 22:47:52 -04: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 00:24:51 -04:00
|
|
|
end
|
2020-02-11 14:38:12 -05: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 00:24:51 -04:00
|
|
|
end
|