Previously we were relying on a highly-customized version of the unmaintained Barber gem for theme template compilation. This commit switches us to use our own DiscourseJsProcessor, which makes use of more modern patterns and will be easier to maintain going forward. In summary: - Refactors DiscourseJsProcessor to move multiline JS heredocs into a companion `discourse-js-processor.js` file - Use MiniRacer's `.call` method to avoid manually escaping JS strings - Move Theme template AST transformers into DiscourseJsProcessor, and formalise interface for extending RawHandlebars AST transformations - Update Ember template compilation to use a babel-based approach, just like Ember CLI. This gives each template its own ES6 module rather than directly assigning `Ember.TEMPLATES` values - Improve testing of template compilation (and move some tests from `theme_javascript_compiler_spec.rb` to `discourse_js_processor_spec.rb`
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
(function () {
|
|
if (window.unsupportedBrowser) {
|
|
throw "Unsupported browser detected";
|
|
}
|
|
|
|
// TODO: Remove this and have resolver find the templates
|
|
const discoursePrefix = "discourse/templates/";
|
|
const adminPrefix = "admin/templates/";
|
|
const wizardPrefix = "wizard/templates/";
|
|
const discoursePrefixLength = discoursePrefix.length;
|
|
|
|
const pluginRegex = /^discourse\/plugins\/([^\/]+)\//;
|
|
const themeRegex = /^discourse\/theme-([^\/]+)\//;
|
|
|
|
Object.keys(requirejs.entries).forEach(function (key) {
|
|
let templateKey;
|
|
let pluginName;
|
|
let themeId;
|
|
if (key.startsWith(discoursePrefix)) {
|
|
templateKey = key.slice(discoursePrefixLength);
|
|
} else if (key.startsWith(adminPrefix) || key.startsWith(wizardPrefix)) {
|
|
templateKey = key;
|
|
} else if (
|
|
(pluginName = key.match(pluginRegex)?.[1]) &&
|
|
key.includes("/templates/") &&
|
|
require(key).default.__id // really is a template
|
|
) {
|
|
// This logic mimics the old sprockets compilation system which used to
|
|
// output templates directly to `Ember.TEMPLATES` with this naming logic
|
|
templateKey = key.slice(`discourse/plugins/${pluginName}/`.length);
|
|
templateKey = templateKey.replace("discourse/templates/", "");
|
|
templateKey = `javascripts/${templateKey}`;
|
|
} else if (
|
|
(themeId = key.match(themeRegex)?.[1]) &&
|
|
key.includes("/templates/")
|
|
) {
|
|
// And likewise for themes - this mimics the old logic
|
|
templateKey = key.slice(`discourse/theme-${themeId}/`.length);
|
|
templateKey = templateKey.replace("discourse/templates/", "");
|
|
if (!templateKey.startsWith("javascripts/")) {
|
|
templateKey = `javascripts/${templateKey}`;
|
|
}
|
|
}
|
|
|
|
if (templateKey) {
|
|
Ember.TEMPLATES[templateKey] = require(key).default;
|
|
}
|
|
});
|
|
|
|
window.__widget_helpers = require("discourse-widget-hbs/helpers").default;
|
|
|
|
// TODO: Eliminate this global
|
|
window.virtualDom = require("virtual-dom");
|
|
|
|
let element = document.querySelector(
|
|
`meta[name="discourse/config/environment"]`
|
|
);
|
|
const config = JSON.parse(
|
|
decodeURIComponent(element.getAttribute("content"))
|
|
);
|
|
const event = new CustomEvent("discourse-booted", { detail: config });
|
|
document.dispatchEvent(event);
|
|
})();
|