Home / Function/ importMetaEnv() — astro Function Reference

importMetaEnv() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  6badd6ee_3ca9_2b38_7879_f8cf39409247["importMetaEnv()"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb["vite-plugin-import-meta-env.ts"]
  6badd6ee_3ca9_2b38_7879_f8cf39409247 -->|defined in| 2dd3f86b_2363_453a_ea93_81e9bdfe65eb
  8c7cb946_5553_f865_5667_398baf801f47["getReferencedPrivateKeys()"]
  6badd6ee_3ca9_2b38_7879_f8cf39409247 -->|calls| 8c7cb946_5553_f865_5667_398baf801f47
  0c5ce825_febb_b8a5_72a4_a992a1425b60["replaceDefine()"]
  6badd6ee_3ca9_2b38_7879_f8cf39409247 -->|calls| 0c5ce825_febb_b8a5_72a4_a992a1425b60
  style 6badd6ee_3ca9_2b38_7879_f8cf39409247 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/env/vite-plugin-import-meta-env.ts lines 70–168

export function importMetaEnv({ envLoader }: EnvPluginOptions): vite.Plugin {
	let privateEnv: Record<string, string>;
	let defaultDefines: Record<string, string>;
	let isDev: boolean;
	let devImportMetaEnvPrepend: string;
	let viteConfig: vite.ResolvedConfig;
	return {
		name: 'astro:vite-plugin-env',
		config(_, { command }) {
			isDev = command !== 'build';
		},
		configResolved(resolvedConfig) {
			viteConfig = resolvedConfig;

			// HACK: move ourselves before Vite's define plugin to apply replacements at the right time (before Vite normal plugins)
			const viteDefinePluginIndex = resolvedConfig.plugins.findIndex(
				(p) => p.name === 'vite:define',
			);
			if (viteDefinePluginIndex !== -1) {
				const myPluginIndex = resolvedConfig.plugins.findIndex(
					(p) => p.name === 'astro:vite-plugin-env',
				);
				if (myPluginIndex !== -1) {
					const myPlugin = resolvedConfig.plugins[myPluginIndex];
					// @ts-ignore-error ignore readonly annotation
					resolvedConfig.plugins.splice(viteDefinePluginIndex, 0, myPlugin);
					// @ts-ignore-error ignore readonly annotation
					resolvedConfig.plugins.splice(myPluginIndex, 1);
				}
			}
		},

		transform: {
			filter: {
				id: {
					exclude: [/.*\.(html|htm|json)$/, CSS_LANGS_RE],
				},
				code: /import\.meta\.env/,
			},
			handler(source, id) {
				if (isAstroClientEnvironment(this.environment) || viteConfig.assetsInclude(id)) {
					return;
				}
				// Find matches for *private* env and do our own replacement.
				// Env is retrieved before process.env is populated by astro:env
				// so that import.meta.env is first replaced by values, not process.env
				privateEnv ??= envLoader.getPrivateEnv();

				// In dev, we can assign the private env vars to `import.meta.env` directly for performance
				if (isDev) {
					const s = new MagicString(source);

					if (!devImportMetaEnvPrepend) {
						devImportMetaEnvPrepend = `Object.assign(import.meta.env,{`;
						for (const key in privateEnv) {
							devImportMetaEnvPrepend += `${key}:${privateEnv[key]},`;
						}
						devImportMetaEnvPrepend += '});';
					}
					s.prepend(devImportMetaEnvPrepend);

					return {
						code: s.toString(),
						map: s.generateMap({ hires: 'boundary' }),
					};
				}

				// In build, use esbuild to perform replacements. Compute the default defines for esbuild here as a
				// separate object as it could be extended by `import.meta.env` later.
				if (!defaultDefines) {
					defaultDefines = {};
					for (const key in privateEnv) {
						defaultDefines[`import.meta.env.${key}`] = privateEnv[key];
					}
				}

				let defines = defaultDefines;

				// If reference the `import.meta.env` object directly, we want to inject private env vars
				// into Vite's injected `import.meta.env` object. To do this, we use `Object.assign` and keeping
				// the `import.meta.env` identifier so Vite sees it.

Domain

Subdomains

Frequently Asked Questions

What does importMetaEnv() do?
importMetaEnv() is a function in the astro codebase, defined in packages/astro/src/env/vite-plugin-import-meta-env.ts.
Where is importMetaEnv() defined?
importMetaEnv() is defined in packages/astro/src/env/vite-plugin-import-meta-env.ts at line 70.
What does importMetaEnv() call?
importMetaEnv() calls 2 function(s): getReferencedPrivateKeys, replaceDefine.

Analyze Your Own Codebase

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

Try Supermodel Free