From 895fa07d8b4b8359984e048daca5437e40f44390 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 1 May 2023 10:51:47 -0500 Subject: [PATCH] fix: inline process.env boolean values (0, 1, false, true) (#6910) --- .changeset/shaggy-berries-accept.md | 5 +++++ packages/astro/src/vite-plugin-env/index.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changeset/shaggy-berries-accept.md diff --git a/.changeset/shaggy-berries-accept.md b/.changeset/shaggy-berries-accept.md new file mode 100644 index 0000000000..6c6c9f7619 --- /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 ab88160005..1f21696e30 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]); }