Home / File/ env-loader.ts — astro Source File

env-loader.ts — astro Source File

Architecture documentation for env-loader.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 3 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  21e52eb2_d36f_da11_2633_402d7db8363a["env-loader.ts"]
  baa53824_73a3_1e03_2043_4d0c058ecca5["../types/public/index.js"]
  21e52eb2_d36f_da11_2633_402d7db8363a --> baa53824_73a3_1e03_2043_4d0c058ecca5
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  21e52eb2_d36f_da11_2633_402d7db8363a --> d9a92db9_c95e_9165_13ac_24b3d859d946
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  21e52eb2_d36f_da11_2633_402d7db8363a --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 21e52eb2_d36f_da11_2633_402d7db8363a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { fileURLToPath } from 'node:url';
import { loadEnv } from 'vite';
import type { AstroConfig } from '../types/public/index.js';

// 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$]*$/;

/**
 * From public env, returns private env. Each value may be stringified, transformed as `process.env`
 * or coerced depending on options.
 */
function getPrivateEnv({
	fullEnv,
	viteConfig,
}: {
	fullEnv: Record<string, string>;
	viteConfig: AstroConfig['vite'];
}): Record<string, string> {
	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.some((prefix) => key.startsWith(prefix))) {
			continue;
		}
		privateEnv[key] = JSON.stringify(fullEnv[key]);
	}
	return privateEnv;
}

interface EnvLoaderOptions {
	mode: string;
	config: AstroConfig;
}

function getEnv({ mode, config }: EnvLoaderOptions) {
	const loaded = loadEnv(mode, config.vite.envDir ?? fileURLToPath(config.root), '');
	const privateEnv = getPrivateEnv({ fullEnv: loaded, viteConfig: config.vite });

	return { loaded, privateEnv };
}

export const createEnvLoader = (options: EnvLoaderOptions) => {
	let { loaded, privateEnv } = getEnv(options);
	return {
		get: () => {
			// We refresh the env we have in case process.env has been updated since creating
			// the env loader. That can happen in eg. integrations
			({ loaded, privateEnv } = getEnv(options));
			return loaded;
		},
		getPrivateEnv: () => privateEnv,
	};
};

export type EnvLoader = ReturnType<typeof createEnvLoader>;

Domain

Subdomains

Dependencies

  • ../types/public/index.js
  • node:url
  • vite

Frequently Asked Questions

What does env-loader.ts do?
env-loader.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in env-loader.ts?
env-loader.ts defines 3 function(s): createEnvLoader, getEnv, getPrivateEnv.
What does env-loader.ts depend on?
env-loader.ts imports 3 module(s): ../types/public/index.js, node:url, vite.
Where is env-loader.ts in the architecture?
env-loader.ts is located at packages/astro/src/env/env-loader.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/env).

Analyze Your Own Codebase

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

Try Supermodel Free