Skip to content

Commit d51c81d

Browse files
authored
refactor: patch findDir using ast-grep (#392)
1 parent e79f09c commit d51c81d

File tree

4 files changed

+52
-30
lines changed

4 files changed

+52
-30
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as patches from "./patches/index.js";
1212
import { ContentUpdater } from "./patches/plugins/content-updater.js";
1313
import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js";
1414
import { patchFetchCacheSetMissingWaitUntil } from "./patches/plugins/fetch-cache-wait-until.js";
15+
import { inlineFindDir } from "./patches/plugins/find-dir.js";
1516
import { patchLoadInstrumentation } from "./patches/plugins/load-instrumentation.js";
1617
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
1718
import { fixRequire } from "./patches/plugins/require.js";
@@ -91,6 +92,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
9192
patchLoadInstrumentation(updater),
9293
patchFetchCacheSetMissingWaitUntil(updater),
9394
inlineEvalManifest(updater, buildOpts),
95+
inlineFindDir(updater, buildOpts),
9496
// Apply updater updaters, must be the last plugin
9597
updater.plugin,
9698
],
@@ -195,7 +197,6 @@ export async function updateWorkerBundledCode(
195197
["require", patches.patchRequire],
196198
["`buildId` function", (code) => patches.patchBuildId(code, buildOpts)],
197199
["`loadManifest` function", (code) => patches.patchLoadManifest(code, buildOpts)],
198-
["`findDir` function", (code) => patches.patchFindDir(code, buildOpts)],
199200
["cacheHandler", (code) => patches.patchCache(code, buildOpts)],
200201
[
201202
"'require(this.middlewareManifestPath)'",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Inline `evalManifest` as it relies on `readFileSync` and `runInNewContext`
3+
* that are not supported by workerd.
4+
*/
5+
6+
import { existsSync } from "node:fs";
7+
import { join } from "node:path";
8+
9+
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
10+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
11+
12+
import { patchCode } from "../ast/util.js";
13+
import type { ContentUpdater } from "./content-updater.js";
14+
15+
export function inlineFindDir(updater: ContentUpdater, buildOpts: BuildOptions) {
16+
return updater.updateContent(
17+
"inline-find-dir",
18+
{
19+
filter: getCrossPlatformPathRegex(String.raw`/next/dist/lib/find-pages-dir\.js$`, { escape: false }),
20+
contentFilter: /function findDir\(/,
21+
},
22+
async ({ contents }) => patchCode(contents, await getRule(buildOpts))
23+
);
24+
}
25+
26+
async function getRule(buildOpts: BuildOptions) {
27+
const { outputDir } = buildOpts;
28+
29+
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");
30+
31+
const appExists = existsSync(join(baseDir, "app"));
32+
const pagesExists = existsSync(join(baseDir, "pages"));
33+
34+
return `
35+
rule:
36+
pattern: function findDir($DIR, $NAME) { $$$_ }
37+
fix: |-
38+
function findDir($DIR, $NAME) {
39+
if ($DIR.endsWith(".next/server")) {
40+
if ($NAME === "app") {
41+
return ${appExists};
42+
}
43+
if ($NAME === "pages") {
44+
return ${pagesExists};
45+
}
46+
}
47+
throw new Error(\`Unexpected findDir(\${$DIR}, \${$NAME}) call!\`);
48+
}
49+
`;
50+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from "./inline-middleware-manifest-require.js";
22
export * from "./patch-exception-bubbling.js";
3-
export * from "./patch-find-dir.js";
43
export * from "./patch-read-file.js";

packages/cloudflare/src/cli/build/patches/to-investigate/patch-find-dir.ts

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

0 commit comments

Comments
 (0)