Skip to content

Commit

Permalink
Fix missing wpRequire.c (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
asportnoy authored Dec 1, 2023
1 parent f3aeca4 commit f294160
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/renderer/modules/webpack/get-modules.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -83,12 +83,12 @@ export function getById<T>(id: number, raw?: boolean): T | RawModule<T> | undefi
export function getById<T>(id: number, raw = false): T | RawModule<T> | 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<T> | undefined;
Expand Down Expand Up @@ -152,7 +152,7 @@ export function getModule<T>(
): T | T[] | RawModule<T> | Array<RawModule<T>> | 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 {
Expand All @@ -164,8 +164,8 @@ export function getModule<T>(
};

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<T> | Array<RawModule<T>> | undefined;
Expand Down
9 changes: 8 additions & 1 deletion src/renderer/modules/webpack/patch-load.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { WebpackChunk, WebpackChunkGlobal, WebpackRequire } from "../../../types";
import type {
WebpackChunk,
WebpackChunkGlobal,
WebpackRawModules,
WebpackRequire,
} from "../../../types";

import { listeners } from "./lazy";

Expand All @@ -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;
Expand Down Expand Up @@ -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<string, () => unknown>) => {
Expand Down
4 changes: 3 additions & 1 deletion src/types/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export interface RawModule<T = unknown> {
exports: T;
}

export type WebpackRawModules = Record<string | number, RawModule>;

export type WebpackRequire = ((e: number) => unknown) & {
c: Record<string | number, RawModule>;
c?: WebpackRawModules;
d: (module: unknown, exports: Record<string, () => unknown>) => void;
m: WebpackChunk;
};
Expand Down

0 comments on commit f294160

Please sign in to comment.