@@ -13,25 +13,18 @@ import { validateConfig } from "./validateConfig.js";
13
13
*
14
14
* The configuration is always compiled for Node.js and for the edge only if needed.
15
15
*
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.
18
17
* @param nodeExternals Coma separated list of Externals for the Node.js compilation.
19
18
* @param compileEdge Force compiling for the edge runtime when true
20
19
* @return The configuration and the build directory.
21
20
*/
22
21
export async function compileOpenNextConfig (
23
- baseDir : string ,
24
- openNextConfigPath ?: string ,
22
+ openNextConfigPath : string ,
25
23
{ nodeExternals = "" , compileEdge = false } = { } ,
26
24
) {
27
- const sourcePath = path . join (
28
- baseDir ,
29
- openNextConfigPath ?? "open-next.config.ts" ,
30
- ) ;
31
-
32
25
const buildDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "open-next-tmp" ) ) ;
33
26
let configPath = compileOpenNextConfigNode (
34
- sourcePath ,
27
+ openNextConfigPath ,
35
28
buildDir ,
36
29
nodeExternals . split ( "," ) ,
37
30
) ;
@@ -54,7 +47,11 @@ export async function compileOpenNextConfig(
54
47
( config . middleware ?. external && config . middleware . runtime !== "node" ) ||
55
48
Object . values ( config . functions || { } ) . some ( ( fn ) => fn . runtime === "edge" ) ;
56
49
if ( usesEdgeRuntime || compileEdge ) {
57
- compileOpenNextConfigEdge ( sourcePath , buildDir , config . edgeExternals ?? [ ] ) ;
50
+ compileOpenNextConfigEdge (
51
+ openNextConfigPath ,
52
+ buildDir ,
53
+ config . edgeExternals ?? [ ] ,
54
+ ) ;
58
55
} else {
59
56
// Skip compiling for the edge runtime.
60
57
logger . debug (
@@ -65,22 +62,30 @@ export async function compileOpenNextConfig(
65
62
return { config, buildDir } ;
66
63
}
67
64
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
+ */
68
73
export function compileOpenNextConfigNode (
69
- sourcePath : string ,
74
+ openNextConfigPath : string ,
70
75
outputDir : string ,
71
76
externals : string [ ] ,
72
77
) {
73
78
const outputPath = path . join ( outputDir , "open-next.config.mjs" ) ;
74
79
logger . debug ( "Compiling open-next.config.ts for Node." , outputPath ) ;
75
80
76
81
//Check if open-next.config.ts exists
77
- if ( ! fs . existsSync ( sourcePath ) ) {
82
+ if ( ! fs . existsSync ( openNextConfigPath ) ) {
78
83
//Create a simple open-next.config.mjs file
79
84
logger . debug ( "Cannot find open-next.config.ts. Using default config." ) ;
80
85
fs . writeFileSync ( outputPath , "export default { default: { } };" ) ;
81
86
} else {
82
87
buildSync ( {
83
- entryPoints : [ sourcePath ] ,
88
+ entryPoints : [ openNextConfigPath ] ,
84
89
outfile : outputPath ,
85
90
bundle : true ,
86
91
format : "esm" ,
@@ -101,16 +106,24 @@ export function compileOpenNextConfigNode(
101
106
return outputPath ;
102
107
}
103
108
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
+ */
104
117
export function compileOpenNextConfigEdge (
105
- sourcePath : string ,
118
+ openNextConfigPath : string ,
106
119
outputDir : string ,
107
120
externals : string [ ] ,
108
121
) {
109
122
const outputPath = path . join ( outputDir , "open-next.config.edge.mjs" ) ;
110
123
logger . debug ( "Compiling open-next.config.ts for edge runtime." , outputPath ) ;
111
124
112
125
buildSync ( {
113
- entryPoints : [ sourcePath ] ,
126
+ entryPoints : [ openNextConfigPath ] ,
114
127
outfile : outputPath ,
115
128
bundle : true ,
116
129
format : "esm" ,
@@ -123,4 +136,6 @@ export function compileOpenNextConfigEdge(
123
136
"process.env.NODE_ENV" : '"production"' ,
124
137
} ,
125
138
} ) ;
139
+
140
+ return outputPath ;
126
141
}
0 commit comments