import { createUnifont } from "unifont"; import { Storage } from "unstorage"; import { Plugin } from "vite"; //#region src/types.d.ts /** * Font weight specification - can be a single weight or a range for variable fonts */ type FontWeight = number | `${number} ${number}` | "normal" | "bold"; /** * Font style specification */ type FontStyle = "normal" | "italic" | "oblique"; /** * Font subset specification */ type FontSubset = "latin" | "latin-ext" | "cyrillic" | "cyrillic-ext" | "greek" | "greek-ext" | "vietnamese" | (string & {}); /** * Configuration for a specific font family */ interface FontFamilyConfig { /** Font weights to include */ weights?: FontWeight[]; /** Font styles to include */ styles?: FontStyle[]; /** Unicode subsets to include */ subsets?: FontSubset[]; /** Specific provider to use for this font */ provider?: "bunny" | "fontsource" | "local" | "google"; /** Font display strategy */ display?: "auto" | "block" | "swap" | "fallback" | "optional"; /** Fallback fonts for generating metrics */ fallbacks?: string[]; } /** * Provider-specific configuration */ interface ProvidersConfig { /** Enable Bunny Fonts provider */ bunny?: boolean; /** Enable Fontsource provider */ fontsource?: boolean; /** Enable Google Fonts provider */ google?: boolean; /** Local fonts configuration */ local?: boolean | { /** Paths to scan for local font files */paths?: string[]; }; } /** * Default font options applied to all fonts */ interface FontDefaults { /** Default font weights */ weights?: FontWeight[]; /** Default font styles */ styles?: FontStyle[]; /** Default unicode subsets */ subsets?: FontSubset[]; /** Default font display strategy */ display?: "auto" | "block" | "swap" | "fallback" | "optional"; } /** * Main plugin options */ interface ViteFontsOptions { /** * Default font options applied when not specified per-font * @default { weights: [400, 700], styles: ['normal'], subsets: ['latin'], display: 'swap' } */ defaults?: FontDefaults; /** * Explicitly declare fonts and their options * Key is the font family name, value is config or `true` for defaults */ families?: Record; /** * Provider configuration * @default { bunny: true, fontsource: true, local: true } */ providers?: ProvidersConfig; /** * Generate font fallback metrics to reduce CLS * @default true */ fallbacks?: boolean; /** * Enable font caching * @default true */ cache?: boolean; /** * Cache directory path * @default 'node_modules/.cache/vite-fonts' */ cacheDir?: string; /** * Assets subfolder for font files in the build output * @default 'fonts' */ assetsDir?: string; /** * Inject fonts as inline