From 3e897749084faeb29eb9a524d3f3b3f1d46a8738 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Tue, 11 Feb 2020 13:38:12 -0600 Subject: [PATCH] DEV: Use .hbr for raw template file extension (#8883) --- ...autocomplete.raw.hbs => badge-selector-autocomplete.hbr} | 0 ...ocomplete.raw.hbs => category-selector-autocomplete.hbr} | 0 ...g-autocomplete.raw.hbs => category-tag-autocomplete.hbr} | 0 ...{emoji-picker-recent.raw.hbs => emoji-picker-recent.hbr} | 0 .../{emoji-picker.raw.hbs.erb => emoji-picker.hbr.erb} | 0 ...autocomplete.raw.hbs => emoji-selector-autocomplete.hbr} | 0 .../templates/{flat-button.raw.hbs => flat-button.hbr} | 0 ...autocomplete.raw.hbs => group-selector-autocomplete.hbr} | 0 .../templates/list/{action-list.raw.hbs => action-list.hbr} | 0 .../list/{activity-column.raw.hbs => activity-column.hbr} | 0 .../list/{category-column.raw.hbs => category-column.hbr} | 0 ...ost-count-or-badges.raw.hbs => post-count-or-badges.hbr} | 0 .../list/{posters-column.raw.hbs => posters-column.hbr} | 0 .../{posts-count-column.raw.hbs => posts-count-column.hbr} | 0 .../list/{topic-excerpt.raw.hbs => topic-excerpt.hbr} | 0 .../list/{topic-list-item.raw.hbs => topic-list-item.hbr} | 0 .../list/{unread-indicator.raw.hbs => unread-indicator.hbr} | 0 .../list/{visited-line.raw.hbs => visited-line.hbr} | 0 .../list/{topic-list-item.raw.hbs => topic-list-item.hbr} | 0 .../post/{poster-avatar.raw.hbs => poster-avatar.hbr} | 0 ...t-header-column.raw.hbs => topic-list-header-column.hbr} | 0 .../{topic-list-header.raw.hbs => topic-list-header.hbr} | 0 .../{topic-post-badges.raw.hbs => topic-post-badges.hbr} | 0 .../templates/{topic-status.raw.hbs => topic-status.hbr} | 0 ...-autocomplete.raw.hbs => user-selector-autocomplete.hbr} | 0 app/models/theme_field.rb | 4 ++-- config/application.rb | 4 +++- config/projections.json | 3 +++ lib/autospec/reload_css.rb | 1 + lib/discourse_iife.rb | 1 + lib/discourse_plugin_registry.rb | 2 +- lib/freedom_patches/raw_handlebars.rb | 6 ++++++ lib/plugin/instance.rb | 4 +++- lib/theme_javascript_compiler.rb | 4 +++- spec/models/theme_field_spec.rb | 4 +++- 35 files changed, 26 insertions(+), 7 deletions(-) rename app/assets/javascripts/discourse/templates/{badge-selector-autocomplete.raw.hbs => badge-selector-autocomplete.hbr} (100%) rename app/assets/javascripts/discourse/templates/{category-selector-autocomplete.raw.hbs => category-selector-autocomplete.hbr} (100%) rename app/assets/javascripts/discourse/templates/{category-tag-autocomplete.raw.hbs => category-tag-autocomplete.hbr} (100%) rename app/assets/javascripts/discourse/templates/{emoji-picker-recent.raw.hbs => emoji-picker-recent.hbr} (100%) rename app/assets/javascripts/discourse/templates/{emoji-picker.raw.hbs.erb => emoji-picker.hbr.erb} (100%) rename app/assets/javascripts/discourse/templates/{emoji-selector-autocomplete.raw.hbs => emoji-selector-autocomplete.hbr} (100%) rename app/assets/javascripts/discourse/templates/{flat-button.raw.hbs => flat-button.hbr} (100%) rename app/assets/javascripts/discourse/templates/{group-selector-autocomplete.raw.hbs => group-selector-autocomplete.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{action-list.raw.hbs => action-list.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{activity-column.raw.hbs => activity-column.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{category-column.raw.hbs => category-column.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{post-count-or-badges.raw.hbs => post-count-or-badges.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{posters-column.raw.hbs => posters-column.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{posts-count-column.raw.hbs => posts-count-column.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{topic-excerpt.raw.hbs => topic-excerpt.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{topic-list-item.raw.hbs => topic-list-item.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{unread-indicator.raw.hbs => unread-indicator.hbr} (100%) rename app/assets/javascripts/discourse/templates/list/{visited-line.raw.hbs => visited-line.hbr} (100%) rename app/assets/javascripts/discourse/templates/mobile/list/{topic-list-item.raw.hbs => topic-list-item.hbr} (100%) rename app/assets/javascripts/discourse/templates/post/{poster-avatar.raw.hbs => poster-avatar.hbr} (100%) rename app/assets/javascripts/discourse/templates/{topic-list-header-column.raw.hbs => topic-list-header-column.hbr} (100%) rename app/assets/javascripts/discourse/templates/{topic-list-header.raw.hbs => topic-list-header.hbr} (100%) rename app/assets/javascripts/discourse/templates/{topic-post-badges.raw.hbs => topic-post-badges.hbr} (100%) rename app/assets/javascripts/discourse/templates/{topic-status.raw.hbs => topic-status.hbr} (100%) rename app/assets/javascripts/discourse/templates/{user-selector-autocomplete.raw.hbs => user-selector-autocomplete.hbr} (100%) diff --git a/app/assets/javascripts/discourse/templates/badge-selector-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/badge-selector-autocomplete.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/badge-selector-autocomplete.raw.hbs rename to app/assets/javascripts/discourse/templates/badge-selector-autocomplete.hbr diff --git a/app/assets/javascripts/discourse/templates/category-selector-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/category-selector-autocomplete.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/category-selector-autocomplete.raw.hbs rename to app/assets/javascripts/discourse/templates/category-selector-autocomplete.hbr diff --git a/app/assets/javascripts/discourse/templates/category-tag-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/category-tag-autocomplete.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/category-tag-autocomplete.raw.hbs rename to app/assets/javascripts/discourse/templates/category-tag-autocomplete.hbr diff --git a/app/assets/javascripts/discourse/templates/emoji-picker-recent.raw.hbs b/app/assets/javascripts/discourse/templates/emoji-picker-recent.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/emoji-picker-recent.raw.hbs rename to app/assets/javascripts/discourse/templates/emoji-picker-recent.hbr diff --git a/app/assets/javascripts/discourse/templates/emoji-picker.raw.hbs.erb b/app/assets/javascripts/discourse/templates/emoji-picker.hbr.erb similarity index 100% rename from app/assets/javascripts/discourse/templates/emoji-picker.raw.hbs.erb rename to app/assets/javascripts/discourse/templates/emoji-picker.hbr.erb diff --git a/app/assets/javascripts/discourse/templates/emoji-selector-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/emoji-selector-autocomplete.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/emoji-selector-autocomplete.raw.hbs rename to app/assets/javascripts/discourse/templates/emoji-selector-autocomplete.hbr diff --git a/app/assets/javascripts/discourse/templates/flat-button.raw.hbs b/app/assets/javascripts/discourse/templates/flat-button.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/flat-button.raw.hbs rename to app/assets/javascripts/discourse/templates/flat-button.hbr diff --git a/app/assets/javascripts/discourse/templates/group-selector-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/group-selector-autocomplete.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/group-selector-autocomplete.raw.hbs rename to app/assets/javascripts/discourse/templates/group-selector-autocomplete.hbr diff --git a/app/assets/javascripts/discourse/templates/list/action-list.raw.hbs b/app/assets/javascripts/discourse/templates/list/action-list.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/action-list.raw.hbs rename to app/assets/javascripts/discourse/templates/list/action-list.hbr diff --git a/app/assets/javascripts/discourse/templates/list/activity-column.raw.hbs b/app/assets/javascripts/discourse/templates/list/activity-column.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/activity-column.raw.hbs rename to app/assets/javascripts/discourse/templates/list/activity-column.hbr diff --git a/app/assets/javascripts/discourse/templates/list/category-column.raw.hbs b/app/assets/javascripts/discourse/templates/list/category-column.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/category-column.raw.hbs rename to app/assets/javascripts/discourse/templates/list/category-column.hbr diff --git a/app/assets/javascripts/discourse/templates/list/post-count-or-badges.raw.hbs b/app/assets/javascripts/discourse/templates/list/post-count-or-badges.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/post-count-or-badges.raw.hbs rename to app/assets/javascripts/discourse/templates/list/post-count-or-badges.hbr diff --git a/app/assets/javascripts/discourse/templates/list/posters-column.raw.hbs b/app/assets/javascripts/discourse/templates/list/posters-column.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/posters-column.raw.hbs rename to app/assets/javascripts/discourse/templates/list/posters-column.hbr diff --git a/app/assets/javascripts/discourse/templates/list/posts-count-column.raw.hbs b/app/assets/javascripts/discourse/templates/list/posts-count-column.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/posts-count-column.raw.hbs rename to app/assets/javascripts/discourse/templates/list/posts-count-column.hbr diff --git a/app/assets/javascripts/discourse/templates/list/topic-excerpt.raw.hbs b/app/assets/javascripts/discourse/templates/list/topic-excerpt.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/topic-excerpt.raw.hbs rename to app/assets/javascripts/discourse/templates/list/topic-excerpt.hbr diff --git a/app/assets/javascripts/discourse/templates/list/topic-list-item.raw.hbs b/app/assets/javascripts/discourse/templates/list/topic-list-item.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/topic-list-item.raw.hbs rename to app/assets/javascripts/discourse/templates/list/topic-list-item.hbr diff --git a/app/assets/javascripts/discourse/templates/list/unread-indicator.raw.hbs b/app/assets/javascripts/discourse/templates/list/unread-indicator.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/unread-indicator.raw.hbs rename to app/assets/javascripts/discourse/templates/list/unread-indicator.hbr diff --git a/app/assets/javascripts/discourse/templates/list/visited-line.raw.hbs b/app/assets/javascripts/discourse/templates/list/visited-line.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/list/visited-line.raw.hbs rename to app/assets/javascripts/discourse/templates/list/visited-line.hbr diff --git a/app/assets/javascripts/discourse/templates/mobile/list/topic-list-item.raw.hbs b/app/assets/javascripts/discourse/templates/mobile/list/topic-list-item.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/mobile/list/topic-list-item.raw.hbs rename to app/assets/javascripts/discourse/templates/mobile/list/topic-list-item.hbr diff --git a/app/assets/javascripts/discourse/templates/post/poster-avatar.raw.hbs b/app/assets/javascripts/discourse/templates/post/poster-avatar.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/post/poster-avatar.raw.hbs rename to app/assets/javascripts/discourse/templates/post/poster-avatar.hbr diff --git a/app/assets/javascripts/discourse/templates/topic-list-header-column.raw.hbs b/app/assets/javascripts/discourse/templates/topic-list-header-column.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/topic-list-header-column.raw.hbs rename to app/assets/javascripts/discourse/templates/topic-list-header-column.hbr diff --git a/app/assets/javascripts/discourse/templates/topic-list-header.raw.hbs b/app/assets/javascripts/discourse/templates/topic-list-header.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/topic-list-header.raw.hbs rename to app/assets/javascripts/discourse/templates/topic-list-header.hbr diff --git a/app/assets/javascripts/discourse/templates/topic-post-badges.raw.hbs b/app/assets/javascripts/discourse/templates/topic-post-badges.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/topic-post-badges.raw.hbs rename to app/assets/javascripts/discourse/templates/topic-post-badges.hbr diff --git a/app/assets/javascripts/discourse/templates/topic-status.raw.hbs b/app/assets/javascripts/discourse/templates/topic-status.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/topic-status.raw.hbs rename to app/assets/javascripts/discourse/templates/topic-status.hbr diff --git a/app/assets/javascripts/discourse/templates/user-selector-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/user-selector-autocomplete.hbr similarity index 100% rename from app/assets/javascripts/discourse/templates/user-selector-autocomplete.raw.hbs rename to app/assets/javascripts/discourse/templates/user-selector-autocomplete.hbr diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb index 218c880c0fd..ef98ea03900 100644 --- a/app/models/theme_field.rb +++ b/app/models/theme_field.rb @@ -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)) diff --git a/config/application.rb b/config/application.rb index b0d334bb0d5..52820120be0 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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' diff --git a/config/projections.json b/config/projections.json index 74498635850..7f49f292172 100644 --- a/config/projections.json +++ b/config/projections.json @@ -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" }, diff --git a/lib/autospec/reload_css.rb b/lib/autospec/reload_css.rb index 0e9da1da35b..54753e22ff5 100644 --- a/lib/autospec/reload_css.rb +++ b/lib/autospec/reload_css.rb @@ -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| diff --git a/lib/discourse_iife.rb b/lib/discourse_iife.rb index 946aa2f59f9..4b702d55ff4 100644 --- a/lib/discourse_iife.rb +++ b/lib/discourse_iife.rb @@ -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/ diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 800b9364c10..0a604ef2d1c 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -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 diff --git a/lib/freedom_patches/raw_handlebars.rb b/lib/freedom_patches/raw_handlebars.rb index d607a3c46f3..804d66f4109 100644 --- a/lib/freedom_patches/raw_handlebars.rb +++ b/lib/freedom_patches/raw_handlebars.rb @@ -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 diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 9c75950ede3..b810a9dd78a 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -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 diff --git a/lib/theme_javascript_compiler.rb b/lib/theme_javascript_compiler.rb index 5371fa1e77d..23650434176 100644 --- a/lib/theme_javascript_compiler.rb +++ b/lib/theme_javascript_compiler.rb @@ -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() { diff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rb index c6fc9c5764e..28d2e3de140 100644 --- a/spec/models/theme_field_spec.rb +++ b/spec/models/theme_field_spec.rb @@ -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"))