Skip to content

Commit b5b7994

Browse files
remove process.env.NODE_ENV warning
1 parent 12a1f75 commit b5b7994

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.changeset/kind-beans-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
remove `process.env.NODE_ENV` warning

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
230231
export 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'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*)process\.env\.NODE_ENV\s*=\s*process\.env\.NODE_ENV\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

Comments
 (0)