@@ -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 */
2221export 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+ */
6873export 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+ */
104117export 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