-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inline project env files in worker (#181)
* extract env vars from file system * combine variables from a global with the request-scoped env * inline build-time env vars in the worker script * for some reason the tests failed in the pipeline but not locally * switch between modes at runtime and apply on process.env * add test for referencing variables * use a .env.mjs file for the vars * Update packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts * move the merging to extractProjectEnvVars * rename secrets to nextEnvVars * add missing mode when retrieving value * add link to nextjs var load order * rename to compile * change function to read a single file * move the readEnvFile call inside the flatMap * remove process.env.node_env usage * add e2e test for env vars * move locations
- Loading branch information
1 parent
baeb14d
commit 4341c70
Showing
14 changed files
with
294 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXTJS_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TEST_ENV_VAR=TEST_VALUE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export async function GET() { | ||
return new Response(JSON.stringify(process.env)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/cloudflare/src/cli/build/open-next/compile-env-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
|
||
import { extractProjectEnvVars } from "../utils"; | ||
|
||
/** | ||
* Compiles the values extracted from the project's env files to the output directory for use in the worker. | ||
*/ | ||
export function compileEnvFiles(options: BuildOptions) { | ||
["production", "development", "test"].forEach((mode) => | ||
fs.appendFileSync( | ||
path.join(options.outputDir, `.env.mjs`), | ||
`export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, options))};\n` | ||
) | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/cloudflare/src/cli/build/utils/extract-project-env-vars.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { appendFileSync, writeFileSync } from "node:fs"; | ||
|
||
import { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
import mockFs from "mock-fs"; | ||
import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
|
||
import { extractProjectEnvVars } from "./extract-project-env-vars"; | ||
|
||
const options = { monorepoRoot: "", appPath: "" } as BuildOptions; | ||
|
||
describe("extractProjectEnvVars", () => { | ||
beforeEach(() => { | ||
mockFs({ | ||
".env": "ENV_VAR=value", | ||
".env.local": "ENV_LOCAL_VAR=value", | ||
".env.development": "ENV_DEV_VAR=value", | ||
".env.development.local": "ENV_DEV_LOCAL_VAR=value", | ||
".env.production": "ENV_PROD_VAR=value", | ||
".env.production.local": "ENV_PROD_LOCAL_VAR=value", | ||
}); | ||
}); | ||
|
||
afterEach(() => mockFs.restore()); | ||
|
||
it("should extract production env vars", () => { | ||
const result = extractProjectEnvVars("production", options); | ||
expect(result).toEqual({ | ||
ENV_LOCAL_VAR: "value", | ||
ENV_PROD_LOCAL_VAR: "value", | ||
ENV_PROD_VAR: "value", | ||
ENV_VAR: "value", | ||
}); | ||
}); | ||
|
||
it("should extract development env vars", () => { | ||
writeFileSync(".dev.vars", 'NEXTJS_ENV = "development"'); | ||
|
||
const result = extractProjectEnvVars("development", options); | ||
expect(result).toEqual({ | ||
ENV_LOCAL_VAR: "value", | ||
ENV_DEV_LOCAL_VAR: "value", | ||
ENV_DEV_VAR: "value", | ||
ENV_VAR: "value", | ||
}); | ||
}); | ||
|
||
it("should override env vars with those in a local file", () => { | ||
writeFileSync(".env.production.local", "ENV_PROD_VAR=overridden"); | ||
|
||
const result = extractProjectEnvVars("production", options); | ||
expect(result).toEqual({ | ||
ENV_LOCAL_VAR: "value", | ||
ENV_PROD_VAR: "overridden", | ||
ENV_VAR: "value", | ||
}); | ||
}); | ||
|
||
it("should support referencing variables", () => { | ||
appendFileSync(".env.production.local", "\nENV_PROD_LOCAL_VAR_REF=$ENV_PROD_LOCAL_VAR"); | ||
|
||
const result = extractProjectEnvVars("production", options); | ||
expect(result).toEqual({ | ||
ENV_LOCAL_VAR: "value", | ||
ENV_PROD_LOCAL_VAR: "value", | ||
ENV_PROD_LOCAL_VAR_REF: "value", | ||
ENV_PROD_VAR: "value", | ||
ENV_VAR: "value", | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/cloudflare/src/cli/build/utils/extract-project-env-vars.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
|
||
import { parse } from "@dotenvx/dotenvx"; | ||
import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
|
||
function readEnvFile(filePath: string) { | ||
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { | ||
return parse(fs.readFileSync(filePath).toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Extracts the environment variables defined in various .env files for a project. | ||
* | ||
* The `NEXTJS_ENV` environment variable in `.dev.vars` determines the mode. | ||
* | ||
* Merged variables respect the following priority order. | ||
* 1. `.env.{mode}.local` | ||
* 2. `.env.local` | ||
* 3. `.env.{mode}` | ||
* 4. `.env` | ||
* | ||
* https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#environment-variable-load-order | ||
* | ||
* In a monorepo, the env files in an app's directory will take precedence over | ||
* the env files at the root of the monorepo. | ||
*/ | ||
export function extractProjectEnvVars(mode: string, { monorepoRoot, appPath }: BuildOptions) { | ||
return [".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`] | ||
.flatMap((fileName) => [ | ||
...(monorepoRoot !== appPath ? [readEnvFile(path.join(monorepoRoot, fileName))] : []), | ||
readEnvFile(path.join(appPath, fileName)), | ||
]) | ||
.reduce<Record<string, string>>((acc, overrides) => ({ ...acc, ...overrides }), {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./copy-prerendered-routes"; | ||
export * from "./extract-project-env-vars"; | ||
export * from "./normalize-path"; | ||
export * from "./ts-parse-file"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.