DEV: Remove Discourse. global variable usage in initializers

This commit is contained in:
Robin Ward 2020-05-05 14:14:38 -04:00
parent 2edee74718
commit 4cf3dbe3db

View File

@ -120,54 +120,38 @@ const Discourse = Application.extend(FocusEvent, {
return loginController.authenticationComplete(options); return loginController.authenticationComplete(options);
}, },
_prepareInitializer(moduleName) {
const module = requirejs(moduleName, null, null, true);
if (!module) {
throw new Error(moduleName + " must export an initializer.");
}
const init = module.default;
const oldInitialize = init.initialize;
init.initialize = () => oldInitialize.call(init, this.__container__, this);
return init;
},
// Start up the Discourse application by running all the initializers we've defined. // Start up the Discourse application by running all the initializers we've defined.
start() { start() {
$("noscript").remove(); $("noscript").remove();
Object.keys(requirejs._eak_seen).forEach(function(key) { Object.keys(requirejs._eak_seen).forEach(key => {
if (/\/pre\-initializers\//.test(key)) { if (/\/pre\-initializers\//.test(key)) {
const module = requirejs(key, null, null, true); this.initializer(this._prepareInitializer(key));
if (!module) { } else if (/\/initializers\//.test(key)) {
throw new Error(key + " must export an initializer."); this.instanceInitializer(this._prepareInitializer(key));
}
const init = module.default;
const oldInitialize = init.initialize;
init.initialize = function() {
oldInitialize.call(this, Discourse.__container__, Discourse);
};
Discourse.initializer(init);
}
});
Object.keys(requirejs._eak_seen).forEach(function(key) {
if (/\/initializers\//.test(key)) {
const module = requirejs(key, null, null, true);
if (!module) {
throw new Error(key + " must export an initializer.");
}
const init = module.default;
const oldInitialize = init.initialize;
init.initialize = function() {
oldInitialize.call(this, Discourse.__container__, Discourse);
};
Discourse.instanceInitializer(init);
} }
}); });
// Plugins that are registered via `<script>` tags. // Plugins that are registered via `<script>` tags.
const withPluginApi = requirejs("discourse/lib/plugin-api").withPluginApi; const withPluginApi = requirejs("discourse/lib/plugin-api").withPluginApi;
let initCount = 0; let initCount = 0;
_pluginCallbacks.forEach(function(cb) { _pluginCallbacks.forEach(cb => {
Discourse.instanceInitializer({ this.instanceInitializer({
name: "_discourse_plugin_" + ++initCount, name: `_discourse_plugin_${++initCount}`,
after: "inject-objects", after: "inject-objects",
initialize() { initialize: () => withPluginApi(cb.version, cb.code)
withPluginApi(cb.version, cb.code);
}
}); });
}); });
}, },