Skip to content

Commit 477288f

Browse files
authored
refactor: convert require.resolve patches to astgrep (#707)
1 parent de6a6cd commit 477288f

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { inlineFindDir } from "./patches/plugins/find-dir.js";
1616
import { patchInstrumentation } from "./patches/plugins/instrumentation.js";
1717
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
1818
import { patchNextServer } from "./patches/plugins/next-server.js";
19+
import { patchResolveCache } from "./patches/plugins/open-next.js";
1920
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
2021
import { patchPagesRouterContext } from "./patches/plugins/pages-router-context.js";
2122
import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
@@ -99,6 +100,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
99100
inlineLoadManifest(updater, buildOpts),
100101
patchNextServer(updater, buildOpts),
101102
patchDepdDeprecations(updater),
103+
patchResolveCache(updater, buildOpts),
102104
// Apply updater updates, must be the last plugin
103105
updater.plugin,
104106
] as Plugin[],
@@ -172,21 +174,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
172174
*/
173175
export async function updateWorkerBundledCode(workerOutputFile: string): Promise<void> {
174176
const code = await readFile(workerOutputFile, "utf8");
175-
176-
const patchedCode = await patchCodeWithValidations(code, [
177-
["require", patches.patchRequire],
178-
[
179-
"`require.resolve` call",
180-
// workers do not support dynamic require nor require.resolve
181-
(code) => code.replace('require.resolve("./cache.cjs")', '"unused"'),
182-
],
183-
[
184-
"`require.resolve composable cache` call",
185-
// workers do not support dynamic require nor require.resolve
186-
(code) => code.replace('require.resolve("./composable-cache.cjs")', '"unused"'),
187-
],
188-
]);
189-
177+
const patchedCode = await patchCodeWithValidations(code, [["require", patches.patchRequire]]);
190178
await writeFile(workerOutputFile, patchedCode);
191179
}
192180

packages/cloudflare/src/cli/build/patches/plugins/next-server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import { existsSync, readFileSync } from "node:fs";
12-
import path, { join } from "node:path";
12+
import path from "node:path";
1313

1414
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
1515
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
@@ -31,7 +31,7 @@ export function patchNextServer(updater: ContentUpdater, buildOpts: BuildOptions
3131

3232
contents = patchCode(contents, buildIdRule);
3333

34-
const manifestPath = join(
34+
const manifestPath = path.join(
3535
outputDir,
3636
"server-functions/default",
3737
getPackagePath(buildOpts),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Removed unused `require.resolve` calls in Open Next.
3+
*/
4+
5+
import path from "node:path";
6+
7+
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
8+
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
9+
import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
10+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
11+
12+
export function patchResolveCache(updater: ContentUpdater, buildOpts: BuildOptions): Plugin {
13+
const { outputDir } = buildOpts;
14+
const packagePath = getPackagePath(buildOpts);
15+
const outputPath = path.join(outputDir, "server-functions/default");
16+
17+
const indexPath = path.relative(
18+
buildOpts.appBuildOutputPath,
19+
path.join(outputPath, packagePath, `index.mjs`)
20+
);
21+
22+
console.error({ index: indexPath });
23+
24+
return updater.updateContent("patch-resolve-cache", [
25+
{
26+
field: {
27+
filter: getCrossPlatformPathRegex(indexPath),
28+
contentFilter: /cacheHandlerPath/,
29+
callback: async ({ contents }) => {
30+
contents = patchCode(contents, cacheHandlerRule);
31+
contents = patchCode(contents, compositeCacheHandlerRule);
32+
return contents;
33+
},
34+
},
35+
},
36+
]);
37+
}
38+
39+
export const cacheHandlerRule = `
40+
rule:
41+
pattern: var cacheHandlerPath = __require.resolve("./cache.cjs");
42+
fix: |-
43+
var cacheHandlerPath = "";
44+
`;
45+
46+
export const compositeCacheHandlerRule = `
47+
rule:
48+
pattern: var composableCacheHandlerPath = __require.resolve("./composable-cache.cjs");
49+
fix: |-
50+
var composableCacheHandlerPath = "";
51+
`;

0 commit comments

Comments
 (0)