-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not freeze process.env in dev (#12585)
Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
e21c7e6
commit a9373c0
Showing
12 changed files
with
113 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fixes a case where `process.env` would be frozen despite changes made to environment variables in development |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,60 @@ | ||
import { loadEnv } from 'vite'; | ||
import type { AstroConfig } from '../types/public/index.js'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
// Match valid JS variable names (identifiers), which accepts most alphanumeric characters, | ||
// except that the first character cannot be a number. | ||
const isValidIdentifierRe = /^[_$a-zA-Z][\w$]*$/; | ||
|
||
function getPrivateEnv( | ||
fullEnv: Record<string, string>, | ||
astroConfig: AstroConfig, | ||
): Record<string, string> { | ||
const viteConfig = astroConfig.vite; | ||
let envPrefixes: string[] = ['PUBLIC_']; | ||
if (viteConfig.envPrefix) { | ||
envPrefixes = Array.isArray(viteConfig.envPrefix) | ||
? viteConfig.envPrefix | ||
: [viteConfig.envPrefix]; | ||
} | ||
|
||
const privateEnv: Record<string, string> = {}; | ||
for (const key in fullEnv) { | ||
// Ignore public env var | ||
if (isValidIdentifierRe.test(key) && envPrefixes.every((prefix) => !key.startsWith(prefix))) { | ||
if (typeof process.env[key] !== 'undefined') { | ||
let value = process.env[key]; | ||
// Replacements are always strings, so try to convert to strings here first | ||
if (typeof value !== 'string') { | ||
value = `${value}`; | ||
} | ||
// 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]); | ||
} | ||
} | ||
} | ||
return privateEnv; | ||
} | ||
|
||
export const createEnvLoader = () => { | ||
let privateEnv: Record<string, string> = {}; | ||
return { | ||
load: (mode: string, config: AstroConfig) => { | ||
const loaded = loadEnv(mode, config.vite.envDir ?? fileURLToPath(config.root), ''); | ||
privateEnv = getPrivateEnv(loaded, config); | ||
return loaded; | ||
}, | ||
getPrivateEnv: () => { | ||
return privateEnv; | ||
}, | ||
}; | ||
}; | ||
|
||
export type EnvLoader = ReturnType<typeof createEnvLoader>; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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