this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: convert require.resolve patches to astgrep (#707)

authored by

Victor Berchet and committed by
GitHub
477288f6 de6a6cd9

+56 -17
+3 -15
packages/cloudflare/src/cli/build/bundle-server.ts
··· 16 16 import { patchInstrumentation } from "./patches/plugins/instrumentation.js"; 17 17 import { inlineLoadManifest } from "./patches/plugins/load-manifest.js"; 18 18 import { patchNextServer } from "./patches/plugins/next-server.js"; 19 + import { patchResolveCache } from "./patches/plugins/open-next.js"; 19 20 import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js"; 20 21 import { patchPagesRouterContext } from "./patches/plugins/pages-router-context.js"; 21 22 import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js"; ··· 99 100 inlineLoadManifest(updater, buildOpts), 100 101 patchNextServer(updater, buildOpts), 101 102 patchDepdDeprecations(updater), 103 + patchResolveCache(updater, buildOpts), 102 104 // Apply updater updates, must be the last plugin 103 105 updater.plugin, 104 106 ] as Plugin[], ··· 172 174 */ 173 175 export async function updateWorkerBundledCode(workerOutputFile: string): Promise<void> { 174 176 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]]); 190 178 await writeFile(workerOutputFile, patchedCode); 191 179 } 192 180
+2 -2
packages/cloudflare/src/cli/build/patches/plugins/next-server.ts
··· 9 9 */ 10 10 11 11 import { existsSync, readFileSync } from "node:fs"; 12 - import path, { join } from "node:path"; 12 + import path from "node:path"; 13 13 14 14 import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js"; 15 15 import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; ··· 31 31 32 32 contents = patchCode(contents, buildIdRule); 33 33 34 - const manifestPath = join( 34 + const manifestPath = path.join( 35 35 outputDir, 36 36 "server-functions/default", 37 37 getPackagePath(buildOpts),
+51
packages/cloudflare/src/cli/build/patches/plugins/open-next.ts
··· 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 + `;