From 4c2f4d23846527ac711c27ab0f31b687d66c28f0 Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+samtv12345@users.noreply.github.com> Date: Sat, 13 Jul 2024 21:45:25 +0200 Subject: [PATCH] Added support for plugins --- src/node/hooks/express/specialpages.ts | 1 + src/static/js/pluginfw/shared.js | 8 ++++---- src/templates/padBootstrap.js | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/node/hooks/express/specialpages.ts b/src/node/hooks/express/specialpages.ts index 714d7b3dd..0905abf03 100644 --- a/src/node/hooks/express/specialpages.ts +++ b/src/node/hooks/express/specialpages.ts @@ -90,6 +90,7 @@ exports.expressCreateServer = async (hookName: string, args: any, cb: Function) const pluginModules = new Set(); for (const part of plugins.parts) { for (const [, hookFnName] of Object.entries(part.client_hooks || {})) { + console.log(hookFnName.split(':')[0]) pluginModules.add(hookFnName.split(':')[0]); } } diff --git a/src/static/js/pluginfw/shared.js b/src/static/js/pluginfw/shared.js index 2c81ccd81..7d58412c0 100644 --- a/src/static/js/pluginfw/shared.js +++ b/src/static/js/pluginfw/shared.js @@ -9,7 +9,7 @@ const disabledHookReasons = { }, }; -const loadFn = (path, hookName) => { +const loadFn = (path, hookName, modules) => { let functionName; const parts = path.split(':'); @@ -24,7 +24,7 @@ const loadFn = (path, hookName) => { functionName = parts[1]; } - let fn = require(path); + let fn = modules ? modules.get(path) : require(/* webpackIgnore: true */ path); functionName = functionName ? functionName : hookName; for (const name of functionName.split('.')) { @@ -33,7 +33,7 @@ const loadFn = (path, hookName) => { return fn; }; -const extractHooks = (parts, hookSetName, normalizer) => { +const extractHooks = (parts, hookSetName, normalizer, modules) => { const hooks = {}; for (const part of parts) { for (const [hookName, regHookFnName] of Object.entries(part[hookSetName] || {})) { @@ -53,7 +53,7 @@ const extractHooks = (parts, hookSetName, normalizer) => { } let hookFn; try { - hookFn = loadFn(hookFnName, hookName); + hookFn = loadFn(hookFnName, hookName, modules); if (!hookFn) throw new Error('Not a function'); } catch (err) { console.error(`Failed to load hook function "${hookFnName}" for plugin "${part.plugin}" ` + diff --git a/src/templates/padBootstrap.js b/src/templates/padBootstrap.js index 77861758c..62dd44b58 100644 --- a/src/templates/padBootstrap.js +++ b/src/templates/padBootstrap.js @@ -27,7 +27,7 @@ window.plugins.baseURL = basePath; await window.plugins.update(new Map([ <% for (const module of pluginModules) { %> - [<%- JSON.stringify(module) %>, require(<%- JSON.stringify(module) %>)], + [<%- JSON.stringify(module) %>, require("../../src/plugin_packages/"+<%- JSON.stringify(module) %>)], <% } %> ])); // Mechanism for tests to register hook functions (install fake plugins).