44import fs from "node:fs" ;
55import path from "node:path" ;
66
7+ import { loadMiddlewareManifest } from "@opennextjs/aws/adapters/config/util.js" ;
78import { bundleNextServer } from "@opennextjs/aws/build/bundleNextServer.js" ;
89import { compileCache } from "@opennextjs/aws/build/compileCache.js" ;
910import { copyTracedFiles } from "@opennextjs/aws/build/copyTracedFiles.js" ;
10- import { generateEdgeBundle } from "@opennextjs/aws/build/edge/createEdgeBundle.js" ;
11+ import { copyMiddlewareResources , generateEdgeBundle } from "@opennextjs/aws/build/edge/createEdgeBundle.js" ;
1112import * as buildHelper from "@opennextjs/aws/build/helper.js" ;
1213import { installDependencies } from "@opennextjs/aws/build/installDeps.js" ;
1314import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js" ;
@@ -138,13 +139,11 @@ async function generateBundle(
138139 // `.next/standalone/package/path` (ie. `.next`, `server.js`).
139140 // We need to output the handler file inside the package path.
140141 const packagePath = buildHelper . getPackagePath ( options ) ;
141- fs . mkdirSync ( path . join ( outputPath , packagePath ) , { recursive : true } ) ;
142+ const outPackagePath = path . join ( outputPath , packagePath ) ;
143+ fs . mkdirSync ( outPackagePath , { recursive : true } ) ;
142144
143145 const ext = fnOptions . runtime === "deno" ? "mjs" : "cjs" ;
144- fs . copyFileSync (
145- path . join ( options . buildDir , `cache.${ ext } ` ) ,
146- path . join ( outputPath , packagePath , "cache.cjs" )
147- ) ;
146+ fs . copyFileSync ( path . join ( options . buildDir , `cache.${ ext } ` ) , path . join ( outPackagePath , "cache.cjs" ) ) ;
148147
149148 if ( fnOptions . runtime === "deno" ) {
150149 addDenoJson ( outputPath , packagePath ) ;
@@ -153,7 +152,7 @@ async function generateBundle(
153152 // Bundle next server if necessary
154153 const isBundled = fnOptions . experimentalBundledNextServer ?? false ;
155154 if ( isBundled ) {
156- await bundleNextServer ( path . join ( outputPath , packagePath ) , appPath , {
155+ await bundleNextServer ( outPackagePath , appPath , {
157156 minify : options . minify ,
158157 } ) ;
159158 }
@@ -162,12 +161,16 @@ async function generateBundle(
162161 if ( ! config . middleware ?. external ) {
163162 fs . copyFileSync (
164163 path . join ( options . buildDir , "middleware.mjs" ) ,
165- path . join ( outputPath , packagePath , "middleware.mjs" )
164+ path . join ( outPackagePath , "middleware.mjs" )
166165 ) ;
166+
167+ const middlewareManifest = loadMiddlewareManifest ( path . join ( options . appBuildOutputPath , ".next" ) ) ;
168+
169+ copyMiddlewareResources ( options , middlewareManifest . middleware [ "/" ] , outPackagePath ) ;
167170 }
168171
169172 // Copy open-next.config.mjs
170- buildHelper . copyOpenNextConfig ( options . buildDir , path . join ( outputPath , packagePath ) , true ) ;
173+ buildHelper . copyOpenNextConfig ( options . buildDir , outPackagePath , true ) ;
171174
172175 // Copy env files
173176 buildHelper . copyEnvFile ( appBuildOutputPath , packagePath , outputPath ) ;
@@ -241,7 +244,7 @@ async function generateBundle(
241244
242245 openNextEdgePlugins ( {
243246 nextDir : path . join ( options . appBuildOutputPath , ".next" ) ,
244- isInCloudfare : true ,
247+ isInCloudflare : true ,
245248 } ) ,
246249 ] ;
247250
@@ -322,7 +325,7 @@ function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
322325
323326 fs . writeFileSync (
324327 path . join ( outputPath , "index.mjs" ) ,
325- `export * from "./${ normalizePath ( packagePath ) } /index.mjs";`
328+ `export { handler } from "./${ normalizePath ( packagePath ) } /index.mjs";`
326329 ) ;
327330}
328331
0 commit comments