Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix webpack #584

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions src/renderer/modules/webpack/patch-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,13 @@ function patchChunk(chunk: WebpackChunk): void {
* @internal
*/
function patchPush(webpackChunk: WebpackChunkGlobal): void {
let original = webpackChunk.push;

const original = webpackChunk.push.bind(webpackChunk);
function handlePush(chunk: WebpackChunk): unknown {
patchChunk(chunk);
return original.call(webpackChunk, chunk);
return original(chunk);
}

Object.defineProperty(webpackChunk, "push", {
get: () => handlePush,
set: (v) => (original = v),
configurable: true,
});
webpackChunk.push = handlePush as WebpackChunkGlobal["push"];
}

/**
Expand All @@ -91,32 +86,29 @@ function patchPush(webpackChunk: WebpackChunkGlobal): void {
* @internal
*/
function loadWebpackModules(chunksGlobal: WebpackChunkGlobal): void {
// once the webpack loads the modules, wpRequire is patched and exported
chunksGlobal.push([
[Symbol("replugged")],
{},
(r: WebpackRequire) => {
wpRequire = r;
wpRequire.d = (module: unknown, exports: Record<string, () => unknown>) => {
for (const prop in exports) {
if (
Object.hasOwnProperty.call(exports, prop) &&
!Object.hasOwnProperty.call(module, prop)
) {
Object.defineProperty(module, prop, {
enumerable: true,
configurable: true,
get: () => exports[prop](),
set: (value) => (exports[prop] = () => value),
});
}
}
};
},
]);
chunksGlobal.pop();

if (wpRequire) {
wpRequire.d = (module: unknown, exports: Record<string, () => unknown>) => {
for (const prop in exports) {
if (
Object.hasOwnProperty.call(exports, prop) &&
!Object.hasOwnProperty.call(module, prop)
) {
Object.defineProperty(module, prop, {
enumerable: true,
configurable: true,
get: () => exports[prop](),
set: (value) => (exports[prop] = () => value),
});
}
}
};
}

// Patch previously loaded chunks
if (Array.isArray(chunksGlobal)) {
Expand Down
9 changes: 6 additions & 3 deletions src/types/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ export type WebpackModule = (
wpRequire: WebpackRequire,
) => void;

export type WebpackChunk = [Array<symbol | number>, Record<number, WebpackModule>];
export type WebpackChunk = [
Array<symbol | number>,
Record<number, WebpackModule>,
((r: WebpackRequire) => unknown)?,
];

// Do NOT put `WebpackChunk[]` first, otherwise TS
// prioritizes Array.prototype.push over this custom
// push method and starts producing errors.
export type WebpackChunkGlobal = {
push(chunk: WebpackChunk): void;
push<T extends (r: WebpackRequire) => unknown>(chunk: [...WebpackChunk, T]): ReturnType<T>;
push(chunk: WebpackChunk): unknown;
} & WebpackChunk[];

export type Filter = (module: RawModule) => boolean | ModuleExports;
Expand Down
Loading