Skip to content

Commit 65f7846

Browse files
committed
feat: support for a custom OpenNext config path
1 parent 9b28913 commit 65f7846

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

.changeset/real-bats-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
feat: support for a custom OpenNext config path with the `--openNextConfigPath` flag
File renamed without changes.

examples/overrides/r2-incremental-cache/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "next build",
88
"start": "next start",
99
"lint": "next lint",
10-
"build:worker": "pnpm opennextjs-cloudflare build",
10+
"build:worker": "pnpm opennextjs-cloudflare build --openNextConfigPath=./.custom-config/my-opennext-config.ts",
1111
"preview:worker": "pnpm opennextjs-cloudflare preview",
1212
"preview": "pnpm build:worker && pnpm preview:worker",
1313
"e2e": "playwright test -c e2e/playwright.config.ts"

packages/cloudflare/src/cli/build/utils/create-config-files.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,26 @@ export async function getLatestCompatDate(): Promise<string | undefined> {
8888
}
8989

9090
/**
91-
* Creates a `open-next.config.ts` file for the user if it doesn't exist, but only after asking for the user's confirmation.
91+
* Creates a config file for the user if it doesn't exist, but only after asking for the user's confirmation.
9292
*
9393
* If the user refuses an error is thrown (since the file is mandatory).
9494
*
95+
* @param configPath The path to the config file, relative to the source directory
9596
* @param sourceDir The source directory for the project
9697
*/
97-
export async function createOpenNextConfigIfNotExistent(sourceDir: string): Promise<void> {
98-
const openNextConfigPath = join(sourceDir, "open-next.config.ts");
98+
export async function createOpenNextConfigIfNotExistent(
99+
configPath: string,
100+
sourceDir: string
101+
): Promise<void> {
102+
const openNextConfigPath = join(sourceDir, configPath);
99103

100104
if (!existsSync(openNextConfigPath)) {
101105
const answer = await askConfirmation(
102-
"Missing required `open-next.config.ts` file, do you want to create one?"
106+
`Missing required \`${configPath}\` file, do you want to create one?`
103107
);
104108

105109
if (!answer) {
106-
throw new Error("The `open-next.config.ts` file is required, aborting!");
110+
throw new Error(`The \`${configPath}\` file is required, aborting!`);
107111
}
108112

109113
cpSync(join(getPackageTemplatesDirPath(), "open-next.config.ts"), openNextConfigPath);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ async function buildCommand(
2222
skipNextBuild: boolean;
2323
noMinify: boolean;
2424
skipWranglerConfigCheck: boolean;
25+
openNextConfigPath: string;
2526
}>
2627
): Promise<void> {
2728
printHeaders("build");
2829

29-
const { config, buildDir } = await compileConfig();
30+
const { config, buildDir } = await compileConfig(args.openNextConfigPath);
3031
const options = getNormalizedOptions(config, buildDir);
3132

3233
const wranglerConfig = readWranglerConfig(args);
@@ -65,6 +66,11 @@ export function addBuildCommand<T extends yargs.Argv>(y: T) {
6566
type: "boolean",
6667
default: ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
6768
desc: "Skip checking for a Wrangler config",
69+
})
70+
.option("openNextConfigPath", {
71+
type: "string",
72+
default: "open-next.config.ts",
73+
desc: "Path to OpenNext configuration file, relative to the source directory",
6874
}),
6975
(args) => buildCommand(withWranglerPassthroughArgs(args))
7076
);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ export function printHeaders(command: string) {
3737
*
3838
* @returns OpenNext config.
3939
*/
40-
export async function compileConfig() {
41-
await createOpenNextConfigIfNotExistent(nextAppDir);
40+
export async function compileConfig(configPath: string) {
41+
await createOpenNextConfigIfNotExistent(configPath, nextAppDir);
4242

43-
const { config, buildDir } = await compileOpenNextConfig(nextAppDir, undefined, { compileEdge: true });
43+
const { config, buildDir } = await compileOpenNextConfig(nextAppDir, configPath, { compileEdge: true });
4444
ensureCloudflareConfig(config);
4545

4646
return { config, buildDir };

0 commit comments

Comments
 (0)