Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove process.env.NODE_ENV warning #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-beans-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

remove `process.env.NODE_ENV` warning
27 changes: 25 additions & 2 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export async function bundleServer(config: Config, openNextOptions: BuildOptions
const { appBuildOutputPath, appPath, outputDir, monorepoRoot } = openNextOptions;
const outputPath = path.join(outputDir, "server-functions", "default");
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
const openNextServer = path.join(outputPath, packagePath, `index.mjs`);
const openNextServerBundle = path.join(outputPath, packagePath, `handler.mjs`);
const openNextServer = path.join(outputPath, packagePath, "index.mjs");
patchOpenNextServer(openNextServer);
const openNextServerBundle = path.join(outputPath, packagePath, "handler.mjs");

await build({
entryPoints: [openNextServer],
Expand Down Expand Up @@ -230,3 +231,25 @@ async function patchCodeWithValidations(
export function getOutputWorkerPath(openNextOptions: BuildOptions): string {
return path.join(openNextOptions.outputDir, "worker.js");
}

/**
* Patches the open-next server file to adapt it to our usage
*
* (Note: ideally in the future we should update the open-next server not to
* be more flexible and not require any such patching)
*
* @param openNextServerPath the path to the open-next server file
*/
function patchOpenNextServer(openNextServerPath: string): void {
// this patch is not necessary, it's simply here to remove a warning that `wrangler` would
// otherwise generate (since `process.env.NODE_ENV` is defined by esbuild but the open-next
// server tries to assign to it at runtime
const patchedOpenNextServer = fs
.readFileSync(openNextServerPath, "utf-8")
.replace(
/^(\s*)process\.env\.NODE_ENV\s*=\s*process\.env\.NODE_ENV\s*\?\?/gm,
"$1const processEnv = process.env; processEnv.NODE_ENV = process.env.NODE_ENV ??"
);

fs.writeFileSync(openNextServerPath, patchedOpenNextServer);
}
Loading