11import cp from "node:child_process" ;
2- import fs , { readFileSync } from "node:fs" ;
2+ import fs from "node:fs" ;
33import { createRequire as topLevelCreateRequire } from "node:module" ;
44import os from "node:os" ;
55import path from "node:path" ;
@@ -47,9 +47,9 @@ export async function build(
4747 showWindowsWarning ( ) ;
4848
4949 // Load open-next.config.ts
50- const tempDir = initTempDir ( ) ;
50+ const tempConfigDir = fs . mkdtempSync ( "open-next" ) ;
5151 let configPath = compileOpenNextConfigNode (
52- tempDir ,
52+ tempConfigDir ,
5353 openNextConfigPath ,
5454 nodeExternals ,
5555 ) ;
@@ -64,10 +64,10 @@ export async function build(
6464 }
6565 validateConfig ( config ) ;
6666
67- compileOpenNextConfigEdge ( tempDir , config , openNextConfigPath ) ;
67+ compileOpenNextConfigEdge ( tempConfigDir , config , openNextConfigPath ) ;
6868
6969 // Initialize options
70- const options = normalizeOptions ( config ) ;
70+ const options = normalizeOptions ( config , tempConfigDir ) ;
7171 logger . setLevel ( options . debug ? "debug" : "info" ) ;
7272
7373 // Pre-build validation
@@ -82,7 +82,7 @@ export async function build(
8282
8383 // Generate deployable bundle
8484 printHeader ( "Generating bundle" ) ;
85- initOutputDir ( tempDir , options ) ;
85+ initOutputDir ( options ) ;
8686
8787 // Compile cache.ts
8888 compileCache ( options ) ;
@@ -113,14 +113,6 @@ function showWindowsWarning() {
113113 ) ;
114114}
115115
116- function initTempDir ( ) {
117- const dir = path . join ( process . cwd ( ) , ".open-next" ) ;
118- const tempDir = path . join ( dir , ".build" ) ;
119- fs . rmSync ( dir , { recursive : true , force : true } ) ;
120- fs . mkdirSync ( tempDir , { recursive : true } ) ;
121- return tempDir ;
122- }
123-
124116function checkRunningInsideNextjsApp ( options : BuildOptions ) {
125117 const { appPath } = options ;
126118 const extension = [ "js" , "cjs" , "mjs" , "ts" ] . find ( ( ext ) =>
@@ -175,30 +167,26 @@ function printOpenNextVersion(options: BuildOptions) {
175167 logger . info ( `OpenNext v${ options . openNextVersion } ` ) ;
176168}
177169
178- function initOutputDir ( srcTempDir : string , options : BuildOptions ) {
170+ function initOutputDir ( options : BuildOptions ) {
179171 // We need to get the build relative to the cwd to find the compiled config
180172 // This is needed for the case where the app is a single-version monorepo and the package.json is in the root of the monorepo
181173 // where the build is in the app directory, but the compiled config is in the root of the monorepo.
182- const openNextConfig = readFileSync (
183- path . join ( srcTempDir , "open-next.config.mjs" ) ,
184- "utf8" ,
185- ) ;
186- let openNextConfigEdge : string | null = null ;
187- if ( fs . existsSync ( path . join ( srcTempDir , "open-next.config.edge.mjs" ) ) ) {
188- openNextConfigEdge = readFileSync (
189- path . join ( srcTempDir , "open-next.config.edge.mjs" ) ,
190- "utf8" ,
191- ) ;
192- }
193174 fs . rmSync ( options . outputDir , { recursive : true , force : true } ) ;
194175 const { buildDir } = options ;
195176 fs . mkdirSync ( buildDir , { recursive : true } ) ;
196- fs . writeFileSync ( path . join ( buildDir , "open-next.config.mjs" ) , openNextConfig ) ;
197- if ( openNextConfigEdge ) {
198- fs . writeFileSync (
177+
178+ fs . copyFileSync (
179+ path . join ( options . tempConfigDir , "open-next.config.mjs" ) ,
180+ path . join ( buildDir , "open-next.config.mjs" ) ,
181+ ) ;
182+
183+ try {
184+ fs . copyFileSync (
185+ path . join ( options . tempConfigDir , "open-next.config.edge.mjs" ) ,
199186 path . join ( buildDir , "open-next.config.edge.mjs" ) ,
200- openNextConfigEdge ,
201187 ) ;
188+ } catch {
189+ // The edge config does not always exist.
202190 }
203191}
204192
@@ -692,7 +680,7 @@ async function createMiddleware(options: BuildOptions) {
692680
693681 // Get middleware manifest
694682 const middlewareManifest = JSON . parse (
695- readFileSync (
683+ fs . readFileSync (
696684 path . join ( appBuildOutputPath , ".next/server/middleware-manifest.json" ) ,
697685 "utf8" ,
698686 ) ,
0 commit comments