[plugins] Fix bug where not all plugins are loaded

This commit is contained in:
Ferdinand Thiessen 2021-11-19 14:06:29 +01:00
parent fca79c36ef
commit 3e091fd02b
2 changed files with 28 additions and 33 deletions

View File

@ -86,7 +86,7 @@ module.exports = configure(function (/* ctx */) {
const custom_plgns = require('./plugin.config.js') const custom_plgns = require('./plugin.config.js')
const required_plgns = require('./src/vendor-plugin.config.js') const required_plgns = require('./src/vendor-plugin.config.js')
return src.replace(/\/\* *INSERT_PLUGIN_LIST *\*\//, return src.replace(/\/\* *INSERT_PLUGIN_LIST *\*\//,
[...custom_plgns, ...required_plgns].map(v => `import("${v}").then(v => success(v)).catch(() => failure("${v}"))`) [...custom_plgns, ...required_plgns].map(v => `import("${v}").catch(() => "${v}")`)
.join(',')) .join(','))
} }
} }

View File

@ -21,26 +21,10 @@ function validatePlugin(plugin: FG_Plugin.Plugin) {
); );
} }
/* eslint-disable */
// This functions are used by webpack magic
// Called when import promise resolved
function success(value: ImportPlgn, path: string) {
if (validatePlugin(value.default)) PLUGINS.plugins.set(value.default.id, value.default);
else failure(path);
}
// Called when import promise rejected
function failure(path = 'unknown') {
console.error(`Plugin ${path} could not be found and not imported`);
}
/* eslint-enable */
// Here does some magic happens, WebPack will automatically replace the following comment with the import statements // Here does some magic happens, WebPack will automatically replace the following comment with the import statements
const PLUGINS = { const PLUGINS = <Array<Promise<ImportPlgn>>>[
context: <Array<() => Promise<ImportPlgn>>>[ /*INSERT_PLUGIN_LIST*/
/*INSERT_PLUGIN_LIST*/ ];
],
plugins: new Map<string, FG_Plugin.Plugin>(),
};
interface BackendPlugin { interface BackendPlugin {
permissions: string[]; permissions: string[];
@ -314,23 +298,34 @@ export default boot(async ({ router, app }) => {
widgets: [], widgets: [],
}; };
const BreakError = {};
try { try {
PLUGINS.plugins.forEach((plugin, id) => { // Wait for all plugins to be loaded
if (!loadPlugin(loadedPlugins, plugin, backend)) { const results = await Promise.allSettled(PLUGINS);
void router.push({ name: 'error' });
Notify.create({ // Check if loaded successfully
type: 'negative', results.forEach((result) => {
message: `Fehler beim Laden: Bitte wende dich an den Admin (error: PNF-${id}!`, if (result.status === 'rejected') {
timeout: 10000, throw <string>result.reason;
progress: true, } else {
}); if (
throw BreakError; !(
validatePlugin(result.value.default) &&
loadPlugin(loadedPlugins, result.value.default, backend)
)
)
throw result.value.default.id;
} }
}); });
} catch (e) { } catch (reason) {
if (e !== BreakError) throw e; const id = <string>reason;
void router.push({ name: 'error' });
Notify.create({
type: 'negative',
message: `Fehler beim Laden: Bitte wende dich an den Admin (error: PNF-${id}!`,
timeout: 10000,
progress: true,
});
} }
// Sort widgets by priority // Sort widgets by priority