-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: experimental static import.meta.env #12105
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 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d11b42e
feat: experimental static import.meta.env
florian-lefebvre ede8f22
Merge branch 'main' into feat/experimental-static-meta-env
florian-lefebvre 6c329c4
clarify
florian-lefebvre 422ea9d
fix: remove translations
florian-lefebvre 13a7144
Update src/content/docs/en/reference/experimental-flags/static-import…
florian-lefebvre 9320bad
Yan punctuation review
sarah11918 dbdb84f
Sarah draft content suggestions after talking with Florian
sarah11918 e987046
Astro 6.0 tip
sarah11918 c48f056
Yan final polish
sarah11918 20bb3f7
Merge branch 'main' into feat/experimental-static-meta-env
sarah11918 4ec4e22
semi-colons in code blocks
sarah11918 f54c754
Update src/content/docs/en/reference/experimental-flags/static-import…
florian-lefebvre efd99db
Merge branch '5.13.0' into feat/experimental-static-meta-env
sarah11918 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
50 changes: 0 additions & 50 deletions
50
src/content/docs/en/reference/experimental-flags/raw-env-values.mdx
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
src/content/docs/en/reference/experimental-flags/static-import-meta-env.mdx
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,78 @@ | ||
--- | ||
title: Experimental static import.meta.env | ||
sidebar: | ||
label: Static import.meta.env | ||
i18nReady: true | ||
--- | ||
|
||
import Since from '~/components/Since.astro' | ||
|
||
<p> | ||
|
||
**Type:** `boolean`<br /> | ||
**Default:** `false`<br /> | ||
<Since v="5.13.0" /> | ||
</p> | ||
|
||
Astro allows you to configure a [type-safe schema for your environment variables](/en/guides/environment-variables/#type-safe-environment-variables), and converts variables imported via `astro:env` into the expected type. | ||
sarah11918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
However, Astro by default turns non public `import.meta.env` values into `process.env` calls during the build, if the environment variable name is present in `process.env`. It also converts your environment variables used through `import.meta.env` in some cases, and this can prevent access to some values such as the strings `"true"` (which is converted to a boolean value), and `"1"` (which is converted to a number). | ||
sarah11918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `experimental.staticImportMetaEnv` flag disables this behavior, ensuring that `import.meta.env` values are always inlined. | ||
|
||
To enable this feature, add the experimental flag in your Astro config: | ||
sarah11918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```js title="astro.config.mjs" ins={4-6} | ||
import { defineConfig } from "astro/config" | ||
|
||
export default defineConfig({ | ||
experimental: { | ||
staticImportMetaEnv: true, | ||
} | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
Enabling this experimental flag will no longer convert string values into booleans or numbers, nor turn `import.meta.env` values into `process.env` calls. This aligns `import.meta.env`'s behavior in Astro with [Vite](https://vite.dev/guide/env-and-mode.html#env-variables). | ||
|
||
In a future major version, Astro will switch to this behavior by default, but you can opt in to the future behavior early using the `experimental.staticImportMetaEnv` flag and if necessary, [updating your project](#updating-your-project) accordingly. | ||
sarah11918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Updating your project | ||
|
||
If you were relying on coercion, you may need to update your project code to apply it manually: | ||
|
||
```ts title="src/components/MyComponent.astro" del={1} ins={2} | ||
const enabled: boolean = import.meta.env.ENABLED | ||
const enabled: boolean = import.meta.env.ENABLED === "true" | ||
``` | ||
|
||
If you were relying on the transformation into `process.env`, you may need to update your project code to apply it manually: | ||
|
||
```ts title="src/components/MyComponent.astro" del={1} ins={2} | ||
const enabled: boolean = import.meta.env.DB_PASSWORD | ||
const enabled: boolean = process.env.DB_PASSWORD | ||
``` | ||
|
||
You may need to update types as well: | ||
sarah11918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```ts title="src/env.d.ts" del={3-4} ins={5,12-16} | ||
interface ImportMetaEnv { | ||
readonly PUBLIC_POKEAPI: string; | ||
readonly DB_PASSWORD: string; | ||
readonly ENABLED: boolean; | ||
readonly ENABLED: string; | ||
} | ||
|
||
interface ImportMeta { | ||
readonly env: ImportMetaEnv; | ||
} | ||
|
||
namespace NodeJS { | ||
interface ProcessEnv { | ||
DB_PASSWORD: string; | ||
} | ||
} | ||
``` | ||
|
||
If you need more control over environment variables in Astro, we recommend you use [`astro:env`](/en/guides/environment-variables/). |
50 changes: 0 additions & 50 deletions
50
src/content/docs/fr/reference/experimental-flags/raw-env-values.mdx
This file was deleted.
Oops, something went wrong.
50 changes: 0 additions & 50 deletions
50
src/content/docs/ko/reference/experimental-flags/raw-env-values.mdx
This file was deleted.
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.