mirror of
https://github.com/discourse/discourse.git
synced 2024-12-20 19:03:47 +08:00
16c6ab8661
Previously we were discovering plugin outlets by checking first for dedicated template files, and then looking for classes to match them. This doesn't work for components which are entirely defined in JS (e.g. those authored with gjs, or those which are re-exports of a colocated component). This commit refactors our detection logic to look for both class and template modules in a single pass. It also refactors things so that the modules themselves are required lazily when needd, rather than all being loaded during app boot.
58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
import { getResolverOption } from "discourse-common/resolver";
|
|
import require from "require";
|
|
|
|
export const __DISCOURSE_RAW_TEMPLATES = {};
|
|
|
|
export function addRawTemplate(name, template, opts = {}) {
|
|
// Core templates should never overwrite themes / plugins
|
|
if (opts.core && __DISCOURSE_RAW_TEMPLATES[name]) {
|
|
return;
|
|
}
|
|
__DISCOURSE_RAW_TEMPLATES[name] = template;
|
|
}
|
|
|
|
export function removeRawTemplate(name) {
|
|
delete __DISCOURSE_RAW_TEMPLATES[name];
|
|
}
|
|
|
|
export function findRawTemplate(name) {
|
|
if (getResolverOption("mobileView")) {
|
|
return (
|
|
__DISCOURSE_RAW_TEMPLATES[`javascripts/mobile/${name}`] ||
|
|
__DISCOURSE_RAW_TEMPLATES[`javascripts/${name}`] ||
|
|
__DISCOURSE_RAW_TEMPLATES[`mobile/${name}`] ||
|
|
__DISCOURSE_RAW_TEMPLATES[name]
|
|
);
|
|
}
|
|
|
|
return (
|
|
__DISCOURSE_RAW_TEMPLATES[`javascripts/${name}`] ||
|
|
__DISCOURSE_RAW_TEMPLATES[name]
|
|
);
|
|
}
|
|
|
|
export function buildRawConnectorCache() {
|
|
let result = {};
|
|
Object.keys(__DISCOURSE_RAW_TEMPLATES).forEach((resource) => {
|
|
const segments = resource.split("/");
|
|
const connectorIndex = segments.indexOf("connectors");
|
|
|
|
if (connectorIndex >= 0) {
|
|
const outletName = segments[connectorIndex + 1];
|
|
result[outletName] ??= [];
|
|
result[outletName].push({
|
|
template: __DISCOURSE_RAW_TEMPLATES[resource],
|
|
});
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
export function eagerLoadRawTemplateModules() {
|
|
for (const key of Object.keys(requirejs.entries)) {
|
|
if (key.includes("/raw-templates/")) {
|
|
require(key);
|
|
}
|
|
}
|
|
}
|