@@ -31,8 +31,9 @@ export async function bundleServer(config: Config, openNextOptions: BuildOptions
3131 const { appBuildOutputPath, appPath, outputDir, monorepoRoot } = openNextOptions ;
3232 const outputPath = path . join ( outputDir , "server-functions" , "default" ) ;
3333 const packagePath = path . relative ( monorepoRoot , appBuildOutputPath ) ;
34- const openNextServer = path . join ( outputPath , packagePath , `index.mjs` ) ;
35- const openNextServerBundle = path . join ( outputPath , packagePath , `handler.mjs` ) ;
34+ const openNextServer = path . join ( outputPath , packagePath , "index.mjs" ) ;
35+ patchOpenNextServer ( openNextServer ) ;
36+ const openNextServerBundle = path . join ( outputPath , packagePath , "handler.mjs" ) ;
3637
3738 await build ( {
3839 entryPoints : [ openNextServer ] ,
@@ -230,3 +231,25 @@ async function patchCodeWithValidations(
230231export function getOutputWorkerPath ( openNextOptions : BuildOptions ) : string {
231232 return path . join ( openNextOptions . outputDir , "worker.js" ) ;
232233}
234+
235+ /**
236+ * Patches the open-next server file to adapt it to our usage
237+ *
238+ * (Note: ideally in the future we should update the open-next server not to
239+ * be more flexible and not require any such patching)
240+ *
241+ * @param openNextServerPath the path to the open-next server file
242+ */
243+ function patchOpenNextServer ( openNextServerPath : string ) : void {
244+ // this patch is not necessary, it simply here to remove a warning that `wrangler` would
245+ // otherwise generate (since `process.env.NODE_ENV` is defined by esbuild but the open-next
246+ // server tries to assign to it at runtime
247+ const patchedOpenNextServer = fs
248+ . readFileSync ( openNextServerPath , "utf-8" )
249+ . replace (
250+ / ^ ( \s * ) p r o c e s s \. e n v \. N O D E _ E N V \s * = \s * p r o c e s s \. e n v \. N O D E _ E N V \s * \? \? / gm,
251+ "$1const processEnv = process.env; processEnv.NODE_ENV = process.env.NODE_ENV ??"
252+ ) ;
253+
254+ fs . writeFileSync ( openNextServerPath , patchedOpenNextServer ) ;
255+ }
0 commit comments