Skip to content

Commit 87149a2

Browse files
committed
feat: add serverExternals option to CloudflareOverrides and update build process
1 parent c399fa8 commit 87149a2

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

packages/cloudflare/src/api/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ export type CloudflareOverrides = {
5555
* @default "none"
5656
*/
5757
routePreloadingBehavior?: RoutePreloadingBehavior;
58+
59+
/**
60+
* List of external modules to exclude from the server bundle.
61+
*/
62+
serverExternals?: string[];
5863
};
5964

6065
/**
@@ -71,6 +76,7 @@ export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNe
7176
cachePurge,
7277
enableCacheInterception = false,
7378
routePreloadingBehavior = "none",
79+
serverExternals = [],
7480
} = config;
7581

7682
return {
@@ -106,6 +112,7 @@ export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNe
106112
},
107113
assetResolver: () => assetResolver,
108114
},
115+
serverExternals,
109116
};
110117
}
111118

@@ -180,6 +187,7 @@ interface OpenNextConfig extends AwsOpenNextConfig {
180187
maxVersionAgeDays?: number;
181188
};
182189
};
190+
serverExternals?: string[];
183191
}
184192

185193
/**

packages/cloudflare/src/cli/build/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import logger from "@opennextjs/aws/logger.js";
88
import type { Unstable_Config } from "wrangler";
99

1010
import { OpenNextConfig } from "../../api/config.js";
11+
import { BuildOptions } from "../commands/utils.js";
1112
import type { ProjectOptions } from "../project-options.js";
1213
import { bundleServer } from "./bundle-server.js";
1314
import { compileCacheAssetsManifestSqlFile } from "./open-next/compile-cache-assets-manifest.js";
@@ -29,7 +30,7 @@ import { getVersion } from "./utils/version.js";
2930
* @param projectOpts The options for the project
3031
*/
3132
export async function build(
32-
options: buildHelper.BuildOptions,
33+
options: BuildOptions,
3334
config: OpenNextConfig,
3435
projectOpts: ProjectOptions,
3536
wranglerConfig: Unstable_Config

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { readFile, writeFile } from "node:fs/promises";
33
import path from "node:path";
44
import { fileURLToPath } from "node:url";
55

6-
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
6+
import { getPackagePath } from "@opennextjs/aws/build/helper.js";
77
import { ContentUpdater } from "@opennextjs/aws/plugins/content-updater.js";
88
import { build, type Plugin } from "esbuild";
99

1010
import { getOpenNextConfig } from "../../api/config.js";
11+
import { BuildOptions } from "../commands/utils.js";
1112
import type { ProjectOptions } from "../project-options.js";
1213
import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js";
1314
import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js";
@@ -111,7 +112,7 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
111112
// Apply updater updates, must be the last plugin
112113
updater.plugin,
113114
] as Plugin[],
114-
external: ["./middleware/handler.mjs"],
115+
external: ["./middleware/handler.mjs", ...buildOpts.config.serverExternals || []],
115116
alias: {
116117
// Workers have `fetch` so the `node-fetch` polyfill is not needed
117118
"next/dist/compiled/node-fetch": path.join(buildOpts.outputDir, "cloudflare-templates/shims/fetch.js"),

packages/cloudflare/src/cli/commands/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@ export function getNormalizedOptions(config: OpenNextConfig, buildDir = nextAppD
9191
const options = normalizeOptions(config, openNextDistDir, buildDir);
9292
logger.setLevel(options.debug ? "debug" : "info");
9393

94-
return options;
94+
return {
95+
...options,
96+
config,
97+
};
9598
}
9699

100+
export type BuildOptions = ReturnType<typeof getNormalizedOptions>;
101+
97102
/**
98103
* Read the Wrangler config.
99104
*

0 commit comments

Comments
 (0)