discourse/lib/freedom_patches/raw_handlebars.rb
Robin Ward a3f0543f99
Support for transpiling .js files (#9160)
* Remove some `.es6` from comments where it does not matter

* Use a post processor for transpilation

This will allow us to eventually use the directory structure to
transpile rather than the extension.

* FIX: Some errors and clean up in confirm-new-email

It would throw an error if the webauthn element wasn't present.
Also I changed things so that no-module is not explicitly
referenced.

* Remove `no-module`

Instead we allow a magic comment: `// discourse-skip-module` to prevent
the asset pipeline from creating a module.

* DEV: Enable babel transpilation based on directory

If it's in `app/assets/javascripts/dicourse` it will be transpiled
even without the `.es6` extension.

* REFACTOR: Remove Tilt/ES6ModuleTranspiler
2020-03-11 09:43:55 -04:00

85 lines
2.4 KiB
Ruby

# frozen_string_literal: true
# barber patches to re-route raw compilation via ember compat handlebars
class Barber::Precompiler
def sources
[File.open("#{Rails.root}/vendor/assets/javascripts/handlebars.js"),
precompiler]
end
def precompiler
if !@precompiler
source = File.read("#{Rails.root}/app/assets/javascripts/discourse-common/lib/raw-handlebars.js.es6")
transpiler = DiscourseJsProcessor::Transpiler.new(skip_module: true)
transpiled = transpiler.perform(source)
# very hacky but lets us use ES6. I'm ashamed of this code -RW
transpiled = transpiled[0...transpiled.index('export ')]
@precompiler = StringIO.new <<~END
var __RawHandlebars;
(function() {
#{transpiled};
__RawHandlebars = RawHandlebars;
})();
Barber = {
precompile: function(string) {
return __RawHandlebars.precompile(string, false).toString();
}
};
END
end
@precompiler
end
end
module Discourse
module Ember
module Handlebars
module Helper
def precompile_handlebars(string)
"requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
end
def compile_handlebars(string)
"requirejs('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
end
end
end
end
end
class Ember::Handlebars::Template
include Discourse::Ember::Handlebars::Helper
def precompile_handlebars(string, input = nil)
"requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
end
def compile_handlebars(string, input = nil)
"requirejs('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
end
def global_template_target(namespace, module_name, config)
"#{namespace}[#{template_path(module_name, config).inspect}]"
end
# 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
end
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
end