Home / Function/ replaceDefine() — astro Function Reference

replaceDefine() — astro Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

packages/astro/src/env/vite-plugin-import-meta-env.ts lines 29–68

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) {
		result.code = result.code.replaceAll(marker, replacementMarkers[marker]);
	}

	return {
		code: result.code,
		map: result.map || null,
	};
}

Domain

Subdomains

Called By

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free