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 - `<MyComponent />` should resolve to the same as `<MyComponent::Index />`. 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.
This commit is contained in:
David Taylor 2023-10-10 16:29:40 +01:00 committed by GitHub
parent 3f8a85ed49
commit ef5cb6e7ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -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) {

View File

@ -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));
}

View File

@ -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"
);
});
});