From 29da3257aa3ab80be5a18b4f71fb8f64b5ea39d0 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 18 Dec 2024 19:06:13 +0000 Subject: [PATCH] prevent eval patch from failing build --- packages/cloudflare/src/cli/build/bundle-server.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/cloudflare/src/cli/build/bundle-server.ts b/packages/cloudflare/src/cli/build/bundle-server.ts index 4cb62227..e8bd5b6e 100644 --- a/packages/cloudflare/src/cli/build/bundle-server.ts +++ b/packages/cloudflare/src/cli/build/bundle-server.ts @@ -167,7 +167,11 @@ async function updateWorkerBundledCode( // TODO: implement for cf (possibly in @opennextjs/aws) .replace("patchAsyncStorage();", "//patchAsyncStorage();"), ], - ['`eval("require")` calls', (code) => code.replaceAll('eval("require")', "require")], + [ + '`eval("require")` calls', + (code) => code.replaceAll('eval("require")', "require"), + { isOptional: true }, + ], [ "`require.resolve` call", // workers do not support dynamic require nor require.resolve @@ -204,18 +208,18 @@ function createFixRequiresESBuildPlugin(config: Config): Plugin { */ async function patchCodeWithValidations( code: string, - patches: [string, (code: string) => string | Promise][] + patches: [string, (code: string) => string | Promise, opts?: { isOptional?: boolean }][] ): Promise { console.log(`Applying code patches:`); let patchedCode = code; - for (const [target, patchFunction] of patches) { + for (const [target, patchFunction, opts] of patches) { console.log(` - patching ${target}`); const prePatchCode = patchedCode; patchedCode = await patchFunction(patchedCode); - if (prePatchCode === patchedCode) { + if (!opts?.isOptional && prePatchCode === patchedCode) { throw new Error(`Failed to patch ${target}`); } }