Unifont

0.7.4 · active · verified Sun Apr 19

Unifont is a framework-agnostic JavaScript/TypeScript library designed to streamline access to font data from various Content Delivery Networks (CDNs) and font providers. It abstracts away the complexities of fetching and resolving fonts, supporting popular services like Google Fonts, Adobe Fonts, Bunny Fonts, Fontshare, and Fontsource. The library is currently at version 0.7.4 and maintains an active release cadence, frequently adding new provider features and addressing bug fixes, as seen in its recent updates like npm package resolution support and Adobe race condition handling. A key differentiator is its modular provider architecture, allowing users to integrate multiple font sources and even develop custom providers, simplifying font management and optimization across projects. It is an ESM-only package.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Unifont with multiple providers, list available fonts, and resolve specific font families with optional configurations like variable axis settings for Google Fonts.

import { createUnifont, providers } from 'unifont';

async function main() {
  // Initialize Unifont with one or more providers.
  // The Google Fonts provider is initialized without specific options here.
  const unifont = await createUnifont([
    providers.google(),
    providers.bunny(), // Adding another provider for a more robust example
  ]);

  // List all available fonts from the configured providers.
  console.log('Fetching available fonts...');
  const availableFonts = await unifont.listFonts();
  console.log('Available fonts:', availableFonts.slice(0, 5).map(f => f.name).join(', ') + '...'); // Log first 5 for brevity

  // Resolve a specific font family, e.g., 'Poppins'.
  // This will fetch the necessary font files and metadata.
  console.log('\nResolving "Poppins" font...');
  const { fonts: poppinsFonts, metrics: poppinsMetrics } = await unifont.resolveFont('Poppins');
  console.log('Resolved Poppins font files count:', poppinsFonts.length);
  // console.log('First Poppins font URL:', poppinsFonts[0]?.src); // Accessing the source URL for a resolved font

  // Example of resolving a font with specific options, like variable axis for Google Fonts
  // For Adobe Fonts, you would typically pass an ID: providers.adobe({ id: process.env.ADOBE_FONT_KIT_ID ?? '' })
  const { fonts: robotoFonts } = await unifont.resolveFont('Roboto Flex', {
    options: {
      google: {
        experimental: {
          variableAxis: {
            wght: [['100', '900']],
          },
        },
      },
    },
  });
  console.log('Resolved Roboto Flex (variable) font files count:', robotoFonts.length);
}

main().catch(console.error);

view raw JSON →