summaryrefslogtreecommitdiff
path: root/packages/astro/src/env/vite-plugin-env.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/astro/src/env/vite-plugin-env.ts')
-rw-r--r--packages/astro/src/env/vite-plugin-env.ts34
1 files changed, 10 insertions, 24 deletions
diff --git a/packages/astro/src/env/vite-plugin-env.ts b/packages/astro/src/env/vite-plugin-env.ts
index 06d5b047c..816f460b3 100644
--- a/packages/astro/src/env/vite-plugin-env.ts
+++ b/packages/astro/src/env/vite-plugin-env.ts
@@ -1,8 +1,8 @@
-import type fsMod from 'node:fs';
+import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { type Plugin, loadEnv } from 'vite';
-import type { AstroSettings } from '../@types/astro.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
+import type { AstroSettings } from '../types/astro.js';
import {
MODULE_TEMPLATE_URL,
VIRTUAL_MODULES_IDS,
@@ -12,23 +12,14 @@ import { type InvalidVariable, invalidVariablesToError } from './errors.js';
import type { EnvSchema } from './schema.js';
import { getEnvFieldType, validateEnvVariable } from './validators.js';
-interface AstroEnvVirtualModPluginParams {
+interface AstroEnvPluginParams {
settings: AstroSettings;
- mode: 'dev' | 'build' | string;
- fs: typeof fsMod;
+ mode: string;
sync: boolean;
}
-export function astroEnv({
- settings,
- mode,
- fs,
- sync,
-}: AstroEnvVirtualModPluginParams): Plugin | undefined {
- if (!settings.config.experimental.env) {
- return;
- }
- const schema = settings.config.experimental.env.schema ?? {};
+export function astroEnv({ settings, mode, sync }: AstroEnvPluginParams): Plugin {
+ const { schema, validateSecrets } = settings.config.env;
let templates: { client: string; server: string; internal: string } | null = null;
@@ -36,11 +27,7 @@ export function astroEnv({
name: 'astro-env-plugin',
enforce: 'pre',
buildStart() {
- const loadedEnv = loadEnv(
- mode === 'dev' ? 'development' : 'production',
- fileURLToPath(settings.config.root),
- '',
- );
+ const loadedEnv = loadEnv(mode, fileURLToPath(settings.config.root), '');
for (const [key, value] of Object.entries(loadedEnv)) {
if (value !== undefined) {
process.env[key] = value;
@@ -50,12 +37,12 @@ export function astroEnv({
const validatedVariables = validatePublicVariables({
schema,
loadedEnv,
- validateSecrets: settings.config.experimental.env?.validateSecrets ?? false,
+ validateSecrets,
sync,
});
templates = {
- ...getTemplates(schema, fs, validatedVariables),
+ ...getTemplates(schema, validatedVariables),
internal: `export const schema = ${JSON.stringify(schema)};`,
};
},
@@ -134,11 +121,10 @@ function validatePublicVariables({
function getTemplates(
schema: EnvSchema,
- fs: typeof fsMod,
validatedVariables: ReturnType<typeof validatePublicVariables>,
) {
let client = '';
- let server = fs.readFileSync(MODULE_TEMPLATE_URL, 'utf-8');
+ let server = readFileSync(MODULE_TEMPLATE_URL, 'utf-8');
let onSetGetEnv = '';
for (const { key, value, context } of validatedVariables) {