this repo has no description
0
fork

Configure Feed

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

refactor: simplify the code (#724)

authored by

Victor Berchet and committed by
GitHub
92a18c46 dd5150d4

+6 -47
+4 -7
packages/cloudflare/src/cli/build/bundle-server.ts
··· 10 10 import { getOpenNextConfig } from "../../api/config.js"; 11 11 import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js"; 12 12 import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js"; 13 - import * as patches from "./patches/index.js"; 14 13 import { inlineDynamicRequires } from "./patches/plugins/dynamic-requires.js"; 15 14 import { inlineFindDir } from "./patches/plugins/find-dir.js"; 16 15 import { patchInstrumentation } from "./patches/plugins/instrumentation.js"; ··· 23 22 import { fixRequire } from "./patches/plugins/require.js"; 24 23 import { shimRequireHook } from "./patches/plugins/require-hook.js"; 25 24 import { setWranglerExternal } from "./patches/plugins/wrangler-external.js"; 26 - import { needsExperimentalReact, normalizePath, patchCodeWithValidations } from "./utils/index.js"; 25 + import { copyPackageCliFiles, needsExperimentalReact, normalizePath } from "./utils/index.js"; 27 26 28 27 /** The dist directory of the Cloudflare adapter package */ 29 28 const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../.."); ··· 46 45 * Bundle the Open Next server. 47 46 */ 48 47 export async function bundleServer(buildOpts: BuildOptions): Promise<void> { 49 - patches.copyPackageCliFiles(packageDistDir, buildOpts); 48 + copyPackageCliFiles(packageDistDir, buildOpts); 50 49 51 50 const { appPath, outputDir, monorepoRoot, debug } = buildOpts; 52 51 const baseManifestPath = path.join( ··· 174 173 } 175 174 176 175 /** 177 - * This function applies patches required for the code to run on workers. 176 + * This function apply updates to the bundled code. 178 177 */ 179 178 export async function updateWorkerBundledCode(workerOutputFile: string): Promise<void> { 180 179 const code = await readFile(workerOutputFile, "utf8"); 181 - const patchedCode = await patchCodeWithValidations(code, [ 182 - ["require", patches.patchRequire, { isOptional: true }], 183 - ]); 180 + const patchedCode = code.replace(/__require\d?\(/g, "require(").replace(/__require\d?\./g, "require."); 184 181 await writeFile(workerOutputFile, patchedCode); 185 182 } 186 183
-1
packages/cloudflare/src/cli/build/patches/index.ts
··· 1 - export * from "./investigated/index.js";
+1 -1
packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts packages/cloudflare/src/cli/build/utils/copy-package-cli-files.ts
··· 3 3 4 4 import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; 5 5 6 - import { getOutputWorkerPath } from "../../bundle-server.js"; 6 + import { getOutputWorkerPath } from "../bundle-server.js"; 7 7 8 8 /** 9 9 * Copies
-2
packages/cloudflare/src/cli/build/patches/investigated/index.ts
··· 1 - export * from "./copy-package-cli-files.js"; 2 - export * from "./patch-require.js";
-6
packages/cloudflare/src/cli/build/patches/investigated/patch-require.ts
··· 1 - /** 2 - * Replaces webpack `__require` with actual `require` 3 - */ 4 - export function patchRequire(code: string): string { 5 - return code.replace(/__require\d?\(/g, "require(").replace(/__require\d?\./g, "require."); 6 - }
-29
packages/cloudflare/src/cli/build/utils/apply-patches.ts
··· 1 - /** 2 - * Applies multiple code patches in order to a given piece of code, at each step it validates that the code 3 - * has actually been patched/changed, if not an error is thrown 4 - * 5 - * @param code the code to apply the patches to 6 - * @param patches array of tuples, containing a string indicating the target of the patching (for logging) and 7 - * a patching function that takes a string (pre-patch code) and returns a string (post-patch code) 8 - * @returns the patched code 9 - */ 10 - export async function patchCodeWithValidations( 11 - code: string, 12 - patches: [string, (code: string) => string | Promise<string>, opts?: { isOptional?: boolean }][] 13 - ): Promise<string> { 14 - console.log(`Applying code patches:`); 15 - let patchedCode = code; 16 - 17 - for (const [target, patchFunction, opts] of patches) { 18 - console.log(` - patching ${target}`); 19 - 20 - const prePatchCode = patchedCode; 21 - patchedCode = await patchFunction(patchedCode); 22 - 23 - if (!opts?.isOptional && prePatchCode === patchedCode) { 24 - throw new Error(`Failed to patch ${target}`); 25 - } 26 - } 27 - 28 - return patchedCode; 29 - }
+1 -1
packages/cloudflare/src/cli/build/utils/index.ts
··· 1 - export * from "./apply-patches.js"; 1 + export * from "./copy-package-cli-files.js"; 2 2 export * from "./create-config-files.js"; 3 3 export * from "./ensure-cf-config.js"; 4 4 export * from "./extract-project-env-vars.js";