From 3e091fd02b9d27e80eac6528857dcb14d5b1d134 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 19 Nov 2021 14:06:29 +0100 Subject: [PATCH] [plugins] Fix bug where not all plugins are loaded --- quasar.conf.js | 2 +- src/boot/plugins.ts | 59 +++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/quasar.conf.js b/quasar.conf.js index 72e454b..2602cf5 100644 --- a/quasar.conf.js +++ b/quasar.conf.js @@ -86,7 +86,7 @@ module.exports = configure(function (/* ctx */) { const custom_plgns = require('./plugin.config.js') const required_plgns = require('./src/vendor-plugin.config.js') 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(',')) } } diff --git a/src/boot/plugins.ts b/src/boot/plugins.ts index db7f4d0..a3e6ba7 100644 --- a/src/boot/plugins.ts +++ b/src/boot/plugins.ts @@ -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 -const PLUGINS = { - context: Promise>>[ - /*INSERT_PLUGIN_LIST*/ - ], - plugins: new Map(), -}; +const PLUGINS = >>[ + /*INSERT_PLUGIN_LIST*/ +]; interface BackendPlugin { permissions: string[]; @@ -314,23 +298,34 @@ export default boot(async ({ router, app }) => { widgets: [], }; - const BreakError = {}; try { - PLUGINS.plugins.forEach((plugin, id) => { - if (!loadPlugin(loadedPlugins, plugin, backend)) { - void router.push({ name: 'error' }); + // Wait for all plugins to be loaded + const results = await Promise.allSettled(PLUGINS); - Notify.create({ - type: 'negative', - message: `Fehler beim Laden: Bitte wende dich an den Admin (error: PNF-${id}!`, - timeout: 10000, - progress: true, - }); - throw BreakError; + // Check if loaded successfully + results.forEach((result) => { + if (result.status === 'rejected') { + throw result.reason; + } else { + if ( + !( + validatePlugin(result.value.default) && + loadPlugin(loadedPlugins, result.value.default, backend) + ) + ) + throw result.value.default.id; } }); - } catch (e) { - if (e !== BreakError) throw e; + } catch (reason) { + const id = 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