Skip to content

Commit 3994aca

Browse files
authored
refactor: compileOpenNextConfig now take a path to the config file (opennextjs#972)
1 parent ba02f6b commit 3994aca

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
refactor: `compileOpenNextConfig` now takes `openNextConfigPath` only and no more `baseDir`.
6+
7+
`openNextConfigPath` is now in line with fs APIs: it is either absolute or relative to the working directory (`cwd`).

packages/open-next/src/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "node:path";
12
import url from "node:url";
23

34
import {
@@ -32,8 +33,7 @@ export async function build(
3233
const openNextDistDir = url.fileURLToPath(new URL(".", import.meta.url));
3334

3435
const { config, buildDir } = await compileOpenNextConfig(
35-
baseDir,
36-
openNextConfigPath,
36+
path.join(baseDir, openNextConfigPath ?? "open-next.config.ts"),
3737
{ nodeExternals },
3838
);
3939

packages/open-next/src/build/compileConfig.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,18 @@ import { validateConfig } from "./validateConfig.js";
1313
*
1414
* The configuration is always compiled for Node.js and for the edge only if needed.
1515
*
16-
* @param baseDir Directory where to look for the configuration.
17-
* @param openNextConfigPath Override the default configuration when provided. Relative to baseDir.
16+
* @param openNextConfigPath Path to the configuration file. Absolute or relative to cwd.
1817
* @param nodeExternals Coma separated list of Externals for the Node.js compilation.
1918
* @param compileEdge Force compiling for the edge runtime when true
2019
* @return The configuration and the build directory.
2120
*/
2221
export async function compileOpenNextConfig(
23-
baseDir: string,
24-
openNextConfigPath?: string,
22+
openNextConfigPath: string,
2523
{ nodeExternals = "", compileEdge = false } = {},
2624
) {
27-
const sourcePath = path.join(
28-
baseDir,
29-
openNextConfigPath ?? "open-next.config.ts",
30-
);
31-
3225
const buildDir = fs.mkdtempSync(path.join(os.tmpdir(), "open-next-tmp"));
3326
let configPath = compileOpenNextConfigNode(
34-
sourcePath,
27+
openNextConfigPath,
3528
buildDir,
3629
nodeExternals.split(","),
3730
);
@@ -54,7 +47,11 @@ export async function compileOpenNextConfig(
5447
(config.middleware?.external && config.middleware.runtime !== "node") ||
5548
Object.values(config.functions || {}).some((fn) => fn.runtime === "edge");
5649
if (usesEdgeRuntime || compileEdge) {
57-
compileOpenNextConfigEdge(sourcePath, buildDir, config.edgeExternals ?? []);
50+
compileOpenNextConfigEdge(
51+
openNextConfigPath,
52+
buildDir,
53+
config.edgeExternals ?? [],
54+
);
5855
} else {
5956
// Skip compiling for the edge runtime.
6057
logger.debug(
@@ -65,22 +62,30 @@ export async function compileOpenNextConfig(
6562
return { config, buildDir };
6663
}
6764

65+
/**
66+
* Compiles the OpenNext configuration for Node.
67+
*
68+
* @param openNextConfigPath Path to the configuration file. Absolute or relative to cwd.
69+
* @param outputDir Folder where to output the compiled config file (`open-next.config.mjs`).
70+
* @param externals List of packages that should not be bundled.
71+
* @return Path to the compiled config.
72+
*/
6873
export function compileOpenNextConfigNode(
69-
sourcePath: string,
74+
openNextConfigPath: string,
7075
outputDir: string,
7176
externals: string[],
7277
) {
7378
const outputPath = path.join(outputDir, "open-next.config.mjs");
7479
logger.debug("Compiling open-next.config.ts for Node.", outputPath);
7580

7681
//Check if open-next.config.ts exists
77-
if (!fs.existsSync(sourcePath)) {
82+
if (!fs.existsSync(openNextConfigPath)) {
7883
//Create a simple open-next.config.mjs file
7984
logger.debug("Cannot find open-next.config.ts. Using default config.");
8085
fs.writeFileSync(outputPath, "export default { default: { } };");
8186
} else {
8287
buildSync({
83-
entryPoints: [sourcePath],
88+
entryPoints: [openNextConfigPath],
8489
outfile: outputPath,
8590
bundle: true,
8691
format: "esm",
@@ -101,16 +106,24 @@ export function compileOpenNextConfigNode(
101106
return outputPath;
102107
}
103108

109+
/**
110+
* Compiles the OpenNext configuration for Edge.
111+
*
112+
* @param openNextConfigPath Path to the configuration file. Absolute or relative to cwd.
113+
* @param outputDir Folder where to output the compiled config file (`open-next.config.edge.mjs`).
114+
* @param externals List of packages that should not be bundled.
115+
* @return Path to the compiled config.
116+
*/
104117
export function compileOpenNextConfigEdge(
105-
sourcePath: string,
118+
openNextConfigPath: string,
106119
outputDir: string,
107120
externals: string[],
108121
) {
109122
const outputPath = path.join(outputDir, "open-next.config.edge.mjs");
110123
logger.debug("Compiling open-next.config.ts for edge runtime.", outputPath);
111124

112125
buildSync({
113-
entryPoints: [sourcePath],
126+
entryPoints: [openNextConfigPath],
114127
outfile: outputPath,
115128
bundle: true,
116129
format: "esm",
@@ -123,4 +136,6 @@ export function compileOpenNextConfigEdge(
123136
"process.env.NODE_ENV": '"production"',
124137
},
125138
});
139+
140+
return outputPath;
126141
}

0 commit comments

Comments
 (0)