fix: inline process.env boolean values (0, 1, false, true) (#6910)

This commit is contained in:
Nate Moore 2023-05-01 10:51:47 -05:00 committed by GitHub
parent e5bd084c01
commit 895fa07d8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -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.

View file

@ -31,7 +31,14 @@ function getPrivateEnv(
// Ignore public env var // Ignore public env var
if (envPrefixes.every((prefix) => !key.startsWith(prefix))) { if (envPrefixes.every((prefix) => !key.startsWith(prefix))) {
if (typeof process.env[key] !== 'undefined') { if (typeof process.env[key] !== 'undefined') {
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}`; privateEnv[key] = `process.env.${key}`;
}
} else { } else {
privateEnv[key] = JSON.stringify(fullEnv[key]); privateEnv[key] = JSON.stringify(fullEnv[key]);
} }