optimize-fallbacks.ts — astro Source File
Architecture documentation for optimize-fallbacks.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR deca7729_9709_1830_1d5e_db8f337779b7["optimize-fallbacks.ts"] e92fda44_4a5d_b83b_1a45_958cdae6c91b["./definitions.js"] deca7729_9709_1830_1d5e_db8f337779b7 --> e92fda44_4a5d_b83b_1a45_958cdae6c91b ea387312_d0a2_bb52_dafc_3872f7307f95["../../src/assets/fonts/types.js"] deca7729_9709_1830_1d5e_db8f337779b7 --> ea387312_d0a2_bb52_dafc_3872f7307f95 aca757ac_dcac_a793_a9c5_394cd0d7d313["../utils.js"] deca7729_9709_1830_1d5e_db8f337779b7 --> aca757ac_dcac_a793_a9c5_394cd0d7d313 07866fa4_1b50_10c2_1b6b_5b1f385406b5["unifont"] deca7729_9709_1830_1d5e_db8f337779b7 --> 07866fa4_1b50_10c2_1b6b_5b1f385406b5 style deca7729_9709_1830_1d5e_db8f337779b7 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type * as unifont from 'unifont';
import type { FontMetricsResolver, SystemFallbacksProvider } from '../definitions.js';
import type { FontFileData, ResolvedFontFamily } from '../types.js';
import { isGenericFontFamily, unifontFontFaceDataToProperties } from '../utils.js';
export interface CollectedFontForMetrics extends FontFileData {
data: Partial<unifont.FontFaceData>;
}
export async function optimizeFallbacks({
family,
fallbacks: _fallbacks,
collectedFonts,
systemFallbacksProvider,
fontMetricsResolver,
}: {
family: Pick<ResolvedFontFamily, 'name' | 'uniqueName'>;
fallbacks: Array<string>;
collectedFonts: Array<CollectedFontForMetrics>;
systemFallbacksProvider: SystemFallbacksProvider;
fontMetricsResolver: FontMetricsResolver;
}): Promise<null | {
css: string;
fallbacks: Array<string>;
}> {
// We avoid mutating the original array
let fallbacks = [..._fallbacks];
if (fallbacks.length === 0 || collectedFonts.length === 0) {
return null;
}
// The last element of the fallbacks is usually a generic family name (eg. serif)
const lastFallback = fallbacks[fallbacks.length - 1];
// If it's not a generic family name, we can't infer local fonts to be used as fallbacks
if (!isGenericFontFamily(lastFallback)) {
return null;
}
// If it's a generic family name, we get the associated local fonts (eg. Arial)
const localFonts = systemFallbacksProvider.getLocalFonts(lastFallback);
// Some generic families do not have associated local fonts so we abort early
if (!localFonts || localFonts.length === 0) {
return null;
}
// If the family is already a system font, no need to generate fallbacks
if (localFonts.includes(family.name)) {
return null;
}
const localFontsMappings = localFonts.map((font) => ({
font,
// We mustn't wrap in quote because that's handled by the CSS renderer
name: `${family.uniqueName} fallback: ${font}`,
}));
// We prepend the fallbacks with the local fonts and we dedupe in case a local font is already provided
fallbacks = [...localFontsMappings.map((m) => m.name), ...fallbacks];
let css = '';
for (const { font, name } of localFontsMappings) {
for (const collected of collectedFonts) {
// We generate a fallback for each font collected, which is per weight and style
css += fontMetricsResolver.generateFontFace({
metrics: await fontMetricsResolver.getMetrics(family.name, collected),
fallbackMetrics: systemFallbacksProvider.getMetricsForLocalFont(font),
font,
name,
properties: unifontFontFaceDataToProperties(collected.data),
});
}
}
return { css, fallbacks };
}
Domain
Subdomains
Functions
Types
Dependencies
- ../../src/assets/fonts/types.js
- ../utils.js
- ./definitions.js
- unifont
Source
Frequently Asked Questions
What does optimize-fallbacks.ts do?
optimize-fallbacks.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in optimize-fallbacks.ts?
optimize-fallbacks.ts defines 1 function(s): optimizeFallbacks.
What does optimize-fallbacks.ts depend on?
optimize-fallbacks.ts imports 4 module(s): ../../src/assets/fonts/types.js, ../utils.js, ./definitions.js, unifont.
Where is optimize-fallbacks.ts in the architecture?
optimize-fallbacks.ts is located at packages/astro/src/assets/fonts/core/optimize-fallbacks.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/fonts/core).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free