diff --git a/packages/cloudflare/src/cli/build/build-worker.ts b/packages/cloudflare/src/cli/build/build-worker.ts index 2acdd63..136d4da 100644 --- a/packages/cloudflare/src/cli/build/build-worker.ts +++ b/packages/cloudflare/src/cli/build/build-worker.ts @@ -5,6 +5,7 @@ import { Config } from "../config"; import { copyPackageCliFiles } from "./patches/investigated/copy-package-cli-files"; import { fileURLToPath } from "node:url"; import { inlineEvalManifest } from "./patches/to-investigate/inline-eval-manifest"; +import { inlineMiddlewareManifestRequire } from "./patches/to-investigate/inline-middleware-manifest-require"; import { inlineNextRequire } from "./patches/to-investigate/inline-next-require"; import { patchCache } from "./patches/investigated/patch-cache"; import { patchFindDir } from "./patches/to-investigate/patch-find-dir"; @@ -90,10 +91,6 @@ export async function buildWorker(config: Config): Promise { // Note: we need the __non_webpack_require__ variable declared as it is used by next-server: // https://github.com/vercel/next.js/blob/be0c3283/packages/next/src/server/next-server.ts#L116-L119 __non_webpack_require__: "require", - // The next.js server can run in minimal mode: https://github.com/vercel/next.js/blob/aa90fe9bb/packages/next/src/server/base-server.ts#L510-L511 - // this avoids some extra (/problematic) `require` calls, such as here: https://github.com/vercel/next.js/blob/aa90fe9bb/packages/next/src/server/next-server.ts#L1259 - // that's wht we enable it - "process.env.NEXT_PRIVATE_MINIMAL_MODE": "true", // Ask mhart if he can explain why the `define`s below are necessary "process.env.NEXT_RUNTIME": '"nodejs"', "process.env.NODE_ENV": '"production"', @@ -166,6 +163,7 @@ async function updateWorkerBundledCode(workerOutputFile: string, config: Config) patchedCode = patchFindDir(patchedCode, config); patchedCode = inlineEvalManifest(patchedCode, config); patchedCode = patchCache(patchedCode, config); + patchedCode = inlineMiddlewareManifestRequire(patchedCode, config); await writeFile(workerOutputFile, patchedCode); } diff --git a/packages/cloudflare/src/cli/build/patches/to-investigate/inline-middleware-manifest-require.ts b/packages/cloudflare/src/cli/build/patches/to-investigate/inline-middleware-manifest-require.ts new file mode 100644 index 0000000..791db33 --- /dev/null +++ b/packages/cloudflare/src/cli/build/patches/to-investigate/inline-middleware-manifest-require.ts @@ -0,0 +1,19 @@ +import { existsSync, readFileSync } from "node:fs"; +import { Config } from "../../../config"; +import path from "node:path"; + +/** + * Inlines the middleware manifest from the build output to prevent a dynamic require statement + * as they result in runtime failures. + */ +export function inlineMiddlewareManifestRequire(code: string, config: Config) { + console.log("# inlineMiddlewareManifestRequire"); + + const middlewareManifestPath = path.join(config.paths.standaloneAppServer, "middleware-manifest.json"); + + const middlewareManifest = existsSync(middlewareManifestPath) + ? JSON.parse(readFileSync(middlewareManifestPath, "utf-8")) + : {}; + + return code.replace(/require\(this.middlewareManifestPath\)/, JSON.stringify(middlewareManifest)); +}