diff --git a/packages/cloudflare/src/cli/build/bundle-server.ts b/packages/cloudflare/src/cli/build/bundle-server.ts index 455f2266f..5f8dee424 100644 --- a/packages/cloudflare/src/cli/build/bundle-server.ts +++ b/packages/cloudflare/src/cli/build/bundle-server.ts @@ -10,7 +10,6 @@ import { build, type Plugin } from "esbuild"; import { getOpenNextConfig } from "../../api/config.js"; import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js"; import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js"; -import * as patches from "./patches/index.js"; import { inlineDynamicRequires } from "./patches/plugins/dynamic-requires.js"; import { inlineFindDir } from "./patches/plugins/find-dir.js"; import { patchInstrumentation } from "./patches/plugins/instrumentation.js"; @@ -23,7 +22,7 @@ import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations import { fixRequire } from "./patches/plugins/require.js"; import { shimRequireHook } from "./patches/plugins/require-hook.js"; import { setWranglerExternal } from "./patches/plugins/wrangler-external.js"; -import { needsExperimentalReact, normalizePath, patchCodeWithValidations } from "./utils/index.js"; +import { copyPackageCliFiles, needsExperimentalReact, normalizePath } from "./utils/index.js"; /** The dist directory of the Cloudflare adapter package */ const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../.."); @@ -46,7 +45,7 @@ const optionalDependencies = [ * Bundle the Open Next server. */ export async function bundleServer(buildOpts: BuildOptions): Promise { - patches.copyPackageCliFiles(packageDistDir, buildOpts); + copyPackageCliFiles(packageDistDir, buildOpts); const { appPath, outputDir, monorepoRoot, debug } = buildOpts; const baseManifestPath = path.join( @@ -174,13 +173,11 @@ export async function bundleServer(buildOpts: BuildOptions): Promise { } /** - * This function applies patches required for the code to run on workers. + * This function apply updates to the bundled code. */ export async function updateWorkerBundledCode(workerOutputFile: string): Promise { const code = await readFile(workerOutputFile, "utf8"); - const patchedCode = await patchCodeWithValidations(code, [ - ["require", patches.patchRequire, { isOptional: true }], - ]); + const patchedCode = code.replace(/__require\d?\(/g, "require(").replace(/__require\d?\./g, "require."); await writeFile(workerOutputFile, patchedCode); } diff --git a/packages/cloudflare/src/cli/build/patches/index.ts b/packages/cloudflare/src/cli/build/patches/index.ts deleted file mode 100644 index a33214603..000000000 --- a/packages/cloudflare/src/cli/build/patches/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./investigated/index.js"; diff --git a/packages/cloudflare/src/cli/build/patches/investigated/index.ts b/packages/cloudflare/src/cli/build/patches/investigated/index.ts deleted file mode 100644 index 70bb87206..000000000 --- a/packages/cloudflare/src/cli/build/patches/investigated/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./copy-package-cli-files.js"; -export * from "./patch-require.js"; diff --git a/packages/cloudflare/src/cli/build/patches/investigated/patch-require.ts b/packages/cloudflare/src/cli/build/patches/investigated/patch-require.ts deleted file mode 100644 index d86ff3a99..000000000 --- a/packages/cloudflare/src/cli/build/patches/investigated/patch-require.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Replaces webpack `__require` with actual `require` - */ -export function patchRequire(code: string): string { - return code.replace(/__require\d?\(/g, "require(").replace(/__require\d?\./g, "require."); -} diff --git a/packages/cloudflare/src/cli/build/utils/apply-patches.ts b/packages/cloudflare/src/cli/build/utils/apply-patches.ts deleted file mode 100644 index 599158daa..000000000 --- a/packages/cloudflare/src/cli/build/utils/apply-patches.ts +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Applies multiple code patches in order to a given piece of code, at each step it validates that the code - * has actually been patched/changed, if not an error is thrown - * - * @param code the code to apply the patches to - * @param patches array of tuples, containing a string indicating the target of the patching (for logging) and - * a patching function that takes a string (pre-patch code) and returns a string (post-patch code) - * @returns the patched code - */ -export async function patchCodeWithValidations( - code: string, - patches: [string, (code: string) => string | Promise, opts?: { isOptional?: boolean }][] -): Promise { - console.log(`Applying code patches:`); - let patchedCode = code; - - for (const [target, patchFunction, opts] of patches) { - console.log(` - patching ${target}`); - - const prePatchCode = patchedCode; - patchedCode = await patchFunction(patchedCode); - - if (!opts?.isOptional && prePatchCode === patchedCode) { - throw new Error(`Failed to patch ${target}`); - } - } - - return patchedCode; -} diff --git a/packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts b/packages/cloudflare/src/cli/build/utils/copy-package-cli-files.ts similarity index 92% rename from packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts rename to packages/cloudflare/src/cli/build/utils/copy-package-cli-files.ts index 263b480f4..cbe5c993f 100644 --- a/packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts +++ b/packages/cloudflare/src/cli/build/utils/copy-package-cli-files.ts @@ -3,7 +3,7 @@ import path from "node:path"; import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; -import { getOutputWorkerPath } from "../../bundle-server.js"; +import { getOutputWorkerPath } from "../bundle-server.js"; /** * Copies diff --git a/packages/cloudflare/src/cli/build/utils/index.ts b/packages/cloudflare/src/cli/build/utils/index.ts index cca97f023..869bbe6bb 100644 --- a/packages/cloudflare/src/cli/build/utils/index.ts +++ b/packages/cloudflare/src/cli/build/utils/index.ts @@ -1,4 +1,4 @@ -export * from "./apply-patches.js"; +export * from "./copy-package-cli-files.js"; export * from "./create-config-files.js"; export * from "./ensure-cf-config.js"; export * from "./extract-project-env-vars.js";