Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js";
import { patchFetchCacheSetMissingWaitUntil } from "./patches/plugins/fetch-cache-wait-until.js";
import { inlineFindDir } from "./patches/plugins/find-dir.js";
import { patchLoadInstrumentation } from "./patches/plugins/load-instrumentation.js";
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
import { fixRequire } from "./patches/plugins/require.js";
import { shimRequireHook } from "./patches/plugins/require-hook.js";
Expand Down Expand Up @@ -93,6 +94,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
patchFetchCacheSetMissingWaitUntil(updater),
inlineEvalManifest(updater, buildOpts),
inlineFindDir(updater, buildOpts),
inlineLoadManifest(updater, buildOpts),
// Apply updater updaters, must be the last plugin
updater.plugin,
],
Expand Down Expand Up @@ -196,7 +198,6 @@ export async function updateWorkerBundledCode(
const patchedCode = await patchCodeWithValidations(code, [
["require", patches.patchRequire],
["`buildId` function", (code) => patches.patchBuildId(code, buildOpts)],
["`loadManifest` function", (code) => patches.patchLoadManifest(code, buildOpts)],
["cacheHandler", (code) => patches.patchCache(code, buildOpts)],
[
"'require(this.middlewareManifestPath)'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function evalManifest($PATH, $$$ARGS) {
fix: `
function evalManifest($PATH, $$$ARGS) {
${returnManifests}
throw new Error("Unknown evalManifest: " + $PATH);
throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
}`,
} satisfies RuleConfig;
}
60 changes: 60 additions & 0 deletions packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Inline `loadManifest` as it relies on `readFileSync`that is not supported by workerd.
*/

import { readFile } from "node:fs/promises";
import { join, relative } from "node:path";

import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
import { glob } from "glob";

import { normalizePath } from "../../utils/normalize-path.js";
import { patchCode, type RuleConfig } from "../ast/util.js";
import type { ContentUpdater } from "./content-updater.js";

export function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOptions) {
return updater.updateContent(
"inline-load-manifest",
{
filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/load-manifest\.js$`, { escape: false }),
contentFilter: /function loadManifest\(/,
},
async ({ contents }) => patchCode(contents, await getRule(buildOpts))
);
}

async function getRule(buildOpts: BuildOptions) {
const { outputDir } = buildOpts;

const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
const dotNextDir = join(baseDir, ".next");

const manifests = await glob(join(dotNextDir, "**/*-manifest.json"));

const returnManifests = (
await Promise.all(
manifests.map(
async (manifest) => `
if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
return ${await readFile(manifest, "utf-8")};
}
`
)
)
).join("\n");

return {
rule: {
pattern: `
function loadManifest($PATH, $$$ARGS) {
$$$_
}`,
},
fix: `
function loadManifest($PATH, $$$ARGS) {
${returnManifests}
throw new Error(\`Unexpected loadManifest(\${$PATH}) call!\`);
}`,
} satisfies RuleConfig;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { readFileSync } from "node:fs";
import { join, relative } from "node:path";

import { type BuildOptions, getBuildId, getPackagePath } from "@opennextjs/aws/build/helper.js";
import { globSync } from "glob";

import { normalizePath } from "../../utils/index.js";
import { type BuildOptions, getBuildId } from "@opennextjs/aws/build/helper.js";

export function patchBuildId(code: string, buildOpts: BuildOptions): string {
// The Next code gets the buildId from the filesystem so we hardcode the value at build time.
Expand All @@ -15,30 +9,3 @@ export function patchBuildId(code: string, buildOpts: BuildOptions): string {
`
);
}

export function patchLoadManifest(code: string, buildOpts: BuildOptions): string {
// Inline manifest that Next would otherwise retrieve from the file system.

const { outputDir } = buildOpts;

const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
const dotNextDir = join(baseDir, ".next");

const manifests = globSync(join(dotNextDir, "**/*-manifest.json"));

return code.replace(
/function loadManifest\((.+?), .+?\) {/,
`$&
${manifests
.map(
(manifest) => `
if ($1.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
return ${readFileSync(manifest, "utf-8")};
}
`
)
.join("\n")}
throw new Error("Unknown loadManifest: " + $1);
`
);
}