实验性原始环境变量值
类型:boolean
默认:false
¥Type: boolean
Default: false
astro@5.12.0
新
Astro 允许你配置 环境变量的类型安全模式,并将通过 astro:env
导入的变量转换为预期类型。
¥Astro allows you to configure a type-safe schema for your environment variables, and converts variables imported via astro:env
into the expected type.
但是,Astro 在某些情况下也会转换通过 import.meta.env
使用的环境变量,这可能会阻止访问某些值,例如字符串 "true"
(转换为布尔值)和 "1"
(转换为数字)。
¥However, Astro 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).
experimental.rawEnvValues
标志禁用强制从 process.env
填充的 import.meta.env
值,从而允许你使用原始值。
¥The experimental.rawEnvValues
flag disables coercion of import.meta.env
values that are populated from process.env
, allowing you to use the raw value.
要禁用 Astro 对通过 import.meta.env
使用的值的强制转换,请在 Astro 配置中将 experimental.rawEnvValues
标志设置为 true
:
¥To disable Astro’s coercion on values used through import.meta.env
, set the experimental.rawEnvValues
flag to true
in your Astro configuration:
import { defineConfig } from "astro/config"
export default defineConfig({ experimental: { rawEnvValues: true, }})
¥Usage
启用此实验性标志将不再将字符串值转换为布尔值或数字。这使 Astro 中 import.meta.env
的行为与 Vite 保持一致。
¥Enabling this experimental flag will no longer convert string values into booleans or numbers. This aligns import.meta.env
’s behavior in Astro with Vite.
在未来的主要版本中,Astro 将默认切换为不强制转换 import.meta.env
值,但你可以使用 experimental.rawEnvValues
标志提前选择未来的行为,并在必要时相应地使用 更新你的项目。
¥In a future major version, Astro will switch to not coercing import.meta.env
values by default, but you can opt in to the future behavior early using the experimental.rawEnvValues
flag and if necessary, updating your project accordingly.
更新你的项目
Section titled “更新你的项目”¥Updating your project
如果你依赖于此强制转换,则可能需要更新项目代码以手动应用它:
¥If you were relying on this coercion, you may need to update your project code to apply it manually:
const enabled: boolean = import.meta.env.ENABLEDconst enabled: boolean = import.meta.env.ENABLED === "true"
如果你需要在 Astro 中进行强制转换,我们建议你使用 astro:env
。
¥If you need coercion in Astro, we recommend you use astro:env
.