Skip to content

Plugin Fonts API reference

Source information
  • Package: @pikacss/plugin-fonts
  • Generated from the exported surface and JSDoc in packages/plugin-fonts/src/index.ts.
  • Source files: packages/plugin-fonts/src/index.ts, packages/plugin-fonts/src/providers.ts

Package summary

Web font integration

Use Fonts plugin when you need conceptual usage guidance instead of exact symbol lookup.

Functions

defineFontsProvider(provider)

Identity helper that defines a font provider with full type inference.

ParameterTypeDescription
providerTThe provider definition object.

Returns: T - The same provider definition, typed as T.

Remarks:

Provides type safety without any runtime transformation.

ts
const myProvider = defineFontsProvider({
  buildImportUrls(fonts, ctx) {
    return fonts.map(f => `https://cdn.example.com/css?family=${f.name}`)
  },
})


fonts()

Creates the fonts engine plugin for web-font integration.

Returns: EnginePlugin - An engine plugin that registers font imports, @font-face preflights, CSS variables, and font-<token> shortcuts.

Remarks:

Reads its configuration from the fonts key in the engine config. Supports Google Fonts, Bunny Fonts, Fontshare, Coollabs, and custom providers.

ts
import { fonts } from '@pikacss/plugin-fonts'

export default defineEngineConfig({
  plugins: [fonts()],
  fonts: {
    provider: 'google',
    fonts: { sans: 'Inter:400,600,700' },
  },
})


Constants

builtInFontsProviders

Registry mapping each built-in provider name to its implementation.

Remarks:

Includes Google Fonts, Bunny Fonts, Fontshare, Coollabs (self-hosted Google proxy), and none (no-op).

ts
const urls = builtInFontsProviders.google.buildImportUrls?.(fonts, ctx)


Types

FontFaceDefinition

Describes a raw CSS @font-face declaration injected as a preflight.

PropertyTypeDescriptionDefault
fontFamilystringThe font-family name for the @font-face rule.
srcstring | string[]One or more src descriptors (e.g. url(...) expressions).
fontDisplay?stringCSS font-display descriptor for this face.undefined
fontWeight?string | numberCSS font-weight descriptor, such as '400' or '100 900' for variable fonts.undefined
fontStyle?stringCSS font-style descriptor (e.g. 'normal', 'italic').undefined
fontStretch?stringCSS font-stretch descriptor (e.g. 'condensed', '75% 125%').undefined
unicodeRange?string | string[]CSS unicode-range descriptor to limit the character set.undefined

Remarks:

Each definition produces one @font-face block. Use this for self-hosted fonts or fonts that do not come from a provider URL.

ts
const face: FontFaceDefinition = {
  fontFamily: 'MyFont',
  src: 'url(/fonts/MyFont.woff2) format("woff2")',
  fontWeight: '400 700',
  fontDisplay: 'swap',
}


FontFamilyEntry

A font entry — either a shorthand string or a full metadata object.

Remarks:

Strings are parsed as 'Name' or 'Name:weight1,weight2'. Use FontMeta when you need italic or provider overrides.

ts
const simple: FontFamilyEntry = 'Roboto'
const withWeights: FontFamilyEntry = 'Roboto:400,700'
const detailed: FontFamilyEntry = { name: 'Roboto', weights: [400, 700], italic: true }


FontMeta

Detailed metadata for a font family entry.

PropertyTypeDescriptionDefault
namestringFont family name as expected by the provider (e.g. 'Inter').
weights?Array<string | number>Font weight values to load from the provider.[]
italic?booleanWhether to include italic variants for the requested weights.false
provider?FontsProviderProvider override for this font, taking precedence over the global provider option.undefined
providerOptions?FontsProviderOptionsProvider-specific options for this font, merged with global provider options.undefined

Remarks:

Use this form instead of a plain string when you need to specify weights, italic variants, or a per-font provider override.

