Fix patching pre-populated factories

This commit is contained in:
Nuckyz 2024-06-01 19:52:17 -03:00
parent 61e1eada01
commit 4e14232b5a
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9

View file

@ -88,7 +88,7 @@ define(Function.prototype, "O", {
// Patch the pre-populated factories // Patch the pre-populated factories
for (const id in this.m) { for (const id in this.m) {
if (updateExistingFactory(this.m, id, this.m[id])) { if (updateExistingFactory(this.m, id, this.m[id], true)) {
continue; continue;
} }
@ -150,11 +150,14 @@ function defineModulesFactoryGetter(id: PropertyKey, factory: PatchedModuleFacto
* @target The module factories where this new original factory is being set * @target The module factories where this new original factory is being set
* @param id The id of the module * @param id The id of the module
* @param newFactory The new original factory * @param newFactory The new original factory
* @param ignoreExistingInTarget Whether to ignore checking if the factory already exists in the moduleFactoriesTarget
* @returns Whether the original factory was updated, or false if it doesn't exist in any Webpack instance * @returns Whether the original factory was updated, or false if it doesn't exist in any Webpack instance
*/ */
function updateExistingFactory(target: AnyWebpackRequire["m"], id: PropertyKey, newFactory: ModuleFactory) { function updateExistingFactory(moduleFactoriesTarget: AnyWebpackRequire["m"], id: PropertyKey, newFactory: ModuleFactory, ignoreExistingInTarget: boolean = false) {
let existingFactory: TypedPropertyDescriptor<PatchedModuleFactory> | undefined; let existingFactory: TypedPropertyDescriptor<PatchedModuleFactory> | undefined;
for (const wreq of allWebpackInstances) { for (const wreq of allWebpackInstances) {
if (ignoreExistingInTarget && wreq.m === moduleFactoriesTarget) continue;
if (Reflect.getOwnPropertyDescriptor(wreq.m, id) != null) { if (Reflect.getOwnPropertyDescriptor(wreq.m, id) != null) {
existingFactory = Reflect.getOwnPropertyDescriptor(wreq.m, id); existingFactory = Reflect.getOwnPropertyDescriptor(wreq.m, id);
break; break;
@ -166,8 +169,8 @@ function updateExistingFactory(target: AnyWebpackRequire["m"], id: PropertyKey,
// So define the descriptor of it on this current Webpack instance, call Reflect.set with the new original, // So define the descriptor of it on this current Webpack instance, call Reflect.set with the new original,
// and let the correct logic apply (normal set, or defineModuleFactoryGetter setter) // and let the correct logic apply (normal set, or defineModuleFactoryGetter setter)
Reflect.defineProperty(target, id, existingFactory); Reflect.defineProperty(moduleFactoriesTarget, id, existingFactory);
return Reflect.set(target, id, newFactory, target); return Reflect.set(moduleFactoriesTarget, id, newFactory, moduleFactoriesTarget);
} }
return false; return false;