-
Notifications
You must be signed in to change notification settings - Fork 73
inline project env files in worker #181
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
181910c
extract env vars from file system
james-elicx fcd1156
combine variables from a global with the request-scoped env
james-elicx c8ac7d8
inline build-time env vars in the worker script
james-elicx 3ffdc83
for some reason the tests failed in the pipeline but not locally
james-elicx df61c41
switch between modes at runtime and apply on process.env
james-elicx d3163b3
add test for referencing variables
james-elicx ae2b8ee
use a .env.mjs file for the vars
james-elicx 9ff653c
Update packages/cloudflare/src/cli/build/patches/investigated/copy-pa…
james-elicx e740d18
move the merging to extractProjectEnvVars
james-elicx ea4bb69
rename secrets to nextEnvVars
james-elicx c43a925
add missing mode when retrieving value
james-elicx 64e51a5
add link to nextjs var load order
james-elicx 8e5b382
rename to compile
james-elicx bf62788
change function to read a single file
james-elicx a107abb
move the readEnvFile call inside the flatMap
james-elicx a9f7ec4
remove process.env.node_env usage
james-elicx a5268a7
add e2e test for env vars
james-elicx dbb57bf
move locations
james-elicx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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` | ||
* | ||
james-elicx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.