-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for reading .env files as runtime vars in local development
#9914
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
42 commits
Select commit
Hold shift + click to select a range
72946da
Implement .env support in local development
petebacondarwin b659ce8
add support to Wrangler for setting the path to the .env files
petebacondarwin e4a1dba
Add support for loading local vars from .env files to the vite-plugin
petebacondarwin 6bb75c5
add various e2e and fixture tests for dotenv support
petebacondarwin 9148765
add a changeset
petebacondarwin ffc0922
fixup! add support to Wrangler for setting the path to the .env files
petebacondarwin 35c8831
use `toMatchObject()` in tests
petebacondarwin 76eee90
add support for CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV to opt-out of .…
petebacondarwin 18da8be
tidy up `envFiles` documentation
petebacondarwin 85c487e
fixup! use `toMatchObject()` in tests
petebacondarwin 186d19c
fixup! Add support for loading local vars from .env files to the vite…
petebacondarwin 6cea665
review fixups
petebacondarwin b363993
fix up snapshots after rebase
petebacondarwin 281b6c4
more fixups
petebacondarwin 863a09e
more fixups
petebacondarwin 6bd0490
simplify boolean env var defaults
petebacondarwin 75ba0d2
review fixups
petebacondarwin cc130a6
skip buildAndPreview test on Windows
petebacondarwin 9a84592
move config update handling to a devServer watcher rather than hotUpdate
petebacondarwin a347c5e
don't run playgrounds in parallel
petebacondarwin 4523114
force runs of playground tests
petebacondarwin 1beee4c
log configid
petebacondarwin b0b9655
remove watchers
petebacondarwin 35cb6bf
wait for miniflare to be ready before setting options
petebacondarwin bf5581e
dispose vite server between playground tests
petebacondarwin 2a3be8c
More logs on mockfile changes
petebacondarwin bc34d6a
handle internal body reading errors in miniflare
petebacondarwin ad20352
test updates
petebacondarwin f52d4b1
don't run `page.goto()` in the setup file
petebacondarwin 3b62d4b
restart fixes
petebacondarwin 0777d99
dispose of Miniflare instance when Vite is disposed
petebacondarwin 6fd4861
log stack
petebacondarwin fe5d375
more diagnostics
petebacondarwin 761bdfa
fix lockfile
petebacondarwin e692a46
separate forks
petebacondarwin c51ba25
final cleanups
petebacondarwin c311775
Revert "force runs of playground tests"
petebacondarwin 2a555d4
longer timeout on the container fixture test
petebacondarwin 0f93e5e
correctly unwatch config files
petebacondarwin faba736
revert interactive dev fix
petebacondarwin 5a4592b
cleanup beforeAll in vitest-setup
petebacondarwin 7ec7a0c
remove redundant change to `restartingServer` flag
petebacondarwin 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,20 @@ | ||
| --- | ||
| "@cloudflare/vite-plugin": minor | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| Add support for loading local dev vars from .env files | ||
|
|
||
| If there are no `.dev.vars` or `.dev.vars.<environment>` files, when running Wrangler or the Vite plugin in local development mode, | ||
| they will now try to load additional local dev vars from `.env`, `.env.local`, `.env.<environment>` and `.env.<environment>.local` files. | ||
|
|
||
| These loaded vars are only for local development and have no effect in production to the vars in a deployed Worker. | ||
| Wrangler and Vite will continue to load `.env` files in order to configure themselves as a tool. | ||
|
|
||
| Further details: | ||
|
|
||
| - In `vite build` the local vars will be computed and stored in a `.dev.vars` file next to the compiled Worker code, so that `vite preview` can use them. | ||
| - The `wrangler types` command will similarly read the `.env` files (if no `.dev.vars` files) in order to generate the `Env` interface. | ||
| - If the `CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV` environment variable is `"false"` then local dev variables will not be loaded from `.env` files. | ||
| - If the `CLOUDFLARE_INCLUDE_PROCESS_ENV` environment variable is `"true"` then all the environment variables found on `process.env` will be included as local dev vars. | ||
| - Wrangler (but not Vite plugin) also now supports the `--env-file=<path/to/dotenv/file>` global CLI option. This affects both loading `.env` to configure Wrangler the tool as well as loading local dev vars. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DEV_VAR_FROM_DOT_ENV="dev-var-from-dot-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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| !.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| FOO=bar |
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 @@ | ||
| !.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| import text from "./other-script"; | ||
|
|
||
| export default { | ||
| async fetch() { | ||
| return new Response(text); | ||
| async fetch(request, env) { | ||
| if (request.url.endsWith("/env")) { | ||
| return Response.json(env); | ||
| } else { | ||
| return new Response(text); | ||
| } | ||
| }, | ||
| }; |
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 @@ | ||
| FOO=bar |
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 +1,2 @@ | ||
| dist | ||
| !.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
File renamed without changes.
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
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.