Skip to content

Commit 0701070

Browse files
authored
refactor: group patch for load-manifest.js (#704)
1 parent 64a10a6 commit 0701070

File tree

3 files changed

+56
-82
lines changed

3 files changed

+56
-82
lines changed

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js";
1313
import * as patches from "./patches/index.js";
1414
import { inlineBuildId } from "./patches/plugins/build-id.js";
1515
import { inlineDynamicRequires } from "./patches/plugins/dynamic-requires.js";
16-
import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js";
1716
import { inlineFindDir } from "./patches/plugins/find-dir.js";
1817
import { patchInstrumentation } from "./patches/plugins/instrumentation.js";
1918
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
@@ -96,7 +95,6 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
9695
handleOptionalDependencies(optionalDependencies),
9796
patchInstrumentation(updater, buildOpts),
9897
patchPagesRouterContext(buildOpts),
99-
inlineEvalManifest(updater, buildOpts),
10098
inlineFindDir(updater, buildOpts),
10199
inlineLoadManifest(updater, buildOpts),
102100
inlineBuildId(updater),

packages/cloudflare/src/cli/build/patches/plugins/eval-manifest.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/**
2-
* Inline `loadManifest` as it relies on `readFileSync` that is not supported by workerd.
2+
* Inline `loadManifest` and `evalManifest` from `load-manifest.js`
3+
*
4+
* They rely on `readFileSync` that is not supported by workerd.
35
*/
46

57
import { readFile } from "node:fs/promises";
@@ -21,13 +23,17 @@ export function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOpti
2123
escape: false,
2224
}),
2325
contentFilter: /function loadManifest\(/,
24-
callback: async ({ contents }) => patchCode(contents, await getRule(buildOpts)),
26+
callback: async ({ contents }) => {
27+
contents = await patchCode(contents, await getLoadManifestRule(buildOpts));
28+
contents = await patchCode(contents, await getEvalManifestRule(buildOpts));
29+
return contents;
30+
},
2531
},
2632
},
2733
]);
2834
}
2935

30-
async function getRule(buildOpts: BuildOptions) {
36+
async function getLoadManifestRule(buildOpts: BuildOptions) {
3137
const { outputDir } = buildOpts;
3238

3339
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
@@ -39,10 +45,9 @@ async function getRule(buildOpts: BuildOptions) {
3945
await Promise.all(
4046
manifests.map(
4147
async (manifest) => `
42-
if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
43-
return ${await readFile(manifest, "utf-8")};
44-
}
45-
`
48+
if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
49+
return ${await readFile(manifest, "utf-8")};
50+
}`
4651
)
4752
)
4853
).join("\n");
@@ -62,3 +67,47 @@ function loadManifest($PATH, $$$ARGS) {
6267
}`,
6368
} satisfies RuleConfig;
6469
}
70+
71+
async function getEvalManifestRule(buildOpts: BuildOptions) {
72+
const { outputDir } = buildOpts;
73+
74+
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next");
75+
const appDir = join(baseDir, "server/app");
76+
const manifests = await glob(join(baseDir, "**/*_client-reference-manifest.js"), {
77+
windowsPathsNoEscape: true,
78+
});
79+
80+
const returnManifests = manifests
81+
.map((manifest) => {
82+
const endsWith = normalizePath(relative(baseDir, manifest));
83+
const key = normalizePath("/" + relative(appDir, manifest)).replace(
84+
"_client-reference-manifest.js",
85+
""
86+
);
87+
return `
88+
if ($PATH.endsWith("${endsWith}")) {
89+
require(${JSON.stringify(manifest)});
90+
return {
91+
__RSC_MANIFEST: {
92+
"${key}": globalThis.__RSC_MANIFEST["${key}"],
93+
},
94+
};
95+
}`;
96+
})
97+
.join("\n");
98+
99+
return {
100+
rule: {
101+
pattern: `
102+
function evalManifest($PATH, $$$ARGS) {
103+
$$$_
104+
}`,
105+
},
106+
fix: `
107+
function evalManifest($PATH, $$$ARGS) {
108+
$PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
109+
${returnManifests}
110+
throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
111+
}`,
112+
} satisfies RuleConfig;
113+
}

0 commit comments

Comments
 (0)