Skip to content

Commit 3c41bee

Browse files
committed
refactor: convert require.resolve patches to astgrep
1 parent 3d80aed commit 3c41bee

File tree

3 files changed

+60
-24
lines changed

3 files changed

+60
-24
lines changed

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

Lines changed: 5 additions & 22 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[],
@@ -152,7 +154,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
152154

153155
fs.writeFileSync(openNextServerBundle + ".meta.json", JSON.stringify(result.metafile, null, 2));
154156

155-
await updateWorkerBundledCode(openNextServerBundle, buildOpts);
157+
await updateWorkerBundledCode(openNextServerBundle);
156158

157159
const isMonorepo = monorepoRoot !== appPath;
158160
if (isMonorepo) {
@@ -170,28 +172,9 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
170172
/**
171173
* This function applies patches required for the code to run on workers.
172174
*/
173-
export async function updateWorkerBundledCode(
174-
workerOutputFile: string,
175-
buildOpts: BuildOptions
176-
): Promise<void> {
175+
export async function updateWorkerBundledCode(workerOutputFile: string): Promise<void> {
177176
const code = await readFile(workerOutputFile, "utf8");
178-
179-
const patchedCode = await patchCodeWithValidations(code, [
180-
["require", patches.patchRequire],
181-
["cacheHandler", (code) => patches.patchCache(code, buildOpts)],
182-
["composableCache", (code) => patches.patchComposableCache(code, buildOpts), { isOptional: true }],
183-
[
184-
"`require.resolve` call",
185-
// workers do not support dynamic require nor require.resolve
186-
(code) => code.replace('require.resolve("./cache.cjs")', '"unused"'),
187-
],
188-
[
189-
"`require.resolve composable cache` call",
190-
// workers do not support dynamic require nor require.resolve
191-
(code) => code.replace('require.resolve("./composable-cache.cjs")', '"unused"'),
192-
],
193-
]);
194-
177+
const patchedCode = await patchCodeWithValidations(code, [["require", patches.patchRequire]]);
195178
await writeFile(workerOutputFile, patchedCode);
196179
}
197180

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

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

1010
import { existsSync, readFileSync } from "node:fs";
11-
import { join } from "node:path";
11+
import path from "node:path";
1212

1313
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
1414
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
@@ -26,7 +26,9 @@ export function patchNextServer(updater: ContentUpdater, buildOpts: BuildOptions
2626
callback: async ({ contents }) => {
2727
const { outputDir } = buildOpts;
2828

29-
const manifestPath = join(
29+
contents = patchCode(contents, buildIdRule);
30+
31+
const manifestPath = path.join(
3032
outputDir,
3133
"server-functions/default",
3234
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)