discourse/app/assets/javascripts/discourse-common/addon/lib/raw-templates.js
David Taylor c139767055
DEV: Remove Ember.TEMPLATES and centralize template resolution rules (#19220)
In the past, the result of template compilation would be stored directly in `Ember.TEMPLATES`. Following the move to more modern ember-cli-based compilation, templates are now compiled to es6 modules. To handle forward/backwards compatibility during these changes we had logic in `discourse-boot` which would extract templates from the es6 modules and store them into the legacy-style `Ember.TEMPLATES` object.

This commit removes that shim, and updates our resolver to fetch templates directly from es6 modules. This is closer to how 'vanilla' Ember handles template resolution. We still have a lot of discourse-specific logic, but now it is centralised in one location and should be easier to understand and normalize in future.

This commit should not introduce any behaviour change.
2022-11-29 10:24:35 +00:00

57 lines
1.4 KiB
JavaScript

import { getResolverOption } from "discourse-common/resolver";
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(findOutlets) {
let result = {};
findOutlets(
Object.keys(__DISCOURSE_RAW_TEMPLATES),
(outletName, resource) => {
result[outletName] ??= [];
result[outletName].push({
template: __DISCOURSE_RAW_TEMPLATES[resource],
});
}
);
return result;
}
export function eagerLoadRawTemplateModules() {
for (const [key, value] of Object.entries(requirejs.entries)) {
if (
key.includes("/templates/") &&
value.deps.includes("discourse-common/lib/raw-templates")
) {
require(key);
}
}
}