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
2 changes: 0 additions & 2 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -96,7 +95,6 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
handleOptionalDependencies(optionalDependencies),
patchInstrumentation(updater, buildOpts),
patchPagesRouterContext(buildOpts),
inlineEvalManifest(updater, buildOpts),
inlineFindDir(updater, buildOpts),
inlineLoadManifest(updater, buildOpts),
inlineBuildId(updater),
Expand Down
73 changes: 0 additions & 73 deletions packages/cloudflare/src/cli/build/patches/plugins/eval-manifest.ts

This file was deleted.

63 changes: 56 additions & 7 deletions packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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));
Expand All @@ -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");
Expand All @@ -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;
}