Skip to content

Commit d6d8c8d

Browse files
committed
Add support for passing multiple configs
1 parent f1c73cc commit d6d8c8d

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

.changeset/weak-crews-tickle.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
Support multiple Wrangler configuration files via `--config` flag
6+
7+
Enable passing multiple configuration files to the OpenNext.js Cloudflare CLI
8+
using the `--config` flag, matching Wrangler's native capability. This allows
9+
running multiple workers in a single dev session, which is essential for RPC
10+
communication with Durable Objects during local development as documented in the
11+
[Wrangler API bindings guide](https://developers.cloudflare.com/workers/wrangler/api/#supported-bindings)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: num
2828
const wranglerConfig = readWranglerConfig(args);
2929

3030
const envVars = await getEnvFromPlatformProxy({
31-
configPath: args.wranglerConfigPath,
31+
configPath: args.nextjsWranglerConfigPath,
3232
environment: args.env,
3333
});
3434

@@ -37,7 +37,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3737
await populateCache(options, config, wranglerConfig, {
3838
target: "remote",
3939
environment: args.env,
40-
wranglerConfigPath: args.wranglerConfigPath,
40+
wranglerConfigPath: args.nextjsWranglerConfigPath,
4141
cacheChunkSize: args.cacheChunkSize,
4242
});
4343

packages/cloudflare/src/cli/commands/populate-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ async function populateCacheCommand(
313313
await populateCache(options, config, wranglerConfig, {
314314
target,
315315
environment: args.env,
316-
wranglerConfigPath: args.wranglerConfigPath,
316+
wranglerConfigPath: args.nextjsWranglerConfigPath,
317317
cacheChunkSize: args.cacheChunkSize,
318318
});
319319
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: nu
2727
await populateCache(options, config, wranglerConfig, {
2828
target: "local",
2929
environment: args.env,
30-
wranglerConfigPath: args.wranglerConfigPath,
30+
wranglerConfigPath: args.nextjsWranglerConfigPath,
3131
cacheChunkSize: args.cacheChunkSize,
3232
});
3333

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: num
2828
const wranglerConfig = readWranglerConfig(args);
2929

3030
const envVars = await getEnvFromPlatformProxy({
31-
configPath: args.wranglerConfigPath,
31+
configPath: args.nextjsWranglerConfigPath,
3232
environment: args.env,
3333
});
3434

@@ -37,7 +37,7 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3737
await populateCache(options, config, wranglerConfig, {
3838
target: "remote",
3939
environment: args.env,
40-
wranglerConfigPath: args.wranglerConfigPath,
40+
wranglerConfigPath: args.nextjsWranglerConfigPath,
4141
cacheChunkSize: args.cacheChunkSize,
4242
});
4343

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { createOpenNextConfigIfNotExistent, ensureCloudflareConfig } from "../bu
1515
export type WithWranglerArgs<T = unknown> = T & {
1616
// Array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
1717
wranglerArgs: string[];
18-
wranglerConfigPath: string | undefined;
18+
wranglerConfigPath: string[] | undefined;
19+
// The first wrangler config path passed into the CLI, if any. Assumed to be the one used for OpenNext.
20+
nextjsWranglerConfigPath: string | undefined;
1921
env: string | undefined;
2022
};
2123

@@ -101,7 +103,7 @@ export function getNormalizedOptions(config: OpenNextConfig, buildDir = nextAppD
101103
* @returns Wrangler config.
102104
*/
103105
export function readWranglerConfig(args: WithWranglerArgs) {
104-
return unstable_readConfig({ env: args.env, config: args.wranglerConfigPath });
106+
return unstable_readConfig({ env: args.env, config: args.nextjsWranglerConfigPath });
105107
}
106108

107109
/**
@@ -111,6 +113,7 @@ export function withWranglerOptions<T extends yargs.Argv>(args: T) {
111113
return args
112114
.option("config", {
113115
type: "string",
116+
array: true,
114117
alias: "c",
115118
desc: "Path to Wrangler configuration file",
116119
})
@@ -128,7 +131,7 @@ export function withWranglerOptions<T extends yargs.Argv>(args: T) {
128131

129132
type WranglerInputArgs = {
130133
configPath: string | undefined;
131-
config: string | undefined;
134+
config: string[] | undefined;
132135
env: string | undefined;
133136
};
134137

@@ -143,15 +146,15 @@ function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }):
143146

144147
if (args.config) {
145148
logger.error(
146-
"Multiple config flags found. Please use the `--config` flag for your Wrangler config path."
149+
"Duplicate config flags found. Unable to pass both `--config` and `--configPath`. Please use the `--config` flag for your Wrangler config path."
147150
);
148151
process.exit(1);
149152
}
150153
}
151154

152155
return [
153156
...(args.configPath ? ["--config", args.configPath] : []),
154-
...(args.config ? ["--config", args.config] : []),
157+
...(args.config ? args.config.flatMap((c) => ["--config", c]) : []),
155158
...(args.env ? ["--env", args.env] : []),
156159
// Note: the first args in `_` will be the commands.
157160
...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`),
@@ -166,9 +169,15 @@ function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }):
166169
export function withWranglerPassthroughArgs<T extends yargs.ArgumentsCamelCase<WranglerInputArgs>>(
167170
args: T
168171
): WithWranglerArgs<T> {
172+
const wranglerConfigPath = args.config ?? (args.configPath ? [args.configPath] : undefined);
173+
if (wranglerConfigPath && wranglerConfigPath?.length > 1) {
174+
logger.info("Multiple Wrangler config paths found, first config assumed as opennext config.");
175+
}
176+
169177
return {
170178
...args,
171-
wranglerConfigPath: args.config ?? args.configPath,
179+
wranglerConfigPath,
180+
nextjsWranglerConfigPath: wranglerConfigPath?.[0],
172181
wranglerArgs: getWranglerArgs(args),
173182
};
174183
}

0 commit comments

Comments
 (0)