Home / File/ vite-plugin-import-meta-env.ts — astro Source File

vite-plugin-import-meta-env.ts — astro Source File

Architecture documentation for vite-plugin-import-meta-env.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 6 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb["vite-plugin-import-meta-env.ts"]
  11b32933_a884_3b27_f001_e2d1b1259b07["./env-loader.js"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb --> 11b32933_a884_3b27_f001_e2d1b1259b07
  a370a45c_02f1_30de_445d_47ee08d095a2["../core/viteUtils.js"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb --> a370a45c_02f1_30de_445d_47ee08d095a2
  f202f4fa_f9f9_9398_9510_7a42ce8ea007["../environments.js"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb --> f202f4fa_f9f9_9398_9510_7a42ce8ea007
  972c6fe0_4a8d_d55e_c1b0_a57c5bf4ed60["esbuild"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb --> 972c6fe0_4a8d_d55e_c1b0_a57c5bf4ed60
  57045b72_8cbd_6e93_7687_5176cbcd502d["magic-string"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb --> 57045b72_8cbd_6e93_7687_5176cbcd502d
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  2dd3f86b_2363_453a_ea93_81e9bdfe65eb --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 2dd3f86b_2363_453a_ea93_81e9bdfe65eb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { transform } from 'esbuild';
import MagicString from 'magic-string';
import type * as vite from 'vite';
import type { EnvLoader } from './env-loader.js';
import { CSS_LANGS_RE } from '../core/viteUtils.js';
import { isAstroClientEnvironment } from '../environments.js';

interface EnvPluginOptions {
	envLoader: EnvLoader;
}

// Match `import.meta.env` directly without trailing property access
const importMetaEnvOnlyRe = /\bimport\.meta\.env\b(?!\.)/;

function getReferencedPrivateKeys(source: string, privateEnv: Record<string, any>): Set<string> {
	const references = new Set<string>();
	for (const key in privateEnv) {
		if (source.includes(key)) {
			references.add(key);
		}
	}
	return references;
}

/**
 * Use esbuild to perform replacememts like Vite
 * https://github.com/vitejs/vite/blob/5ea9edbc9ceb991e85f893fe62d68ed028677451/packages/vite/src/node/plugins/define.ts#L130
 */
async function replaceDefine(
	code: string,
	id: string,
	define: Record<string, string>,
	config: vite.ResolvedConfig,
): Promise<{ code: string; map: string | null }> {
	// Since esbuild doesn't support replacing complex expressions, we replace `import.meta.env`
	// with a marker string first, then postprocess and apply the `Object.assign` code.
	const replacementMarkers: Record<string, string> = {};
	const env = define['import.meta.env'];
	if (env) {
		// Compute the marker from the length of the replaced code. We do this so that esbuild generates
		// the sourcemap with the right column offset when we do the postprocessing.
		const marker = `__astro_import_meta_env${'_'.repeat(
			env.length - 23 /* length of preceding string */,
		)}`;
		replacementMarkers[marker] = env;
		define = { ...define, 'import.meta.env': marker };
	}

	const esbuildOptions = config.esbuild || {};

	const result = await transform(code, {
		loader: 'js',
		charset: esbuildOptions.charset ?? 'utf8',
		platform: 'neutral',
		define,
		sourcefile: id,
		sourcemap: config.command === 'build' ? !!config.build.sourcemap : true,
	});

	for (const marker in replacementMarkers) {
// ... (109 more lines)

Domain

Subdomains

Dependencies

  • ../core/viteUtils.js
  • ../environments.js
  • ./env-loader.js
  • esbuild
  • magic-string
  • vite

Frequently Asked Questions

What does vite-plugin-import-meta-env.ts do?
vite-plugin-import-meta-env.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 vite-plugin-import-meta-env.ts?
vite-plugin-import-meta-env.ts defines 3 function(s): getReferencedPrivateKeys, importMetaEnv, replaceDefine.
What does vite-plugin-import-meta-env.ts depend on?
vite-plugin-import-meta-env.ts imports 6 module(s): ../core/viteUtils.js, ../environments.js, ./env-loader.js, esbuild, magic-string, vite.
Where is vite-plugin-import-meta-env.ts in the architecture?
vite-plugin-import-meta-env.ts is located at packages/astro/src/env/vite-plugin-import-meta-env.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