Home / Function/ astroEnv() — astro Function Reference

astroEnv() — astro Function Reference

Architecture documentation for the astroEnv() function in vite-plugin-env.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  1558dc0d_2a73_5227_6f33_9362e6487ba5["astroEnv()"]
  241a5d79_df85_b217_dc9c_a74671cc544b["vite-plugin-env.ts"]
  1558dc0d_2a73_5227_6f33_9362e6487ba5 -->|defined in| 241a5d79_df85_b217_dc9c_a74671cc544b
  2e8a8a54_e127_d1ab_0627_d7000b736f47["validatePublicVariables()"]
  1558dc0d_2a73_5227_6f33_9362e6487ba5 -->|calls| 2e8a8a54_e127_d1ab_0627_d7000b736f47
  b965fd87_d9a7_8cec_eddf_d034000cbcb5["getTemplates()"]
  1558dc0d_2a73_5227_6f33_9362e6487ba5 -->|calls| b965fd87_d9a7_8cec_eddf_d034000cbcb5
  style 1558dc0d_2a73_5227_6f33_9362e6487ba5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/env/vite-plugin-env.ts lines 26–119

export function astroEnv({ settings, sync, envLoader }: AstroEnvPluginParams): Plugin {
	const { schema, validateSecrets } = settings.config.env;
	let isBuild: boolean;
	let populated = false;

	return {
		name: 'astro-env-plugin',
		enforce: 'pre',
		config(_, { command }) {
			isBuild = command === 'build';
		},
		buildStart() {
			if (!isBuild || populated) {
				return;
			}

			const loadedEnv = envLoader.get();

			// During the build, we populate process.env so that secrets can work
			for (const [key, value] of Object.entries(loadedEnv)) {
				if (value !== undefined) {
					process.env[key] = value;
				}
			}

			populated = true;
		},
		resolveId: {
			filter: {
				id: new RegExp(
					`^(${CLIENT_VIRTUAL_MODULE_ID}|${SERVER_VIRTUAL_MODULE_ID}|${INTERNAL_VIRTUAL_MODULE_ID})$`,
				),
			},
			handler(id) {
				if (id === CLIENT_VIRTUAL_MODULE_ID) {
					return RESOLVED_CLIENT_VIRTUAL_MODULE_ID;
				}
				if (id === SERVER_VIRTUAL_MODULE_ID) {
					return RESOLVED_SERVER_VIRTUAL_MODULE_ID;
				}
				if (id === INTERNAL_VIRTUAL_MODULE_ID) {
					return RESOLVED_INTERNAL_VIRTUAL_MODULE_ID;
				}
			},
		},
		load: {
			filter: {
				id: new RegExp(
					`^(${RESOLVED_CLIENT_VIRTUAL_MODULE_ID}|${RESOLVED_SERVER_VIRTUAL_MODULE_ID}|${RESOLVED_INTERNAL_VIRTUAL_MODULE_ID})$`,
				),
			},
			handler(id) {
				if (id === RESOLVED_INTERNAL_VIRTUAL_MODULE_ID) {
					return { code: `export const schema = ${JSON.stringify(schema)};` };
				}

				if (
					id === RESOLVED_SERVER_VIRTUAL_MODULE_ID &&
					isAstroClientEnvironment(this.environment)
				) {
					throw new AstroError({
						...AstroErrorData.ServerOnlyModule,
						message: AstroErrorData.ServerOnlyModule.message(SERVER_VIRTUAL_MODULE_ID),
					});
				}

				if (id === RESOLVED_CLIENT_VIRTUAL_MODULE_ID || id === RESOLVED_SERVER_VIRTUAL_MODULE_ID) {
					const loadedEnv = envLoader.get();

					const validatedVariables = validatePublicVariables({
						schema,
						loadedEnv,
						validateSecrets,
						sync,
					});
					const { client, server } = getTemplates({
						schema,
						validatedVariables,
						// In dev, we inline process.env to avoid freezing it
						loadedEnv: isBuild ? null : loadedEnv,
					});

Domain

Subdomains

Frequently Asked Questions

What does astroEnv() do?
astroEnv() is a function in the astro codebase, defined in packages/astro/src/env/vite-plugin-env.ts.
Where is astroEnv() defined?
astroEnv() is defined in packages/astro/src/env/vite-plugin-env.ts at line 26.
What does astroEnv() call?
astroEnv() calls 2 function(s): getTemplates, validatePublicVariables.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free