diff options
| author | 2023-05-01 10:51:47 -0500 | |
|---|---|---|
| committer | 2023-05-01 10:51:47 -0500 | |
| commit | 895fa07d8b4b8359984e048daca5437e40f44390 (patch) | |
| tree | f17201e4a2bf5022c9acdfff1300058626c70bac | |
| parent | e5bd084c01e4f60a157969b50c05ce002f7b63d2 (diff) | |
| download | astro-895fa07d8b4b8359984e048daca5437e40f44390.tar.gz astro-895fa07d8b4b8359984e048daca5437e40f44390.tar.zst astro-895fa07d8b4b8359984e048daca5437e40f44390.zip | |
fix: inline process.env boolean values (0, 1, false, true) (#6910)
| -rw-r--r-- | .changeset/shaggy-berries-accept.md | 5 | ||||
| -rw-r--r-- | packages/astro/src/vite-plugin-env/index.ts | 9 |
2 files changed, 13 insertions, 1 deletions
diff --git a/.changeset/shaggy-berries-accept.md b/.changeset/shaggy-berries-accept.md new file mode 100644 index 000000000..6c6c9f761 --- /dev/null +++ b/.changeset/shaggy-berries-accept.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Inline `process.env` boolean values (`0`, `1`, `true`, `false`) during the build. This helps with DCE and allows for better `export const prerender` detection. diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index ab8816000..1f21696e3 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -31,7 +31,14 @@ function getPrivateEnv( // Ignore public env var if (envPrefixes.every((prefix) => !key.startsWith(prefix))) { if (typeof process.env[key] !== 'undefined') { - privateEnv[key] = `process.env.${key}`; + const value = process.env[key]; + // Boolean values should be inlined to support `export const prerender` + // We already know that these are NOT sensitive values, so inlining is safe + if (value === '0' || value === '1' || value === 'true' || value === 'false') { + privateEnv[key] = value; + } else { + privateEnv[key] = `process.env.${key}`; + } } else { privateEnv[key] = JSON.stringify(fullEnv[key]); } |
