From ef5cb6e7edb1870ea0c0e74453f9f59ba5bc807f Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 10 Oct 2023 16:29:40 +0100 Subject: [PATCH] DEV: Support components under /index paths in themes/plugins (#23876) Normally, modules defined under `blah/index` can be imported as `blah`. This is also true of Ember resolver lookups - `` should resolve to the same as ``. This was working as expected in Discourse core, but we had not implemented the same in our custom resolver logic for themes/plugins. This commit implements the `/index` fallback, and adds a test for the behaviour. --- .../discourse-common/addon/resolver.js | 9 +++++++- .../tests/helpers/temporary-module-helper.js | 2 ++ .../tests/unit/ember/resolver-test.js | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse-common/addon/resolver.js b/app/assets/javascripts/discourse-common/addon/resolver.js index 6cf0b4627dd..f4f146506bc 100644 --- a/app/assets/javascripts/discourse-common/addon/resolver.js +++ b/app/assets/javascripts/discourse-common/addon/resolver.js @@ -147,7 +147,14 @@ function lookupModuleBySuffix(suffix) { } }); } - return moduleSuffixTrie.withSuffix(suffix, 1)[0]; + return ( + moduleSuffixTrie.withSuffix(suffix, 1)[0] || + moduleSuffixTrie.withSuffix(`${suffix}/index`, 1)[0] + ); +} + +export function expireModuleTrieCache() { + moduleSuffixTrie = null; } export function buildResolver(baseName) { diff --git a/app/assets/javascripts/discourse/tests/helpers/temporary-module-helper.js b/app/assets/javascripts/discourse/tests/helpers/temporary-module-helper.js index 77301ca8deb..a23a5e9c014 100644 --- a/app/assets/javascripts/discourse/tests/helpers/temporary-module-helper.js +++ b/app/assets/javascripts/discourse/tests/helpers/temporary-module-helper.js @@ -1,5 +1,6 @@ import DiscourseTemplateMap from "discourse-common/lib/discourse-template-map"; import { expireConnectorCache } from "discourse/lib/plugin-connectors"; +import { expireModuleTrieCache } from "discourse-common/resolver"; const modifications = []; @@ -21,6 +22,7 @@ export function registerTemporaryModule(moduleName, defaultExport) { define(moduleName, ["exports"], generateTemporaryModule(defaultExport)); modifications.push(modificationData); expireConnectorCache(); + expireModuleTrieCache(); DiscourseTemplateMap.setModuleNames(Object.keys(requirejs.entries)); } diff --git a/app/assets/javascripts/discourse/tests/unit/ember/resolver-test.js b/app/assets/javascripts/discourse/tests/unit/ember/resolver-test.js index a0525df913f..60d459465a9 100644 --- a/app/assets/javascripts/discourse/tests/unit/ember/resolver-test.js +++ b/app/assets/javascripts/discourse/tests/unit/ember/resolver-test.js @@ -12,6 +12,10 @@ function lookupTemplate(assert, name, expectedTemplate, message) { assert.strictEqual(result, expectedTemplate, message); } +function resolve(name) { + return resolver.resolve(name); +} + function setTemplates(templateModuleNames) { for (const name of templateModuleNames) { registerTemporaryModule(name, name); @@ -683,4 +687,21 @@ module("Unit | Ember | resolver", function (hooks) { "uses standard match when both exist" ); }); + + test("resolves plugin/theme components with and without /index", function (assert) { + registerTemporaryModule( + "discourse/plugins/my-fake-plugin/discourse/components/my-component", + "my-component" + ); + registerTemporaryModule( + "discourse/plugins/my-fake-plugin/discourse/components/my-second-component/index", + "my-second-component" + ); + + assert.strictEqual(resolve("component:my-component"), "my-component"); + assert.strictEqual( + resolve("component:my-second-component"), + "my-second-component" + ); + }); });