Home / Function/ compile() — astro Function Reference

compile() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  4f0814b1_1631_6082_74c4_664cd0f6b457["compile()"]
  d1d31d2a_5ee0_0540_2ca3_25d0e761efc6["compile.ts"]
  4f0814b1_1631_6082_74c4_664cd0f6b457 -->|defined in| d1d31d2a_5ee0_0540_2ca3_25d0e761efc6
  8d940ba8_7083_ef03_a9ab_0de2fb08a8cb["normalizeFilename()"]
  4f0814b1_1631_6082_74c4_664cd0f6b457 -->|calls| 8d940ba8_7083_ef03_a9ab_0de2fb08a8cb
  4340809b_c461_1622_c24e_777b6a21aa42["handleCompileResultErrors()"]
  4f0814b1_1631_6082_74c4_664cd0f6b457 -->|calls| 4340809b_c461_1622_c24e_777b6a21aa42
  style 4f0814b1_1631_6082_74c4_664cd0f6b457 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/compile/compile.ts lines 25–92

export async function compile({
	astroConfig,
	viteConfig,
	toolbarEnabled,
	filename,
	source,
}: CompileProps): Promise<CompileResult> {
	// Because `@astrojs/compiler` can't return the dependencies for each style transformed,
	// we need to use an external array to track the dependencies whenever preprocessing is called,
	// and we'll rebuild the final `css` result after transformation.
	const cssPartialCompileResults: PartialCompileCssResult[] = [];
	const cssTransformErrors: AstroError[] = [];
	let transformResult: TransformResult;

	try {
		// Transform from `.astro` to valid `.ts`
		// use `sourcemap: "both"` so that sourcemap is included in the code
		// result passed to esbuild, but also available in the catch handler.
		transformResult = await transform(source, {
			compact: astroConfig.compressHTML,
			filename,
			normalizedFilename: normalizeFilename(filename, astroConfig.root),
			sourcemap: 'both',
			internalURL: 'astro/compiler-runtime',
			// TODO: remove in Astro v7
			astroGlobalArgs: JSON.stringify(astroConfig.site),
			scopedStyleStrategy: astroConfig.scopedStyleStrategy,
			resultScopedSlot: true,
			transitionsAnimationURL: 'astro/components/viewtransitions.css',
			annotateSourceFile:
				viteConfig.command === 'serve' &&
				astroConfig.devToolbar &&
				astroConfig.devToolbar.enabled &&
				toolbarEnabled,
			preprocessStyle: createStylePreprocessor({
				filename,
				viteConfig,
				astroConfig,
				cssPartialCompileResults,
				cssTransformErrors,
			}),
			async resolvePath(specifier) {
				return resolvePath(specifier, filename);
			},
		});
	} catch (err: any) {
		// The compiler should be able to handle errors by itself, however
		// for the rare cases where it can't let's directly throw here with as much info as possible
		throw new CompilerError({
			...AstroErrorData.UnknownCompilerError,
			message: err.message ?? 'Unknown compiler error',
			stack: err.stack,
			location: {
				file: filename,
			},
		});
	}

	handleCompileResultErrors(transformResult, cssTransformErrors);

	return {
		...transformResult,
		css: transformResult.css.map((code, i) => ({
			...cssPartialCompileResults[i],
			code,
		})),
	};
}

Domain

Subdomains

Frequently Asked Questions

What does compile() do?
compile() is a function in the astro codebase, defined in packages/astro/src/core/compile/compile.ts.
Where is compile() defined?
compile() is defined in packages/astro/src/core/compile/compile.ts at line 25.
What does compile() call?
compile() calls 2 function(s): handleCompileResultErrors, normalizeFilename.

Analyze Your Own Codebase

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

Try Supermodel Free