-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: allow $env to be used outside Vite context #15250
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
Open
teemingc
wants to merge
19
commits into
main
Choose a base branch
from
feat-generated-env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c8a0bfa
it works?
teemingc aa668d0
fix unit test
teemingc 929dc49
adjust comment
teemingc e8e98a5
we still need to load them because we exclude $env during build
teemingc 71e9325
forgot this
teemingc d74b806
come onnnn
teemingc 0f17dc8
fix tsconfig test
teemingc fff122f
standardise module ids
teemingc a4c855c
don't serialise dynamic env values
teemingc 01ff47c
Merge branch 'main' into feat-generated-env
teemingc 9273c07
virtual modules still useful!
teemingc 4773cf8
revert
teemingc 3513d44
changeset
teemingc 64b3cfd
forgot to save
teemingc efa1e8c
separate get_env to avoid type errors
teemingc be79128
oops
teemingc 25a3b37
format
teemingc e217ad4
avoid internal types
teemingc 9cc1ebe
load dynamic env once
teemingc 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,5 @@ | ||
| --- | ||
| '@sveltejs/kit': minor | ||
| --- | ||
|
|
||
| feat: `$env/*` modules can now be imported from Playwright and other code running without Vite |
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
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,65 @@ | ||
| import path from 'node:path'; | ||
| import { dedent, write_if_changed } from './utils.js'; | ||
| import { create_static_module } from '../env.js'; | ||
| import { runtime_directory } from '../utils.js'; | ||
| import { s } from '../../utils/misc.js'; | ||
|
|
||
| /** | ||
| * This version deviates from the one in env.js because we don't want to | ||
| * serialise the user's dynamic environment variables. Instead, it loads the | ||
| * environment variables directly. This is okay because it will only be used by | ||
| * modules importing $env/dynamic/* from outside the Vite pipeline. Those inside | ||
| * the Vite pipeline will load the virtual module which reuses the already loaded | ||
| * environment variables. | ||
| * @param {import('../env.js').EnvType} type | ||
| * @returns {string} | ||
| */ | ||
| function create_dynamic_module(type) { | ||
| return dedent` | ||
| import { env as full_env } from './internal.js'; | ||
|
|
||
| export const env = full_env.${type}; | ||
| `; | ||
| } | ||
|
|
||
| /** | ||
| * Writes env variable modules to the output directory | ||
| * @param {import('types').ValidatedKitConfig} config | ||
| * @param {string} mode | ||
| * @param {import('../../exports/vite/types.js').Env} env | ||
| */ | ||
| export function write_env(config, mode, env) { | ||
| const env_static_private = create_static_module('$env/static/private', env.private); | ||
| write_if_changed( | ||
| path.join(config.outDir, 'generated', 'env', 'static', 'private.js'), | ||
| env_static_private | ||
| ); | ||
|
|
||
| const env_static_public = create_static_module('$env/static/public', env.public); | ||
| write_if_changed( | ||
| path.join(config.outDir, 'generated', 'env', 'static', 'public.js'), | ||
| env_static_public | ||
| ); | ||
|
|
||
| const env_dynamic = dedent` | ||
| import { get_env } from '${runtime_directory}/../exports/vite/env.js'; | ||
|
|
||
| export const env = get_env(${s(config.env)}, ${s(mode)}); | ||
| `; | ||
| write_if_changed( | ||
| path.join(config.outDir, 'generated', 'env', 'dynamic', 'internal.js'), | ||
| env_dynamic | ||
| ); | ||
|
|
||
| const env_dynamic_private = create_dynamic_module('private'); | ||
| write_if_changed( | ||
| path.join(config.outDir, 'generated', 'env', 'dynamic', 'private.js'), | ||
| env_dynamic_private | ||
| ); | ||
|
|
||
| const env_dynamic_public = create_dynamic_module('public'); | ||
| write_if_changed( | ||
| path.join(config.outDir, 'generated', 'env', 'dynamic', 'public.js'), | ||
| env_dynamic_public | ||
| ); | ||
| } |
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
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,18 @@ | ||
| import { loadEnv } from 'vite'; | ||
| import { filter_env } from '../../utils/env.js'; | ||
|
|
||
| /** | ||
| * Load environment variables from process.env and .env files | ||
| * @param {{ dir: string; publicPrefix: string; privatePrefix: string }} env_config | ||
| * @param {string} mode | ||
| * @returns {import('./types.js').Env} | ||
| */ | ||
| export function get_env(env_config, mode) { | ||
| const { publicPrefix: public_prefix, privatePrefix: private_prefix } = env_config; | ||
| const env = loadEnv(mode, env_config.dir, ''); | ||
|
|
||
| return { | ||
| public: filter_env(env, public_prefix, private_prefix), | ||
| private: filter_env(env, private_prefix, public_prefix) | ||
| }; | ||
| } |
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,3 +1,8 @@ | ||
| export interface EnforcedConfig { | ||
| [key: string]: EnforcedConfig | true; | ||
| } | ||
|
|
||
| export interface Env { | ||
| public: Record<string, string>; | ||
| private: Record<string, string>; | ||
| } |
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 |
|---|---|---|
|
|
@@ -13,3 +13,25 @@ test.describe('Static files', () => { | |
| expect(response.status()).toBe(404); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe('$env access outside the Vite pipeline', () => { | ||
| test('$env/static/public', async () => { | ||
| const env = await import('$env/static/public'); | ||
| expect(env.PUBLIC_DYNAMIC).toBe('accessible anywhere/evaluated at run time'); | ||
| }); | ||
|
|
||
| test('$env/static/private', async () => { | ||
| const env = await import('$env/static/private'); | ||
| expect(env.PRIVATE_STATIC).toBe('accessible to server-side code/replaced at build time'); | ||
| }); | ||
|
|
||
| test('$env/dynamic/public', async () => { | ||
| const { env } = await import('$env/dynamic/public'); | ||
| expect(env.PUBLIC_DYNAMIC).toBe('accessible anywhere/evaluated at run time'); | ||
| }); | ||
|
|
||
| test('$env/dynamic/private', async () => { | ||
| const { env } = await import('$env/dynamic/private'); | ||
| expect(env.PRIVATE_DYNAMIC).toBe('accessible to server-side code/evaluated at run time'); | ||
| }); | ||
| }); | ||
|
Comment on lines
+17
to
+37
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also change these to Playwright component tests, which is what the majority of users are facing issues with. |
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be moved to a separate module to avoid type errors when users import
$env/dynamic/*and they type check their code. We're currently using it in the generated module to avoid serialising users' dynamic env vars