DEV: Use .hbr for raw template file extension (#8883)

This commit is contained in:
Mark VanLandingham 2020-02-11 13:38:12 -06:00 committed by GitHub
parent 78a0ca53bf
commit 3e89774908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 26 additions and 7 deletions

View File

@ -82,7 +82,7 @@ class ThemeField < ActiveRecord::Base
doc.css('script[type="text/x-handlebars"]').each do |node|
name = node["name"] || node["data-template-name"] || "broken"
is_raw = name =~ /\.raw$/
is_raw = name =~ /\.(raw|hbr)$/
hbs_template = node.inner_html
begin
@ -138,7 +138,7 @@ class ThemeField < ActiveRecord::Base
js_compiler.append_module(content, filename)
when "hbs"
js_compiler.append_ember_template(filename.sub("discourse/templates/", ""), content)
when "raw.hbs"
when "hbr", "raw.hbs"
js_compiler.append_raw_template(filename.sub("discourse/templates/", ""), content)
else
raise ThemeJavascriptCompiler::CompileError.new(I18n.t("themes.compile_error.unrecognized_extension", extension: extension))

View File

@ -169,7 +169,7 @@ module Discourse
initializer :fix_sprockets_loose_file_searcher, after: :set_default_precompile do |app|
app.config.assets.precompile.delete(Sprockets::Railtie::LOOSE_APP_ASSETS)
start_path = ::Rails.root.join("app/assets").to_s
exclude = ['.es6', '.hbs', '.js', '.css', '']
exclude = ['.es6', '.hbs', '.hbr', '.js', '.css', '']
app.config.assets.precompile << lambda do |logical_path, filename|
filename.start_with?(start_path) &&
!exclude.include?(File.extname(logical_path))
@ -238,6 +238,8 @@ module Discourse
# Our templates shouldn't start with 'discourse/templates'
config.handlebars.templates_root = 'discourse/templates'
config.handlebars.raw_template_namespace = "Discourse.RAW_TEMPLATES"
Sprockets.register_mime_type 'text/x-handlebars', extensions: ['.hbr']
Sprockets.register_transformer 'text/x-handlebars', 'application/javascript', Ember::Handlebars::Template
require 'discourse_redis'
require 'logster/redis_store'

View File

@ -20,6 +20,9 @@
"app/assets/javascripts/discourse/templates/*.hbs": {
"command": "dtemplate"
},
"app/assets/javascripts/discourse/templates/*.hbr": {
"command": "dtemplate"
},
"app/assets/javascripts/discourse/views/*.js": {
"command": "dview"
},

View File

@ -14,6 +14,7 @@ class Autospec::ReloadCss
watch(/\.ca?ss\.erb$/)
watch(/\.s[ac]ss$/)
watch(/\.hbs$/)
watch(/\.hbr$/)
def self.message_bus
MessageBus::Instance.new.tap do |bus|

View File

@ -35,6 +35,7 @@ class DiscourseIIFE
return data if path =~ /\.shbrs/
return data if path =~ /\.hbrs/
return data if path =~ /\.hbs/
return data if path =~ /\.hbr/
return data if path =~ /discourse-loader/

View File

@ -152,7 +152,7 @@ class DiscoursePluginRegistry
end
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6|\.js\.no-module\.es6$/
HANDLEBARS_REGEX = /\.hbs$|\.js\.handlebars$/
HANDLEBARS_REGEX = /\.(hb[rs]|js\.handlebars)$/
def self.register_asset(asset, opts = nil, plugin_directory_name = nil)
if asset =~ JS_REGEX

View File

@ -75,4 +75,10 @@ class Ember::Handlebars::Template
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

View File

@ -497,10 +497,12 @@ class Plugin::Instance
root_path = "#{File.dirname(@path)}/assets/javascripts"
DiscoursePluginRegistry.register_glob(root_path, 'js.es6')
DiscoursePluginRegistry.register_glob(root_path, 'hbs')
DiscoursePluginRegistry.register_glob(root_path, 'hbr')
admin_path = "#{File.dirname(@path)}/admin/assets/javascripts"
DiscoursePluginRegistry.register_glob(admin_path, 'js.es6', admin: true)
DiscoursePluginRegistry.register_glob(admin_path, 'hbs', admin: true)
DiscoursePluginRegistry.register_glob(admin_path, 'hbr', admin: true)
end
self.instance_eval File.read(path), path
@ -643,7 +645,7 @@ class Plugin::Instance
Dir.glob("#{root_path}/**/*") do |f|
if File.directory?(f)
yield [f, true]
elsif f.to_s.ends_with?(".js.es6") || f.to_s.ends_with?(".hbs")
elsif f.to_s.ends_with?(".js.es6") || f.to_s.ends_with?(".hbs") || f.to_s.ends_with?(".hbr")
yield [f, false]
end
end

View File

@ -187,7 +187,9 @@ class ThemeJavascriptCompiler
end
def append_raw_template(name, hbs_template)
name = name.sub(/\.raw$/, '').inspect
name = name.inspect
name.sub!(/\.raw$/, '')
name.sub!(/\.hbr$/, '.hbs')
compiled = RawTemplatePrecompiler.new(@theme_id).compile(hbs_template)
@content << <<~JS
(function() {

View File

@ -175,7 +175,8 @@ HTML
theme = Fabricate(:theme)
js_field = theme.set_field(target: :extra_js, name: "discourse/controllers/discovery.js.es6", value: "import 'discourse/lib/ajax'; console.log('hello');")
hbs_field = theme.set_field(target: :extra_js, name: "discourse/templates/discovery.hbs", value: "{{hello-world}}")
raw_hbs_field = theme.set_field(target: :extra_js, name: "discourse/templates/discovery.raw.hbs", value: "{{hello-world}}")
raw_hbs_field = theme.set_field(target: :extra_js, name: "discourse/templates/discovery.hbr", value: "{{hello-world}}")
hbr_field = theme.set_field(target: :extra_js, name: "discourse/templates/other_discovery.hbr", value: "{{hello-world}}")
unknown_field = theme.set_field(target: :extra_js, name: "discourse/controllers/discovery.blah", value: "this wont work")
theme.save!
@ -195,6 +196,7 @@ HTML
expect(hbs_field.reload.value_baked).to include('Ember.TEMPLATES["discovery"]')
expect(raw_hbs_field.reload.value_baked).to include('Discourse.RAW_TEMPLATES["discovery"]')
expect(hbr_field.reload.value_baked).to include('Discourse.RAW_TEMPLATES["other_discovery"]')
expect(unknown_field.reload.value_baked).to eq("")
expect(unknown_field.reload.error).to eq(I18n.t("themes.compile_error.unrecognized_extension", extension: "blah"))