Home / Function/ configHeadVitePlugin() — astro Function Reference

configHeadVitePlugin() — astro Function Reference

Architecture documentation for the configHeadVitePlugin() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  5627396f_6feb_9209_b5b4_e15be65417eb["configHeadVitePlugin()"]
  bf5798b6_a592_be5c_0413_36786059a0dc["index.ts"]
  5627396f_6feb_9209_b5b4_e15be65417eb -->|defined in| bf5798b6_a592_be5c_0413_36786059a0dc
  style 5627396f_6feb_9209_b5b4_e15be65417eb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-head/index.ts lines 14–87

export default function configHeadVitePlugin(): vite.Plugin {
	let environment: DevEnvironment;

	function propagateMetadata<
		P extends keyof PluginMetadata['astro'],
		V extends PluginMetadata['astro'][P],
	>(
		this: { getModuleInfo(id: string): ModuleInfo | null },
		id: string,
		prop: P,
		value: V,
		seen = new Set<string>(),
	) {
		if (seen.has(id)) return;
		seen.add(id);
		const mod = environment.moduleGraph.getModuleById(id);
		const info = this.getModuleInfo(id);

		if (info?.meta.astro) {
			const astroMetadata = getAstroMetadata(info);
			if (astroMetadata) {
				Reflect.set(astroMetadata, prop, value);
			}
		}

		for (const parent of mod?.importers || []) {
			if (parent.id) {
				propagateMetadata.call(this, parent.id, prop, value, seen);
			}
		}
	}

	return {
		name: 'astro:head-metadata',
		enforce: 'pre',
		apply: 'serve',
		configureServer(server) {
			environment = server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr];
		},
		resolveId(source, importer) {
			if (importer) {
				// Do propagation any time a new module is imported. This is because
				// A module with propagation might be loaded before one of its parent pages
				// is loaded, in which case that parent page won't have the in-tree and containsHead
				// values. Walking up the tree in resolveId ensures that they do
				return this.resolve(source, importer, { skipSelf: true }).then((result) => {
					if (result) {
						let info = this.getModuleInfo(result.id);
						const astro = info && getAstroMetadata(info);
						if (astro) {
							if (astro.propagation === 'self' || astro.propagation === 'in-tree') {
								propagateMetadata.call(this, importer, 'propagation', 'in-tree');
							}
							if (astro.containsHead) {
								propagateMetadata.call(this, importer, 'containsHead', true);
							}
						}
					}
					return result;
				});
			}
		},
		transform(source, id) {
			let info = this.getModuleInfo(id);
			if (info && getAstroMetadata(info)?.containsHead) {
				propagateMetadata.call(this, id, 'containsHead', true);
			}

			if (injectExp.test(source)) {
				propagateMetadata.call(this, id, 'propagation', 'in-tree');
			}
		},
	};
}

Domain

Subdomains

Frequently Asked Questions

What does configHeadVitePlugin() do?
configHeadVitePlugin() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-head/index.ts.
Where is configHeadVitePlugin() defined?
configHeadVitePlugin() is defined in packages/astro/src/vite-plugin-head/index.ts at line 14.

Analyze Your Own Codebase

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

Try Supermodel Free