@@ -5,10 +5,13 @@ import os from "node:os";
5
5
import path from "node:path" ;
6
6
import url from "node:url" ;
7
7
8
- import { buildSync } from "esbuild" ;
9
8
import { MiddlewareManifest } from "types/next-types.js" ;
10
9
11
10
import { isBinaryContentType } from "./adapters/binary.js" ;
11
+ import {
12
+ compileOpenNextConfigEdge ,
13
+ compileOpenNextConfigNode ,
14
+ } from "./build/compileConfig.js" ;
12
15
import { createServerBundle } from "./build/createServerBundle.js" ;
13
16
import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js" ;
14
17
import { generateOutput } from "./build/generateOutput.js" ;
@@ -39,15 +42,24 @@ export type PublicFiles = {
39
42
files : string [ ] ;
40
43
} ;
41
44
42
- export async function build ( openNextConfigPath ?: string ) {
45
+ export async function build (
46
+ openNextConfigPath ?: string ,
47
+ nodeExternals ?: string ,
48
+ ) {
43
49
showWindowsWarning ( ) ;
44
50
45
51
// Load open-next.config.ts
46
52
const tempDir = initTempDir ( ) ;
47
- const configPath = compileOpenNextConfig ( tempDir , openNextConfigPath ) ;
53
+ const configPath = compileOpenNextConfigNode (
54
+ tempDir ,
55
+ openNextConfigPath ,
56
+ nodeExternals ,
57
+ ) ;
48
58
config = ( await import ( configPath ) ) . default as OpenNextConfig ;
49
59
validateConfig ( config ) ;
50
60
61
+ compileOpenNextConfigEdge ( tempDir , config , openNextConfigPath ) ;
62
+
51
63
const { root : monorepoRoot , packager } = findMonorepoRoot (
52
64
path . join ( process . cwd ( ) , config . appPath || "." ) ,
53
65
) ;
@@ -107,40 +119,6 @@ function initTempDir() {
107
119
return tempDir ;
108
120
}
109
121
110
- function compileOpenNextConfig ( tempDir : string , openNextConfigPath ?: string ) {
111
- const sourcePath = path . join (
112
- process . cwd ( ) ,
113
- openNextConfigPath ?? "open-next.config.ts" ,
114
- ) ;
115
- const outputPath = path . join ( tempDir , "open-next.config.mjs" ) ;
116
-
117
- //Check if open-next.config.ts exists
118
- if ( ! fs . existsSync ( sourcePath ) ) {
119
- //Create a simple open-next.config.mjs file
120
- logger . debug ( "Cannot find open-next.config.ts. Using default config." ) ;
121
- fs . writeFileSync (
122
- outputPath ,
123
- [
124
- "var config = { default: { } };" ,
125
- "var open_next_config_default = config;" ,
126
- "export { open_next_config_default as default };" ,
127
- ] . join ( "\n" ) ,
128
- ) ;
129
- } else {
130
- buildSync ( {
131
- entryPoints : [ sourcePath ] ,
132
- outfile : outputPath ,
133
- bundle : true ,
134
- format : "esm" ,
135
- target : [ "node18" ] ,
136
- external : [ "node:*" ] ,
137
- platform : "neutral" ,
138
- } ) ;
139
- }
140
-
141
- return outputPath ;
142
- }
143
-
144
122
function checkRunningInsideNextjsApp ( ) {
145
123
const { appPath } = options ;
146
124
const extension = [ "js" , "cjs" , "mjs" ] . find ( ( ext ) =>
@@ -228,9 +206,22 @@ function initOutputDir() {
228
206
path . join ( tempDir , "open-next.config.mjs" ) ,
229
207
"utf8" ,
230
208
) ;
209
+ let openNextConfigEdge : string | null = null ;
210
+ if ( fs . existsSync ( path . join ( tempDir , "open-next.config.edge.mjs" ) ) ) {
211
+ openNextConfigEdge = readFileSync (
212
+ path . join ( tempDir , "open-next.config.edge.mjs" ) ,
213
+ "utf8" ,
214
+ ) ;
215
+ }
231
216
fs . rmSync ( outputDir , { recursive : true , force : true } ) ;
232
217
fs . mkdirSync ( tempDir , { recursive : true } ) ;
233
218
fs . writeFileSync ( path . join ( tempDir , "open-next.config.mjs" ) , openNextConfig ) ;
219
+ if ( openNextConfigEdge ) {
220
+ fs . writeFileSync (
221
+ path . join ( tempDir , "open-next.config.edge.mjs" ) ,
222
+ openNextConfigEdge ,
223
+ ) ;
224
+ }
234
225
}
235
226
236
227
async function createWarmerBundle ( config : OpenNextConfig ) {
0 commit comments