From 1d2b8a43c06ba5c24301b2408f6457a1ff26f827 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:30:26 -0300 Subject: [PATCH] Decouple factoryListeners from patchFactory --- src/webpack/patchWebpack.ts | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index 8b3c12dde..fb18784db 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -84,6 +84,7 @@ define(Function.prototype, "O", { continue; } + notifyFactoryListeners(this.m[id]); defineModulesFactoryGetter(id, Settings.eagerPatches ? patchFactory(id, this.m[id]) : this.m[id]); } @@ -168,6 +169,20 @@ function updateExistingFactory(moduleFactoriesTarget: AnyWebpackRequire["m"], id return false; } +/** + * Notify all factory listeners + * @param factory The original factory to notify for + */ +function notifyFactoryListeners(factory: AnyModuleFactory) { + for (const factoryListener of factoryListeners) { + try { + factoryListener(factory); + } catch (err) { + logger.error("Error in Webpack factory listener:\n", err, factoryListener); + } + } +} + const moduleFactoriesHandler: ProxyHandler = { /* If Discord ever decides to set module factories using the variable of the modules object directly instead of wreq.m, we need to switch the proxy to the prototype @@ -195,18 +210,8 @@ const moduleFactoriesHandler: ProxyHandler = { return true; } - if (!Settings.eagerPatches) { - // eagerPatches are disabled, so the factory argument should be the original - defineModulesFactoryGetter(p, newValue); - return true; - } - - const patchedFactory = patchFactory(p, newValue); - - // If multiple Webpack instances exist, when new a new module is loaded, it will be set in all the module factories objects. - // Because patches are only executed once, we need to set the patched version in all of them, to avoid the Webpack instance - // that uses the factory to contain the original factory instead of the patched, in case it was set first in another instance - defineModulesFactoryGetter(p, patchedFactory); + notifyFactoryListeners(newValue); + defineModulesFactoryGetter(p, Settings.eagerPatches ? patchFactory(p, newValue) : newValue); return true; } @@ -223,14 +228,6 @@ const moduleFactoriesHandler: ProxyHandler = { function patchFactory(id: PropertyKey, factory: AnyModuleFactory) { const originalFactory = factory; - for (const factoryListener of factoryListeners) { - try { - factoryListener(originalFactory); - } catch (err) { - logger.error("Error in Webpack factory listener:\n", err, factoryListener); - } - } - const patchedBy = new Set(); // 0, prefix to turn it into an expression: 0,function(){} would be invalid syntax without the 0,