From 566793208ec932e608fc20bc2724625451cbb070 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 7 Dec 2022 14:24:03 +0000 Subject: [PATCH] DEV: Support colocation under `/admin` namespace in themes/plugins (#19353) --- .../colocated-template-compiler.js | 6 +++--- app/assets/javascripts/discourse-plugins/index.js | 10 +++++++++- lib/theme_javascript_compiler.rb | 4 +--- spec/lib/theme_javascript_compiler_spec.rb | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse-plugins/colocated-template-compiler.js b/app/assets/javascripts/discourse-plugins/colocated-template-compiler.js index 9cc4d462b21..04611e3697d 100644 --- a/app/assets/javascripts/discourse-plugins/colocated-template-compiler.js +++ b/app/assets/javascripts/discourse-plugins/colocated-template-compiler.js @@ -3,12 +3,12 @@ const ColocatedTemplateProcessor = require("ember-cli-htmlbars/lib/colocated-bro module.exports = class DiscoursePluginColocatedTemplateProcessor extends ( ColocatedTemplateProcessor ) { - constructor(tree, discoursePluginName) { + constructor(tree, rootName) { super(tree); - this.discoursePluginName = discoursePluginName; + this.rootName = rootName; } detectRootName() { - return `discourse/plugins/${this.discoursePluginName}/discourse`; + return this.rootName; } }; diff --git a/app/assets/javascripts/discourse-plugins/index.js b/app/assets/javascripts/discourse-plugins/index.js index 730f926472b..b4b25cfcc34 100644 --- a/app/assets/javascripts/discourse-plugins/index.js +++ b/app/assets/javascripts/discourse-plugins/index.js @@ -176,7 +176,15 @@ module.exports = { tree = RawHandlebarsCompiler(tree); - tree = new DiscoursePluginColocatedTemplateProcessor(tree, pluginName); + const colocateBase = `discourse/plugins/${pluginName}`; + tree = new DiscoursePluginColocatedTemplateProcessor( + tree, + `${colocateBase}/discourse` + ); + tree = new DiscoursePluginColocatedTemplateProcessor( + tree, + `${colocateBase}/admin` + ); tree = this.compileTemplates(tree); tree = this.processedAddonJsFiles(tree); diff --git a/lib/theme_javascript_compiler.rb b/lib/theme_javascript_compiler.rb index f4f7047fe2a..b8c9458a5ad 100644 --- a/lib/theme_javascript_compiler.rb +++ b/lib/theme_javascript_compiler.rb @@ -89,8 +89,6 @@ class ThemeJavascriptCompiler end def append_tree(tree, for_tests: false) - root_name = "discourse" - # Replace legacy extensions tree.transform_keys! do |filename| if filename.ends_with? ".js.es6" @@ -121,7 +119,7 @@ class ThemeJavascriptCompiler # Handle colocated components tree.dup.each_pair do |filename, content| - is_component_template = filename.end_with?(".hbs") && filename.start_with?("#{root_name}/components/") + is_component_template = filename.end_with?(".hbs") && filename.start_with?("discourse/components/", "admin/components/") next if !is_component_template template_contents = content diff --git a/spec/lib/theme_javascript_compiler_spec.rb b/spec/lib/theme_javascript_compiler_spec.rb index 8d94ed72d5b..6392b429981 100644 --- a/spec/lib/theme_javascript_compiler_spec.rb +++ b/spec/lib/theme_javascript_compiler_spec.rb @@ -110,6 +110,20 @@ RSpec.describe ThemeJavascriptCompiler do expect(compiler.raw_content).to include("setComponentTemplate") end + it "handles colocated admin components" do + compiler.append_tree( + { + "admin/components/mycomponent.js" => <<~JS, + import Component from "@glimmer/component"; + export default class MyComponent extends Component {} + JS + "admin/components/mycomponent.hbs" => "{{my-component-template}}" + } + ) + expect(compiler.raw_content).to include("__COLOCATED_TEMPLATE__ =") + expect(compiler.raw_content).to include("setComponentTemplate") + end + it "applies theme AST transforms to colocated components" do compiler = ThemeJavascriptCompiler.new(12345678910, 'my theme name') compiler.append_tree(