Skip to content

Conversation

james-elicx
Copy link
Collaborator

@james-elicx james-elicx commented Dec 15, 2024

Changes

  • Extracts env vars from .env files.
  • Chucks them into a .env.mjs that is dynamically imported in the worker.

Copy link

changeset-bot bot commented Dec 15, 2024

⚠️ No Changeset found

Latest commit: a5268a7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

pkg-pr-new bot commented Dec 15, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/@opennextjs/cloudflare@181

commit: dbb57bf

@james-elicx james-elicx marked this pull request as ready for review December 15, 2024 23:22
@james-elicx james-elicx linked an issue Dec 15, 2024 that may be closed by this pull request
@vicb
Copy link
Contributor

vicb commented Dec 16, 2024

A few thoughts (I'll do a deeper review later today)

I find "build time" to be confusing. Looking at the next docs there are 2 things: Non-NEXT_PUBLIC_ and NEXT_PUBLIC_ vars.

Does the current code support referencing other vars - maybe add a test.

It looks like we should support development, production and test. It looks like get the env from NODE_ENV so maybe applyBuildTimeEnv(env["NEXTJS_ENV"]) should actually be applyBuildTimeEnv(env["NEXTJS_ENV"] ?? process.env.NODE_ENV)

To be more consistent with how Open Next work, we might want to generate .env.development.mjs, ... in the build folder of open next and include the file from worker.ts (rather than ESBulding the file). That would be similar to compiling the config

@james-elicx
Copy link
Collaborator Author

I find "build time" to be confusing. Looking at the next docs there are 2 things: Non-NEXT_PUBLIC_ and NEXT_PUBLIC_ vars.

Removed the reference to 'build-time'.

Does the current code support referencing other vars - maybe add a test.

Yeah, that was actually why i used the newer dotenvx instead of dotenv :). Added a test.

It looks like we should support development, production and test. It looks like get the env from NODE_ENV so maybe applyBuildTimeEnv(env["NEXTJS_ENV"]) should actually be applyBuildTimeEnv(env["NEXTJS_ENV"] ?? process.env.NODE_ENV)

To be more consistent with how Open Next work, we might want to generate .env.development.mjs, ... in the build folder of open next and include the file from worker.ts (rather than ESBulding the file). That would be similar to compiling the config

Sorry I hadn't realised it was an intentional part of the design. I've undone that change. Instead, it's not putting all of them into an .env.mjs file, exporting each mode. I originally did .env.${mode}.mjs, but this was causing runtime issues with variable-based dynamic imports due to wrangler's bundle step, so i couldn't do that unfortunately.

@james-elicx james-elicx changed the title inline build-time env files in worker inline project env files in worker Dec 17, 2024
* 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, options: BuildOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't that mean that we will always use the .local files while we only want to use them for wrangler dev but not in prod?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. How come you wouldn't want them in production? Next.js would use those files. I suppose you're talking about if someone builds it locally and then deploys, but in that case, you could make the argument that we shouldnt include any .env files at all because technically they're all local to the machine. I'm not quite sure what else you could do here - open to suggestions though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@james-elicx I did a quick test and you are right, .local files are used if present, regardless if it is a prod or dev build. I was wrong to think that they would only be used in dev.

@vicb
Copy link
Contributor

vicb commented Dec 18, 2024

Do you think you can add a simple test, maybe to the api example app?

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome PR, thanks!

I would move the code out of patch*, I think it should be part of the build.

Approving the PR, you can pick whatever your prefer.

🙏

@james-elicx james-elicx merged commit 4341c70 into experimental Dec 18, 2024
6 checks passed
@james-elicx james-elicx deleted the james/inline-env branch December 18, 2024 19:42
vicb pushed a commit that referenced this pull request Dec 20, 2024
* 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
vicb pushed a commit that referenced this pull request Dec 20, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Next .env file

2 participants