From f294160dd747b4292a52fde65a5aa2a325525c2b Mon Sep 17 00:00:00 2001 From: Albert Portnoy Date: Fri, 1 Dec 2023 17:48:19 -0600 Subject: [PATCH] Fix missing wpRequire.c (#590) --- src/renderer/modules/webpack/get-modules.ts | 12 ++++++------ src/renderer/modules/webpack/patch-load.ts | 9 ++++++++- src/types/webpack.ts | 4 +++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/renderer/modules/webpack/get-modules.ts b/src/renderer/modules/webpack/get-modules.ts index 0e4200c46..8741b5062 100644 --- a/src/renderer/modules/webpack/get-modules.ts +++ b/src/renderer/modules/webpack/get-modules.ts @@ -1,5 +1,5 @@ import type { Filter, GetModuleOptions, RawModule } from "src/types"; -import { wpRequire } from "./patch-load"; +import { webpackChunks, wpRequire } from "./patch-load"; import { logError } from "./util"; // Export-finding utilities @@ -83,12 +83,12 @@ export function getById(id: number, raw?: boolean): T | RawModule | undefi export function getById(id: number, raw = false): T | RawModule | undefined { if (!wpRequire) throw new Error("Webpack not initialized"); // Load the module if not already initialized - if (!(id in wpRequire.c)) { + if (!webpackChunks || !(id in webpackChunks)) { wpRequire(id); } // Get the module from the cache - const rawModule: RawModule | undefined = wpRequire.c[id]; + const rawModule = webpackChunks?.[id]; if (raw) { return rawModule as RawModule | undefined; @@ -152,7 +152,7 @@ export function getModule( ): T | T[] | RawModule | Array> | undefined { try { // Find nothing if webpack hasn't been started yet - if (typeof wpRequire?.c === "undefined") return options.all ? [] : undefined; + if (typeof webpackChunks === "undefined") return options.all ? [] : undefined; const wrappedFilter: Filter = (mod) => { try { @@ -164,8 +164,8 @@ export function getModule( }; const modules = options.all - ? Object.values(wpRequire.c).filter(wrappedFilter) - : Object.values(wpRequire.c).find(wrappedFilter); + ? Object.values(webpackChunks).filter(wrappedFilter) + : Object.values(webpackChunks).find(wrappedFilter); if (options.raw) { return modules as RawModule | Array> | undefined; diff --git a/src/renderer/modules/webpack/patch-load.ts b/src/renderer/modules/webpack/patch-load.ts index 658eb9b82..fda81026e 100644 --- a/src/renderer/modules/webpack/patch-load.ts +++ b/src/renderer/modules/webpack/patch-load.ts @@ -1,4 +1,9 @@ -import type { WebpackChunk, WebpackChunkGlobal, WebpackRequire } from "../../../types"; +import type { + WebpackChunk, + WebpackChunkGlobal, + WebpackRawModules, + WebpackRequire, +} from "../../../types"; import { listeners } from "./lazy"; @@ -10,6 +15,7 @@ import { patchModuleSource } from "./plaintext-patch"; * @hidden */ export let wpRequire: WebpackRequire | undefined; +export let webpackChunks: WebpackRawModules | undefined; let signalReady: () => void; let ready = false; @@ -105,6 +111,7 @@ function loadWebpackModules(chunksGlobal: WebpackChunkGlobal): void { {}, (r: WebpackRequire | undefined) => { wpRequire = r!; + if (wpRequire.c && !webpackChunks) webpackChunks = wpRequire.c; if (r) { r.d = (module: unknown, exports: Record unknown>) => { diff --git a/src/types/webpack.ts b/src/types/webpack.ts index 207c11c83..16974fbed 100644 --- a/src/types/webpack.ts +++ b/src/types/webpack.ts @@ -15,8 +15,10 @@ export interface RawModule { exports: T; } +export type WebpackRawModules = Record; + export type WebpackRequire = ((e: number) => unknown) & { - c: Record; + c?: WebpackRawModules; d: (module: unknown, exports: Record unknown>) => void; m: WebpackChunk; };