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
|
2016-06-30 17:10:08 -04:00
|
|
|
[File.open("#{Rails.root}/vendor/assets/javascripts/handlebars.js"),
|
|
|
|
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
|
|
|
|
|
2020-04-29 12:18:21 -04:00
|
|
|
source = File.read("#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js")
|
2020-03-11 09:43:55 -04:00
|
|
|
transpiler = DiscourseJsProcessor::Transpiler.new(skip_module: true)
|
|
|
|
transpiled = transpiler.perform(source)
|
2016-06-30 17:10:08 -04:00
|
|
|
|
|
|
|
# very hacky but lets us use ES6. I'm ashamed of this code -RW
|
2020-04-30 16:41:02 -04:00
|
|
|
transpiled = transpiled[transpiled.index('var RawHandlebars = ')...transpiled.index('export ')]
|
2016-06-30 17:10:08 -04:00
|
|
|
|
2019-11-01 11:01:16 -04:00
|
|
|
@precompiler = StringIO.new <<~END
|
|
|
|
var __RawHandlebars;
|
|
|
|
(function() {
|
|
|
|
#{transpiled};
|
|
|
|
__RawHandlebars = RawHandlebars;
|
|
|
|
})();
|
|
|
|
|
|
|
|
Barber = {
|
|
|
|
precompile: function(string) {
|
|
|
|
return __RawHandlebars.precompile(string, false).toString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
END
|
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
|
|
|
|
def precompile_handlebars(string)
|
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
|
|
|
|
|
|
|
|
def compile_handlebars(string)
|
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
|
2016-04-17 22:47:52 -04:00
|
|
|
include Discourse::Ember::Handlebars::Helper
|
|
|
|
|
2016-10-25 17:53:03 -04: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-10-25 17:53:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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-10-25 17:53:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def global_template_target(namespace, module_name, config)
|
|
|
|
# We need this for backward-compatibility reasons.
|
2016-04-17 22:47:52 -04:00
|
|
|
# Plugins may not have an app subdirectory.
|
2016-10-25 17:53:03 -04:00
|
|
|
path = template_path(module_name, config).inspect.gsub('discourse/templates/', '')
|
2020-04-23 09:07:54 -04:00
|
|
|
|
2016-10-25 17:53:03 -04:00
|
|
|
"#{namespace}[#{path}]"
|
|
|
|
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
|