mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 10:30:01 +08:00
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:
parent
3f8a85ed49
commit
ef5cb6e7ed
|
@ -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) {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user