ts
const font: FontMeta = {
  name: 'Inter',
  weights: [400, 600, 700],
  italic: true,
  provider: 'bunny',
}


FontsPluginOptions

Configuration options for the fonts plugin.

PropertyTypeDescriptionDefault
provider?FontsProviderDefault font provider used for all font entries that do not specify their own.'google'
fonts?Record<string, FontFamilyEntry | FontFamilyEntry[]>Font families grouped by shortcut token. Each token produces a font-<token> CSS shortcut.{}
families?Record<string, string | string[]>Raw font-family CSS stacks grouped by shortcut token; no provider loading is performed.{}
imports?string | string[]Additional CSS @import url(...) rules injected before provider-generated imports.[]
faces?FontFaceDefinition[]Custom @font-face definitions injected as preflight CSS.[]
display?stringCSS font-display value applied to all provider-generated imports.'swap'
providers?Record<string, FontsProviderDefinition>Custom font provider implementations keyed by provider name.{}
providerOptions?Record<string, FontsProviderOptions>Provider-level options keyed by provider name, forwarded to buildImportUrls.{}

Remarks:

Set these under the fonts key in your engine config. The plugin resolves font entries, builds provider import URLs, generates @font-face rules, and registers font-<token> shortcuts.

ts
const options: FontsPluginOptions = {
  provider: 'google',
  display: 'swap',
  fonts: {
    sans: 'Inter:400,600,700',
    mono: 'Fira Code:400,700',
  },
}


FontsProvider

Identifier for a font provider — either a built-in name or a custom string.

Remarks:

Custom strings must have a matching entry in FontsPluginOptions.providers to take effect.

ts
const builtin: FontsProvider = 'bunny'
const custom: FontsProvider = 'my-cdn'


FontsProviderContext

Runtime context passed to a provider's buildImportUrls callback.

PropertyTypeDescriptionDefault
providerFontsProviderThe provider identifier this context belongs to.
displaystringCSS font-display value applied to all fonts from this provider.
optionsFontsProviderOptionsProvider-level options merged from providerOptions configuration.

Remarks:

Assembled from the resolved plugin configuration during engine setup.

ts
const ctx: FontsProviderContext = {
  provider: 'google',
  display: 'swap',
  options: { text: 'Hello' },
}


FontsProviderDefinition

Blueprint for a font provider that converts font entries into CSS import URLs.

PropertyTypeDescriptionDefault
buildImportUrls?( fonts: readonly FontsProviderFontEntry[], context: FontsProviderContext, ) => string | string[] | null | undefinedGenerates one or more CSS import URLs for the given font entries.undefined

Remarks:

Register custom providers via FontsPluginOptions.providers using defineFontsProvider.

ts
const myProvider: FontsProviderDefinition = {
  buildImportUrls(fonts, ctx) {
    return fonts.map(f => `https://my-cdn.com/css?family=${f.name}`)
  },
}


FontsProviderFontEntry

Describes a single font family to be loaded by a provider.

PropertyTypeDescriptionDefault
namestringFont family name as recognized by the provider (e.g. 'Roboto').
weightsstring[]Font weight values to load (e.g. ['400', '700']).
italicbooleanWhether to include italic variants for the requested weights.
options?FontsProviderOptionsPer-font provider options that override global provider options.undefined

Remarks:

Constructed internally by normalizing user-supplied font entries. The provider uses these to build CSS import URLs.

ts
const entry: FontsProviderFontEntry = {
  name: 'Roboto',
  weights: ['400', '700'],
  italic: true,
}


FontsProviderOptions

Key-value map of provider-specific options passed alongside font requests.

Remarks:

Unsupported option keys are silently ignored by each provider's URL builder.

ts
const opts: FontsProviderOptions = { text: 'Hello' }


Module augmentations

EngineConfig (@pikacss/core)

PropertyTypeDescriptionDefault
fonts?FontsPluginOptionsConfiguration for the fonts plugin.undefined