Skip to content

A platform for community discussion. Free, open, simple.

DEV: Test theme js compiler append_raw_template (#8950)

Mark VanLandingham475f545557bb
diff --git a/lib/theme_javascript_compiler.rb b/lib/theme_javascript_compiler.rb
index d7ea11362fd..b0906eb356a 100644
--- a/lib/theme_javascript_compiler.rb
+++ b/lib/theme_javascript_compiler.rb
@@ -186,15 +186,17 @@ class ThemeJavascriptCompiler
raise CompileError.new e.instance_variable_get(:@error) # e.message contains the entire template, which could be very long
end
+ def raw_template_name(name)
+ name = name.sub(/\.(raw|hbr)$/, '')
+ name.inspect
+ end
+
def append_raw_template(name, hbs_template)
- name = name.sub(/\.raw$/, '')
- name = name.sub(/\.hbr$/, '.hbs')
- name = name.inspect
compiled = RawTemplatePrecompiler.new(@theme_id).compile(hbs_template)
@content << <<~JS
(function() {
if ('Discourse' in window) {
- Discourse.RAW_TEMPLATES[#{name}] = requirejs('discourse-common/lib/raw-handlebars').template(#{compiled});
+ Discourse.RAW_TEMPLATES[#{raw_template_name(name)}] = requirejs('discourse-common/lib/raw-handlebars').template(#{compiled});
}
})();
JS
diff --git a/spec/lib/theme_javascript_compiler_spec.rb b/spec/lib/theme_javascript_compiler_spec.rb
index 911ce18c098..ca0c9cd0467 100644
--- a/spec/lib/theme_javascript_compiler_spec.rb
+++ b/spec/lib/theme_javascript_compiler_spec.rb
@@ -110,4 +110,21 @@ describe ThemeJavascriptCompiler do
end
end
+ describe "#append_raw_template" do
+ let(:compiler) { ThemeJavascriptCompiler.new(1, 'marks') }
+ it 'adds the correct template to "Discourse.RAW_TEMPLATES"' do
+ template = "<h1>hello</h1>"
+ name = "/path/to/templates1"
+ compiler.append_raw_template("#{name}.raw", template)
+ expect(compiler.content.to_s).to include("Discourse.RAW_TEMPLATES[\"#{name}\"]")
+
+ name = "/path/to/templates2"
+ compiler.append_raw_template("#{name}.hbr", template)
+ expect(compiler.content.to_s).to include("Discourse.RAW_TEMPLATES[\"#{name}\"]")
+
+ name = "/path/to/templates3"
+ compiler.append_raw_template("#{name}.hbs", template)
+ expect(compiler.content.to_s).to include("Discourse.RAW_TEMPLATES[\"#{name}.hbs\"]")
+ end
+ end
end