@@ -31,8 +31,9 @@ export async function bundleServer(config: Config, openNextOptions: BuildOptions
31
31
const { appBuildOutputPath, appPath, outputDir, monorepoRoot } = openNextOptions ;
32
32
const outputPath = path . join ( outputDir , "server-functions" , "default" ) ;
33
33
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" ) ;
36
37
37
38
await build ( {
38
39
entryPoints : [ openNextServer ] ,
@@ -230,3 +231,25 @@ async function patchCodeWithValidations(
230
231
export function getOutputWorkerPath ( openNextOptions : BuildOptions ) : string {
231
232
return path . join ( openNextOptions . outputDir , "worker.js" ) ;
232
233
}
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's 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