Skip to content

Commit

Permalink
better plain text patch
Browse files Browse the repository at this point in the history
  • Loading branch information
yofukashino committed Oct 27, 2023
1 parent 6620dcf commit 9dadb45
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/renderer/modules/webpack/patch-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function patchChunk(chunk: WebpackChunk): void {
for (const id in modules) {
const originalMod = modules[id];
sourceStrings[id] = originalMod.toString();
const mod = patchModuleSource(originalMod);
const mod = patchModuleSource(originalMod, id);
modules[id] = function (module, exports, require) {
mod(module, exports, require);

Expand Down
13 changes: 10 additions & 3 deletions src/renderer/modules/webpack/plaintext-patch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { PlaintextPatch, RawPlaintextPatch, WebpackModule } from "../../../types";
import { Logger } from "../logger";

const logger = Logger.api("plaintext-patch");
/**
* All plaintext patches
*/
Expand All @@ -10,7 +12,7 @@ export const plaintextPatches: RawPlaintextPatch[] = [];
* @param mod Module
* @returns Patched module
*/
export function patchModuleSource(mod: WebpackModule): WebpackModule {
export function patchModuleSource(mod: WebpackModule, id: string): WebpackModule {
const originalSource = mod.toString();

const patchedSource = plaintextPatches.reduce((source, patch) => {
Expand Down Expand Up @@ -39,8 +41,13 @@ export function patchModuleSource(mod: WebpackModule): WebpackModule {
}
try {
// eslint-disable-next-line no-eval
return (0, eval)(patchedSource);
} catch {
return (0, eval)(
`${
patchedSource.startsWith("function") ? `0,${patchedSource}` : patchedSource
}\n//# sourceURL=PatchedWebpack-${id}`,
);
} catch (err) {
logger.error(err);
// Syntax error in patched module--fail
return mod;
}
Expand Down

0 comments on commit 9dadb45

Please sign in to comment.