@@ -3,7 +3,7 @@ import { mkdirSync } from "node:fs";
33import fs from "node:fs" ;
44import path from "node:path" ;
55import { build } from "esbuild" ;
6- import type { MiddlewareInfo , MiddlewareManifest } from "types/next-types" ;
6+ import type { MiddlewareInfo } from "types/next-types" ;
77import type {
88 IncludedConverter ,
99 IncludedOriginResolver ,
@@ -13,6 +13,7 @@ import type {
1313 SplittedFunctionOptions ,
1414} from "types/open-next" ;
1515
16+ import { loadMiddlewareManifest } from "config/util.js" ;
1617import type { OriginResolver } from "types/overrides.js" ;
1718import logger from "../../logger.js" ;
1819import { openNextEdgePlugins } from "../../plugins/edge.js" ;
@@ -173,23 +174,19 @@ export async function generateEdgeBundle(
173174 options : BuildOptions ,
174175 fnOptions : SplittedFunctionOptions ,
175176) {
176- const { appBuildOutputPath, outputDir } = options ;
177177 logger . info ( `Generating edge bundle for: ${ name } ` ) ;
178178
179+ const buildOutputDotNextDir = path . join ( options . appBuildOutputPath , ".next" ) ;
180+
179181 // Create output folder
180- const outputPath = path . join ( outputDir , "server-functions" , name ) ;
181- fs . mkdirSync ( outputPath , { recursive : true } ) ;
182+ const outputDir = path . join ( options . outputDir , "server-functions" , name ) ;
183+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
182184
183185 // Copy open-next.config.mjs
184- copyOpenNextConfig ( options . buildDir , outputPath , true ) ;
186+ copyOpenNextConfig ( options . buildDir , outputDir , true ) ;
185187
186188 // Load middleware manifest
187- const middlewareManifest = JSON . parse (
188- fs . readFileSync (
189- path . join ( appBuildOutputPath , ".next/server/middleware-manifest.json" ) ,
190- "utf8" ,
191- ) ,
192- ) as MiddlewareManifest ;
189+ const middlewareManifest = loadMiddlewareManifest ( buildOutputDotNextDir ) ;
193190
194191 // Find functions
195192 const functions = Object . values ( middlewareManifest . functions ) . filter ( ( fn ) =>
@@ -203,32 +200,28 @@ export async function generateEdgeBundle(
203200
204201 //Copy wasm files
205202 const wasmFiles = middlewareInfo . wasm ;
206- mkdirSync ( path . join ( outputPath , "wasm" ) , { recursive : true } ) ;
203+ mkdirSync ( path . join ( outputDir , "wasm" ) , { recursive : true } ) ;
207204 for ( const wasmFile of wasmFiles ) {
208205 fs . copyFileSync (
209- path . join ( appBuildOutputPath , ".next" , wasmFile . filePath ) ,
210- path . join ( outputPath , `wasm/${ wasmFile . name } .wasm` ) ,
206+ path . join ( buildOutputDotNextDir , wasmFile . filePath ) ,
207+ path . join ( outputDir , `wasm/${ wasmFile . name } .wasm` ) ,
211208 ) ;
212209 }
213210
214211 // Copy assets
215212 const assets = middlewareInfo . assets ;
216- mkdirSync ( path . join ( outputPath , "assets" ) , { recursive : true } ) ;
213+ mkdirSync ( path . join ( outputDir , "assets" ) , { recursive : true } ) ;
217214 for ( const asset of assets ) {
218215 fs . copyFileSync (
219- path . join ( appBuildOutputPath , ".next" , asset . filePath ) ,
220- path . join ( outputPath , `assets/${ asset . name } ` ) ,
216+ path . join ( buildOutputDotNextDir , asset . filePath ) ,
217+ path . join ( outputDir , `assets/${ asset . name } ` ) ,
221218 ) ;
222219 }
223220
224221 await buildEdgeBundle ( {
225222 middlewareInfo,
226- entrypoint : path . join (
227- options . openNextDistDir ,
228- "adapters" ,
229- "edge-adapter.js" ,
230- ) ,
231- outfile : path . join ( outputPath , "index.mjs" ) ,
223+ entrypoint : path . join ( options . openNextDistDir , "adapters/edge-adapter.js" ) ,
224+ outfile : path . join ( outputDir , "index.mjs" ) ,
232225 options,
233226 overrides : fnOptions . override ,
234227 additionalExternals : options . config . edgeExternals ,
0 commit comments