diff --git a/packages/cloudflare/src/cli/build/bundle-server.ts b/packages/cloudflare/src/cli/build/bundle-server.ts index 52cf3147..269492a2 100644 --- a/packages/cloudflare/src/cli/build/bundle-server.ts +++ b/packages/cloudflare/src/cli/build/bundle-server.ts @@ -13,7 +13,6 @@ import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js"; import * as patches from "./patches/index.js"; import { inlineBuildId } from "./patches/plugins/build-id.js"; import { inlineDynamicRequires } from "./patches/plugins/dynamic-requires.js"; -import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js"; import { inlineFindDir } from "./patches/plugins/find-dir.js"; import { patchInstrumentation } from "./patches/plugins/instrumentation.js"; import { inlineLoadManifest } from "./patches/plugins/load-manifest.js"; @@ -96,7 +95,6 @@ export async function bundleServer(buildOpts: BuildOptions): Promise { handleOptionalDependencies(optionalDependencies), patchInstrumentation(updater, buildOpts), patchPagesRouterContext(buildOpts), - inlineEvalManifest(updater, buildOpts), inlineFindDir(updater, buildOpts), inlineLoadManifest(updater, buildOpts), inlineBuildId(updater), diff --git a/packages/cloudflare/src/cli/build/patches/plugins/eval-manifest.ts b/packages/cloudflare/src/cli/build/patches/plugins/eval-manifest.ts deleted file mode 100644 index 6111cc57..00000000 --- a/packages/cloudflare/src/cli/build/patches/plugins/eval-manifest.ts +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Inline `evalManifest` as it relies on `readFileSync` and `runInNewContext` - * that are not supported by workerd. - */ - -import { join, posix, relative, sep } from "node:path"; - -import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js"; -import { patchCode, type RuleConfig } from "@opennextjs/aws/build/patch/astCodePatcher.js"; -import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js"; -import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; -import { glob } from "glob"; - -import { normalizePath } from "../../utils/normalize-path.js"; - -export function inlineEvalManifest(updater: ContentUpdater, buildOpts: BuildOptions): Plugin { - return updater.updateContent("inline-eval-manifest", [ - { - field: { - filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/load-manifest(\.external)?\.js$`, { - escape: false, - }), - contentFilter: /function evalManifest\(/, - callback: async ({ contents }) => patchCode(contents, await getRule(buildOpts)), - }, - }, - ]); -} - -async function getRule(buildOpts: BuildOptions) { - const { outputDir } = buildOpts; - - const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next"); - const appDir = join(baseDir, "server/app"); - const manifests = await glob(join(baseDir, "**/*_client-reference-manifest.js"), { - windowsPathsNoEscape: true, - }); - - const returnManifests = manifests - .map((manifest) => { - const endsWith = normalizePath(relative(baseDir, manifest)); - const key = normalizePath("/" + relative(appDir, manifest)).replace( - "_client-reference-manifest.js", - "" - ); - return ` - if ($PATH.endsWith("${endsWith}")) { - require(${JSON.stringify(manifest)}); - return { - __RSC_MANIFEST: { - "${key}": globalThis.__RSC_MANIFEST["${key}"], - }, - }; - } - `; - }) - .join("\n"); - - return { - rule: { - pattern: ` -function evalManifest($PATH, $$$ARGS) { - $$$_ -}`, - }, - fix: ` -function evalManifest($PATH, $$$ARGS) { - $PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)}); - ${returnManifests} - throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`); -}`, - } satisfies RuleConfig; -} diff --git a/packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts b/packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts index fa2339c0..930ab88b 100644 --- a/packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts +++ b/packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts @@ -1,5 +1,7 @@ /** - * Inline `loadManifest` as it relies on `readFileSync` that is not supported by workerd. + * Inline `loadManifest` and `evalManifest` from `load-manifest.js` + * + * They rely on `readFileSync` that is not supported by workerd. */ import { readFile } from "node:fs/promises"; @@ -21,13 +23,17 @@ export function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOpti escape: false, }), contentFilter: /function loadManifest\(/, - callback: async ({ contents }) => patchCode(contents, await getRule(buildOpts)), + callback: async ({ contents }) => { + contents = await patchCode(contents, await getLoadManifestRule(buildOpts)); + contents = await patchCode(contents, await getEvalManifestRule(buildOpts)); + return contents; + }, }, }, ]); } -async function getRule(buildOpts: BuildOptions) { +async function getLoadManifestRule(buildOpts: BuildOptions) { const { outputDir } = buildOpts; const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts)); @@ -39,10 +45,9 @@ async function getRule(buildOpts: BuildOptions) { await Promise.all( manifests.map( async (manifest) => ` - if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) { - return ${await readFile(manifest, "utf-8")}; - } - ` +if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) { + return ${await readFile(manifest, "utf-8")}; +}` ) ) ).join("\n"); @@ -62,3 +67,47 @@ function loadManifest($PATH, $$$ARGS) { }`, } satisfies RuleConfig; } + +async function getEvalManifestRule(buildOpts: BuildOptions) { + const { outputDir } = buildOpts; + + const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next"); + const appDir = join(baseDir, "server/app"); + const manifests = await glob(join(baseDir, "**/*_client-reference-manifest.js"), { + windowsPathsNoEscape: true, + }); + + const returnManifests = manifests + .map((manifest) => { + const endsWith = normalizePath(relative(baseDir, manifest)); + const key = normalizePath("/" + relative(appDir, manifest)).replace( + "_client-reference-manifest.js", + "" + ); + return ` +if ($PATH.endsWith("${endsWith}")) { + require(${JSON.stringify(manifest)}); + return { + __RSC_MANIFEST: { + "${key}": globalThis.__RSC_MANIFEST["${key}"], + }, + }; +}`; + }) + .join("\n"); + + return { + rule: { + pattern: ` +function evalManifest($PATH, $$$ARGS) { + $$$_ +}`, + }, + fix: ` +function evalManifest($PATH, $$$ARGS) { + $PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)}); + ${returnManifests} + throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`); +}`, + } satisfies RuleConfig; +}