mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
fix: do not override process.env (#12227)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
parent
7bcae4e3ee
commit
8b1a641be9
4 changed files with 13 additions and 12 deletions
5
.changeset/real-actors-jog.md
Normal file
5
.changeset/real-actors-jog.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes a case where environment variables would not be refreshed when using `astro:env`
|
1
packages/astro/src/env/runtime-constants.ts
vendored
Normal file
1
packages/astro/src/env/runtime-constants.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export const ENV_SYMBOL = Symbol.for('astro:env/dev');
|
6
packages/astro/src/env/runtime.ts
vendored
6
packages/astro/src/env/runtime.ts
vendored
|
@ -1,4 +1,5 @@
|
|||
import { AstroError, AstroErrorData } from '../core/errors/index.js';
|
||||
import { ENV_SYMBOL } from './runtime-constants.js';
|
||||
import { invalidVariablesToError } from './errors.js';
|
||||
import type { ValidationResultInvalid } from './validators.js';
|
||||
export { validateEnvVariable, getEnvFieldType } from './validators.js';
|
||||
|
@ -6,7 +7,10 @@ export { validateEnvVariable, getEnvFieldType } from './validators.js';
|
|||
export type GetEnv = (key: string) => string | undefined;
|
||||
type OnSetGetEnv = (reset: boolean) => void;
|
||||
|
||||
let _getEnv: GetEnv = (key) => process.env[key];
|
||||
let _getEnv: GetEnv = (key) => {
|
||||
const env = (globalThis as any)[ENV_SYMBOL] ?? {};
|
||||
return env[key];
|
||||
};
|
||||
|
||||
export function setGetEnv(fn: GetEnv, reset = false) {
|
||||
_getEnv = fn;
|
||||
|
|
13
packages/astro/src/env/vite-plugin-env.ts
vendored
13
packages/astro/src/env/vite-plugin-env.ts
vendored
|
@ -11,12 +11,7 @@ import {
|
|||
import { type InvalidVariable, invalidVariablesToError } from './errors.js';
|
||||
import type { EnvSchema } from './schema.js';
|
||||
import { getEnvFieldType, validateEnvVariable } from './validators.js';
|
||||
|
||||
// TODO: reminders for when astro:env comes out of experimental
|
||||
// Types should always be generated (like in types/content.d.ts). That means the client module will be empty
|
||||
// and server will only contain getSecret for unknown variables. Then, specifying a schema should only add
|
||||
// variables as needed. For secret variables, it will only require specifying SecretValues and it should get
|
||||
// merged with the static types/content.d.ts
|
||||
import { ENV_SYMBOL } from './runtime-constants.js';
|
||||
|
||||
interface AstroEnvVirtualModPluginParams {
|
||||
settings: AstroSettings;
|
||||
|
@ -47,11 +42,7 @@ export function astroEnv({
|
|||
fileURLToPath(settings.config.root),
|
||||
'',
|
||||
);
|
||||
for (const [key, value] of Object.entries(loadedEnv)) {
|
||||
if (value !== undefined) {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
(globalThis as any)[ENV_SYMBOL] = loadedEnv;
|
||||
|
||||
const validatedVariables = validatePublicVariables({
|
||||
schema,
|
||||
|
|
Loading…
Reference in a new issue