Skip to content

Commit 4cd819b

Browse files
authored
refactor: make inlineRequirePagePlugin async (#352)
1 parent 420b598 commit 4cd819b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

packages/cloudflare/src/cli/build/patches/plugins/require-page.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { existsSync, readFileSync } from "node:fs";
21
import { readFile } from "node:fs/promises";
32
import { join } from "node:path";
43

@@ -20,27 +19,37 @@ export function inlineRequirePagePlugin(buildOpts: BuildOptions) {
2019
async ({ path }) => {
2120
const jsCode = await readFile(path, "utf8");
2221
if (/function requirePage\(/.test(jsCode)) {
23-
return { contents: patchCode(jsCode, getRule(buildOpts)) };
22+
return { contents: patchCode(jsCode, await getRule(buildOpts)) };
2423
}
2524
}
2625
);
2726
},
2827
};
2928
}
3029

31-
function getRule(buildOpts: BuildOptions) {
30+
async function getRule(buildOpts: BuildOptions) {
3231
const { outputDir } = buildOpts;
3332
const serverDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");
3433

3534
const pagesManifestFile = join(serverDir, "pages-manifest.json");
3635
const appPathsManifestFile = join(serverDir, "app-paths-manifest.json");
3736

38-
const pagesManifests: string[] = existsSync(pagesManifestFile)
39-
? Object.values(JSON.parse(readFileSync(pagesManifestFile, "utf-8")))
40-
: [];
41-
const appPathsManifests: string[] = existsSync(appPathsManifestFile)
42-
? Object.values(JSON.parse(readFileSync(appPathsManifestFile, "utf-8")))
43-
: [];
37+
let pagesManifests: string[] = [];
38+
try {
39+
pagesManifests = Object.values(JSON.parse(await readFile(pagesManifestFile, "utf-8")));
40+
} catch {
41+
// The file does not exists
42+
pagesManifests = [];
43+
}
44+
45+
let appPathsManifests: string[];
46+
try {
47+
appPathsManifests = Object.values(JSON.parse(await readFile(appPathsManifestFile, "utf-8")));
48+
} catch {
49+
// The file does not exists
50+
appPathsManifests = [];
51+
}
52+
4453
const manifests = pagesManifests.concat(appPathsManifests);
4554

4655
const htmlFiles = manifests.filter((file) => file.endsWith(".html"));
@@ -49,13 +58,15 @@ function getRule(buildOpts: BuildOptions) {
4958
// Inline fs access and dynamic require that are not supported by workerd.
5059
const fnBody = `
5160
// html
52-
${htmlFiles
53-
.map(
54-
(file) => `if (pagePath.endsWith("${file}")) {
55-
return ${JSON.stringify(readFileSync(join(serverDir, file), "utf-8"))};
61+
${(
62+
await Promise.all(
63+
htmlFiles.map(
64+
async (file) => `if (pagePath.endsWith("${file}")) {
65+
return ${JSON.stringify(await readFile(join(serverDir, file), "utf-8"))};
5666
}`
67+
)
5768
)
58-
.join("\n")}
69+
).join("\n")}
5970
// js
6071
process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages';
6172
try {
@@ -74,13 +85,13 @@ try {
7485
return {
7586
rule: {
7687
pattern: `
77-
function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) {
88+
function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) {
7889
const $_ = getPagePath($$$ARGS);
7990
$$$_BODY
8091
}`,
8192
},
8293
fix: `
83-
function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) {
94+
function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) {
8495
const pagePath = getPagePath($$$ARGS);
8596
${fnBody}
8697
}`,

0 commit comments

Comments
 (0)