Home / Function/ fontsPlugin() — astro Function Reference

fontsPlugin() — astro Function Reference

Architecture documentation for the fontsPlugin() function in vite-plugin-fonts.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  c267776a_7f3c_bdd8_3945_991f21b452eb["fontsPlugin()"]
  2ba9f2da_4496_f3c2_ba41_537f99208811["vite-plugin-fonts.ts"]
  c267776a_7f3c_bdd8_3945_991f21b452eb -->|defined in| 2ba9f2da_4496_f3c2_ba41_537f99208811
  style c267776a_7f3c_bdd8_3945_991f21b452eb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/fonts/vite-plugin-fonts.ts lines 60–341

export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
	// We don't need to worry about config.trailingSlash because we are dealing with
	// static assets only, ie. trailingSlash: 'never'
	const assetsDir = prependForwardSlash(
		appendForwardSlash(joinPaths(settings.config.build.assets, ASSETS_DIR)),
	);
	const baseUrl = joinPaths(settings.config.base, assetsDir);

	// We initialize shared variables here and reset them in buildEnd
	// to avoid locking memory
	let fontFileById: FontFileById | null = null;
	let componentDataByCssVariable: ComponentDataByCssVariable | null = null;
	let fontDataByCssVariable: FontDataByCssVariable | null = null;

	let isBuild: boolean;
	let fontFetcher: FontFetcher | null = null;
	let fontTypeExtractor: FontTypeExtractor | null = null;

	const cleanup = () => {
		componentDataByCssVariable = null;
		fontDataByCssVariable = null;
		fontFileById = null;
		fontFetcher = null;
	};

	return {
		name: 'astro:fonts',
		config(_, { command }) {
			isBuild = command === 'build';
		},
		async buildStart() {
			const { root } = settings.config;
			// Dependencies. Once extracted to a dedicated vite plugin, those may be passed as
			// a Vite plugin option.
			const hasher = await XxhashHasher.create();
			const storage = new UnstorageFsStorage({
				// In dev, we cache fonts data in .astro so it can be easily inspected and cleared
				base: new URL(CACHE_DIR, isBuild ? settings.config.cacheDir : settings.dotAstroDir),
			});
			const systemFallbacksProvider = new RealSystemFallbacksProvider();
			fontFetcher = new CachedFontFetcher({ storage, fetch, readFile });
			const cssRenderer = new MinifiableCssRenderer({ minify: isBuild });
			const fontMetricsResolver = new CapsizeFontMetricsResolver({ fontFetcher, cssRenderer });
			fontTypeExtractor = new NodeFontTypeExtractor();
			const stringMatcher = new LevenshteinStringMatcher();
			const urlResolver = isBuild
				? new BuildUrlResolver({
						base: baseUrl,
						assetsPrefix: settings.config.build.assetsPrefix,
						searchParams: settings.adapter?.client?.assetQueryParams ?? new URLSearchParams(),
					})
				: new DevUrlResolver({
						base: baseUrl,
						searchParams: settings.adapter?.client?.assetQueryParams ?? new URLSearchParams(),
					});
			const contentResolver = new FsFontFileContentResolver({
				readFileSync: (path) => readFileSync(path, 'utf-8'),
			});
			const fontFileIdGenerator = isBuild
				? new BuildFontFileIdGenerator({
						hasher,
						contentResolver,
					})
				: new DevFontFileIdGenerator({
						hasher,
						contentResolver,
					});
			const { bold } = colors;
			const defaults = DEFAULTS;
			const resolvedFamilies =
				settings.config.fonts?.map((family) =>
					resolveFamily({ family: family as FontFamily, hasher }),
				) ?? [];
			const { fontFamilyAssets, fontFileById: _fontFileById } = await computeFontFamiliesAssets({
				resolvedFamilies,
				defaults,
				bold,
				logger,
				stringMatcher,
				fontResolver: await UnifontFontResolver.create({
					families: resolvedFamilies,

Domain

Subdomains

Frequently Asked Questions

What does fontsPlugin() do?
fontsPlugin() is a function in the astro codebase, defined in packages/astro/src/assets/fonts/vite-plugin-fonts.ts.
Where is fontsPlugin() defined?
fontsPlugin() is defined in packages/astro/src/assets/fonts/vite-plugin-fonts.ts at line 60.

Analyze Your Own Codebase

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

Try Supermodel Free