Skip to content

Commit a0ca689

Browse files
committed
use a .env.mjs file for the vars
1 parent 640d660 commit a0ca689

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

packages/cloudflare/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ declare global {
77
NEXT_PRIVATE_DEBUG_CACHE?: string;
88
__OPENNEXT_KV_BINDING_NAME: string;
99
OPEN_NEXT_ORIGIN: string;
10+
NODE_ENV?: string;
11+
__OPENNEXT_PROCESSED_ENV?: string;
1012
}
1113
}
1214
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
77
import { build, Plugin } from "esbuild";
88

99
import { Config } from "../config";
10+
import { copyEnvFiles } from "./patches/investigated/copy-env-files";
1011
import { copyPackageCliFiles } from "./patches/investigated/copy-package-cli-files";
1112
import { patchCache } from "./patches/investigated/patch-cache";
1213
import { patchRequire } from "./patches/investigated/patch-require";
@@ -31,7 +32,8 @@ export async function bundleServer(config: Config, openNextOptions: BuildOptions
3132
// Copy over prerendered assets (e.g. SSG routes)
3233
copyPrerenderedRoutes(config);
3334

34-
await copyPackageCliFiles(packageDistDir, config, openNextOptions);
35+
copyPackageCliFiles(packageDistDir, config, openNextOptions);
36+
copyEnvFiles(openNextOptions);
3537

3638
const nextConfigStr =
3739
fs
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
4+
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
5+
6+
import { extractProjectEnvVars } from "../../utils";
7+
8+
/**
9+
* Copies the values extracted from the project's env files to the output directory for use in the worker.
10+
*/
11+
export function copyEnvFiles(options: BuildOptions) {
12+
["production", "development", "test"].forEach((mode) =>
13+
fs.appendFileSync(
14+
path.join(options.outputDir, `.env.mjs`),
15+
`export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, options))};\n`
16+
)
17+
);
18+
}

packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,21 @@ import fs from "node:fs";
22
import path from "node:path";
33

44
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
5-
import { build } from "esbuild";
65

76
import { Config } from "../../../config";
8-
import { extractProjectEnvVars } from "../../utils";
97

108
/**
11-
* Copies the template files present in the cloudflare adapter package into the
12-
* standalone node_modules folder and applies necessary transformations.
9+
* Copies the template files present in the cloudflare adapter package into the standalone node_modules folder.
1310
*/
14-
export async function copyPackageCliFiles(
15-
packageDistDir: string,
16-
config: Config,
17-
openNextConfig: BuildOptions
18-
) {
11+
export function copyPackageCliFiles(packageDistDir: string, config: Config, openNextConfig: BuildOptions) {
1912
console.log("# copyPackageTemplateFiles");
2013
const sourceDir = path.join(packageDistDir, "cli");
2114
const destinationDir = path.join(config.paths.internal.package, "cli");
2215

2316
fs.cpSync(sourceDir, destinationDir, { recursive: true });
2417

25-
const devEnvVars = extractProjectEnvVars("development", openNextConfig);
26-
const prodEnvVars = extractProjectEnvVars("production", openNextConfig);
27-
28-
await build({
29-
entryPoints: [path.join(packageDistDir, "cli", "templates", "worker.ts")],
30-
outfile: path.join(openNextConfig.outputDir, "worker.js"),
31-
format: "esm",
32-
target: "esnext",
33-
bundle: false,
34-
minify: false,
35-
define: {
36-
__OPENNEXT_BUILD_TIME_DEV_ENV: JSON.stringify(devEnvVars),
37-
__OPENNEXT_BUILD_TIME_PROD_ENV: JSON.stringify(prodEnvVars),
38-
},
39-
});
18+
fs.copyFileSync(
19+
path.join(packageDistDir, "cli", "templates", "worker.ts"),
20+
path.join(openNextConfig.outputDir, "worker.ts")
21+
);
4022
}

packages/cloudflare/src/cli/build/utils/extract-project-env-vars.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ function readEnvFiles(fileNames: string[], { monorepoRoot, appPath }: BuildOptio
2929
* In a monorepo, the env files in an app's directory will take precedence over
3030
* the env files at the root of the monorepo.
3131
*/
32-
export function extractProjectEnvVars(mode: "development" | "production", options: BuildOptions) {
32+
export function extractProjectEnvVars(mode: string, options: BuildOptions) {
3333
return readEnvFiles([".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`], options);
3434
}

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
2121
}
2222
);
2323

24-
declare const __OPENNEXT_BUILD_TIME_DEV_ENV: Record<string, string>;
25-
declare const __OPENNEXT_BUILD_TIME_PROD_ENV: Record<string, string>;
24+
async function applyProjectEnvVars(mode = "production") {
25+
if (process.env.__OPENNEXT_PROCESSED_ENV === "1") return;
2626

27-
function applyBuildTimeEnv(mode?: string) {
28-
const secrets = mode === "development" ? __OPENNEXT_BUILD_TIME_DEV_ENV : __OPENNEXT_BUILD_TIME_PROD_ENV;
27+
// @ts-expect-error: resolved by wrangler build
28+
const secrets = await import("./.env.mjs");
2929

30-
for (const key in secrets) {
31-
process.env[key] = secrets[key];
30+
if (secrets[mode]) {
31+
for (const key in secrets[mode]) {
32+
process.env[key] = secrets[key];
33+
}
3234
}
35+
36+
process.env.__OPENNEXT_PROCESSED_ENV = "1";
3337
}
3438

3539
export default {
@@ -45,7 +49,7 @@ export default {
4549
},
4650
});
4751

48-
applyBuildTimeEnv(env["NEXTJS_ENV"]);
52+
await applyProjectEnvVars(env.NEXTJS_ENV ?? globalThis.process.env.NODE_ENV);
4953

5054
// The Middleware handler can return either a `Response` or a `Request`:
5155
// - `Response`s should be returned early

0 commit comments

Comments
 (0)