Start Discourse in an initializer (#9930)

* DEV: To be pedantic, there is more than EMBER in there now

* DEV: Use less globals. Have `Discourse` start in an initializer

* DEV: Remove another global
This commit is contained in:
Robin Ward 2020-05-29 14:37:02 -04:00 committed by GitHub
parent ecfce93f28
commit 2b2434b82d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 109 additions and 95 deletions

View File

@ -1,7 +1,7 @@
var define, requirejs;
(function() {
var EMBER_MODULES = {};
var JS_MODULES = {};
var ALIASES = {
"ember-addons/ember-computed-decorators":
"discourse-common/utils/decorators",
@ -11,7 +11,7 @@ var define, requirejs;
// In future versions of ember we don't need this
if (typeof Ember !== "undefined") {
EMBER_MODULES = {
JS_MODULES = {
jquery: { default: $ },
"@ember/array": {
default: Ember.Array,
@ -285,7 +285,7 @@ var define, requirejs;
name = "@ember/object";
}
var mod = EMBER_MODULES[name] || registry[name];
var mod = JS_MODULES[name] || registry[name];
if (!mod) {
throw new Error(
"Could not find module `" + name + "` imported from `" + origin + "`"
@ -308,8 +308,8 @@ var define, requirejs;
requirejs = require = function(name) {
name = transformForAliases(name);
if (EMBER_MODULES[name]) {
return EMBER_MODULES[name];
if (JS_MODULES[name]) {
return JS_MODULES[name];
}
var mod = registry[name];

View File

@ -0,0 +1,94 @@
import PreloadStore from "discourse/lib/preload-store";
import I18n from "I18n";
import Session from "discourse/models/session";
import RSVP from "rsvp";
import { isTesting } from "discourse-common/config/environment";
export default {
name: "discourse-bootstrap",
// The very first initializer to run
initialize(container, app) {
// Our test environment has its own bootstrap code
if (isTesting()) {
return;
}
const preloadedDataElement = document.getElementById("data-preloaded");
const setupData = document.getElementById("data-discourse-setup").dataset;
if (preloadedDataElement) {
const preloaded = JSON.parse(preloadedDataElement.dataset.preloaded);
Object.keys(preloaded).forEach(function(key) {
PreloadStore.store(key, JSON.parse(preloaded[key]));
if (setupData.debugPreloadedAppData === "true") {
/* eslint-disable no-console */
console.log(key, PreloadStore.get(key));
/* eslint-enable no-console */
}
});
}
app.CDN = setupData.cdn;
app.BaseUrl = setupData.baseUrl;
app.BaseUri = setupData.baseUri;
app.Environment = setupData.environment;
app.SiteSettings = PreloadStore.get("siteSettings");
app.ThemeSettings = PreloadStore.get("themeSettings");
app.LetterAvatarVersion = setupData.letterAvatarVersion;
app.MarkdownItURL = setupData.markdownItUrl;
app.ServiceWorkerURL = setupData.serviceWorkerUrl;
I18n.defaultLocale = setupData.defaultLocale;
window.Logster = window.Logster || {};
window.Logster.enabled = setupData.enableJsErrorReporting === "true";
app.set("assetVersion", setupData.assetVersion);
Session.currentProp(
"disableCustomCSS",
setupData.disableCustomCss === "true"
);
if (setupData.safeMode) {
Session.currentProp("safe_mode", setupData.safeMode);
}
app.HighlightJSPath = setupData.highlightJsPath;
app.SvgSpritePath = setupData.svgSpritePath;
if (app.Environment === "development") {
app.SvgIconList = setupData.svgIconList;
}
if (setupData.s3BaseUrl) {
app.S3CDN = setupData.s3Cdn;
app.S3BaseUrl = setupData.s3BaseUrl;
}
RSVP.configure("onerror", function(e) {
// Ignore TransitionAborted exceptions that bubble up
if (e && e.message === "TransitionAborted") {
return;
}
if (Discourse.Environment === "development") {
/* eslint-disable no-console */
if (e) {
if (e.message || e.stack) {
console.log(e.message);
console.log(e.stack);
} else {
console.log("Uncaught promise: ", e);
}
} else {
console.log("A promise failed but was not caught.");
}
/* eslint-enable no-console */
}
window.onerror(e && e.message, null, null, null, e);
});
}
};

View File

@ -15,6 +15,7 @@ const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"];
export default {
name: "inject-discourse-objects",
after: "discourse-bootstrap",
initialize(container, app) {
ALL_TARGETS.forEach(t => app.inject(t, "appEvents", "service:app-events"));

View File

@ -3,6 +3,7 @@ import { createWidget } from "discourse/widgets/widget";
import transformPost from "discourse/lib/transform-post";
import { Placeholder } from "discourse/lib/posts-with-placeholders";
import { addWidgetCleanCallback } from "discourse/components/mount-widget";
import { isTesting } from "discourse-common/config/environment";
let transformCallbacks = null;
export function postTransformCallbacks(transformed) {
@ -19,7 +20,7 @@ export function addPostTransformCallback(callback) {
transformCallbacks.push(callback);
}
const CLOAKING_ENABLED = !window.inTestEnv;
const CLOAKING_ENABLED = !isTesting();
const DAY = 1000 * 60 * 60 * 24;
const _dontCloak = {};

View File

@ -1,84 +0,0 @@
// discourse-skip-module
(function() {
const ps = require("discourse/lib/preload-store").default;
const preloadedDataElement = document.getElementById("data-preloaded");
const setupData = document.getElementById("data-discourse-setup").dataset;
const I18n = require("I18n").default;
if (preloadedDataElement) {
const preloaded = JSON.parse(preloadedDataElement.dataset.preloaded);
Object.keys(preloaded).forEach(function(key) {
ps.store(key, JSON.parse(preloaded[key]));
if (setupData.debugPreloadedAppData === "true") {
/* eslint-disable no-console */
console.log(key, ps.get(key));
/* eslint-enable no-console */
}
});
}
window.Logster = window.Logster || {};
window.Logster.enabled = setupData.enableJsErrorReporting === "true";
Discourse.CDN = setupData.cdn;
Discourse.BaseUrl = setupData.baseUrl;
Discourse.BaseUri = setupData.baseUri;
Discourse.Environment = setupData.environment;
Discourse.SiteSettings = ps.get("siteSettings");
Discourse.ThemeSettings = ps.get("themeSettings");
Discourse.LetterAvatarVersion = setupData.letterAvatarVersion;
Discourse.MarkdownItURL = setupData.markdownItUrl;
Discourse.ServiceWorkerURL = setupData.serviceWorkerUrl;
I18n.defaultLocale = setupData.defaultLocale;
Discourse.start();
Discourse.set("assetVersion", setupData.assetVersion);
const Session = require("discourse/models/session").default;
Session.currentProp(
"disableCustomCSS",
setupData.disableCustomCss === "true"
);
if (setupData.safeMode) {
Session.currentProp("safe_mode", setupData.safeMode);
}
Discourse.HighlightJSPath = setupData.highlightJsPath;
Discourse.SvgSpritePath = setupData.svgSpritePath;
if (Discourse.Environment === "development") {
Discourse.SvgIconList = setupData.svgIconList;
}
if (setupData.s3BaseUrl) {
Discourse.S3CDN = setupData.s3Cdn;
Discourse.S3BaseUrl = setupData.s3BaseUrl;
}
// eslint-disable-next-line
Ember.RSVP.configure("onerror", function(e) {
// Ignore TransitionAborted exceptions that bubble up
if (e && e.message === "TransitionAborted") {
return;
}
if (Discourse.Environment === "development") {
/* eslint-disable no-console */
if (e) {
if (e.message || e.stack) {
console.log(e.message);
console.log(e.stack);
} else {
console.log("Uncaught promise: ", e);
}
} else {
console.log("A promise failed but was not caught.");
}
/* eslint-enable no-console */
}
window.onerror(e && e.message, null, null, null, e);
});
})();

View File

@ -0,0 +1,4 @@
// discourse-skip-module
(function() {
Discourse.start();
})();

View File

@ -109,7 +109,7 @@
<% end %>
<div class="hidden" id="data-preloaded" data-preloaded="<%= preloaded_json %>"></div>
<%= preload_script "preload-application-data" %>
<%= preload_script "start-discourse" %>
<%= yield :data %>

View File

@ -154,7 +154,7 @@ module Discourse
service-worker.js
google-tag-manager.js
google-universal-analytics.js
preload-application-data.js
start-discourse.js
print-page.js
omniauth-complete.js
activate-account.js

View File

@ -48,7 +48,7 @@ class DiscourseJsProcessor
return false if relative_path.start_with?("#{js_root}/plugins/")
return true if %w(
preload-application-data
start-discourse
wizard-start
onpopstate-handler
google-tag-manager

View File

@ -53,8 +53,6 @@ sinon.config = {
useFakeServer: false
};
window.inTestEnv = true;
let MessageBus = require("message-bus-client").default;
// Stop the message bus so we don't get ajax calls