Home / Function/ astro() — astro Function Reference

astro() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  13d113dd_4a25_7029_c624_7d2dbaf4871c["astro()"]
  97247306_c6df_a187_0755_eec66ac1134b["index.ts"]
  13d113dd_4a25_7029_c624_7d2dbaf4871c -->|defined in| 97247306_c6df_a187_0755_eec66ac1134b
  3ca272ed_eb00_d38b_5345_6dd2041b384c["appendSourceMap()"]
  13d113dd_4a25_7029_c624_7d2dbaf4871c -->|calls| 3ca272ed_eb00_d38b_5345_6dd2041b384c
  style 13d113dd_4a25_7029_c624_7d2dbaf4871c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-astro/index.ts lines 28–321

export default function astro({ settings, logger }: AstroPluginOptions): vite.Plugin[] {
	const { config } = settings;
	let server: vite.ViteDevServer | undefined;
	let compile: (code: string, filename: string) => Promise<CompileAstroResult>;
	// Each Astro file has its own compile metadata so that its scripts and styles virtual module
	// can retrieve their code from here.
	// NOTE: We need to initialize a map here and in `buildStart` because our unit tests don't
	// call `buildStart` (test bug)
	let astroFileToCompileMetadata = new Map<string, CompileMetadata>();

	// Variables for determining if an id starts with /src...
	const srcRootWeb = config.srcDir.pathname.slice(config.root.pathname.length - 1);
	const isBrowserPath = (path: string) => path.startsWith(srcRootWeb) && srcRootWeb !== '/';
	const notAstroComponent = (component: HydratedComponent) =>
		!component.resolvedPath.endsWith('.astro');

	return [
		{
			name: 'astro:build:css-hmr',
			enforce: 'pre',
			applyToEnvironment(environment) {
				return environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.client;
			},
			transform: {
				filter: {
					id: {
						include: [/(?:\?|&)astro(?:&|=|$)/],
						// ignore astro file sub-requests, e.g. Foo.astro?astro&type=script&index=0&lang.ts
						exclude: [specialQueriesRE],
					},
				},
				async handler(_source, id) {
					const parsedId = parseAstroRequest(id);
					// Special edge case handling for Vite 6 beta, the style dependencies need to be registered here take affect
					// TODO: Remove this when Vite fixes it (https://github.com/vitejs/vite/pull/18103)
					const astroFilename = normalizePath(normalizeFilename(parsedId.filename, config.root));
					const compileMetadata = astroFileToCompileMetadata.get(astroFilename);
					if (compileMetadata && parsedId.query.type === 'style' && parsedId.query.index != null) {
						const result = compileMetadata.css[parsedId.query.index];

						// Register dependencies from preprocessing this style
						result.dependencies?.forEach((dep) => this.addWatchFile(dep));
					}
				},
			},
		},
		{
			name: 'astro:build',
			enforce: 'pre', // run transforms before other plugins can
			async configEnvironment(name, viteConfig, opts) {
				viteConfig.resolve ??= {};
				// Emulate Vite default fallback for `resolve.conditions` if not set
				if (viteConfig.resolve.conditions == null) {
					if (viteConfig.consumer === 'client' || name === 'client' || opts.isSsrTargetWebworker) {
						viteConfig.resolve.conditions = [...defaultClientConditions];
					} else {
						viteConfig.resolve.conditions = [...defaultServerConditions];
					}
				}
				viteConfig.resolve.conditions.push('astro');
			},
			async configResolved(viteConfig) {
				const toolbarEnabled = await settings.preferences.get('devToolbar.enabled');
				// Initialize `compile` function to simplify usage later
				compile = (code, filename) => {
					return compileAstro({
						compileProps: {
							astroConfig: config,
							viteConfig,
							toolbarEnabled,
							filename,
							source: code,
						},
						astroFileToCompileMetadata,
						logger,
					});
				};
			},
			configureServer(_server) {
				server = _server;
				// Make sure deleted files are removed from the compile metadata to save memory

Domain

Subdomains

Frequently Asked Questions

What does astro() do?
astro() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-astro/index.ts.
Where is astro() defined?
astro() is defined in packages/astro/src/vite-plugin-astro/index.ts at line 28.
What does astro() call?
astro() calls 1 function(s): appendSourceMap.

Analyze Your Own Codebase

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

Try Supermodel Free