(コンテンツへのスキップリンク)

Webフォントプリセット

Google FontsFontShare から、フォント名を提供するだけでWebフォントを使用できます。

サポートされているすべてのプロバイダーを参照してください。

ソースコード

インストール

(bashコマンド)
pnpm add -D @unocss/preset-web-fonts
(bashコマンド)
yarn add -D @unocss/preset-web-fonts
(bashコマンド)
npm install -D @unocss/preset-web-fonts
uno.config.ts
(TypeScriptコード)
import presetUno from '@unocss/preset-uno'
import presetWebFonts from '@unocss/preset-web-fonts'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetUno(),
    presetWebFonts({ /* options */ }),
  ],
})

ヒント

このプリセットはunocssパッケージに含まれています。そこからインポートすることもできます。

(TypeScriptコード)
import { presetWebFonts } from 'unocss'

プロバイダー

現在サポートされているプロバイダー

情報

より多くのプロバイダーを追加するためのPRを歓迎します。🙌

カスタムフェッチ関数

フォントソースをフェッチする独自の関数を使用します。

uno.config.ts
(TypeScriptコード)
import presetUno from '@unocss/preset-uno'
import presetWebFonts from '@unocss/preset-web-fonts'
import axios from 'axios'
import ProxyAgent from 'proxy-agent'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetUno(),
    presetWebFonts({
      // use axios with an https proxy
      customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://#:7890') }).then(it => it.data),
      provider: 'google',
      fonts: {
        sans: 'Roboto',
        mono: ['Fira Code', 'Fira Mono:400,700'],
      },
    }),
  ],
})

オプション

provider

  • **タイプ:** WebFontsProviders
  • **デフォルト:** google

Webフォントのプロバイダーサービス。

(TypeScriptコード)
type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none'

fonts

  • **タイプ:** Record<string, WebFontMeta | string | (WebFontMeta | string)[]>

フォント。詳細はを参照してください。

(TypeScriptコード)
interface WebFontMeta {
  name: string
  weights?: (string | number)[]
  italic?: boolean
  /**
   * Override the provider
   * @default <matches root config>
   */
  provider?: WebFontsProviders
}

extendTheme

  • **タイプ:** boolean
  • **デフォルト:** true

テーマオブジェクトを拡張します。

themeKey

  • **タイプ:** string
  • **デフォルト:** fontFamily

テーマオブジェクトのキー。

inlineImports

  • **タイプ:** boolean
  • **デフォルト:** true

インラインCSS @import().

customFetch

  • **タイプ:** (url: string) => Promise<string>
  • **デフォルト:** undefined

フォントソースをフェッチする独自の関数を使用します。カスタムフェッチ関数を参照してください。

(TypeScriptコード)
presetWebFonts({
  provider: 'google', // default provider
  fonts: {
    // these will extend the default theme
    sans: 'Roboto',
    mono: ['Fira Code', 'Fira Mono:400,700'],
    // custom ones
    lobster: 'Lobster',
    lato: [
      {
        name: 'Lato',
        weights: ['400', '700'],
        italic: true,
      },
      {
        name: 'sans-serif',
        provider: 'none',
      },
    ],
  },
})

以下のCSSが自動的に生成されます

(cssコード)
@import url('https://fonts.googleapis.com/css2?family=Roboto&family=Fira+Code&family=Fira+Mono:wght@400;700&family=Lobster&family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap');

/* layer: default */
.font-lato {
  font-family: "Lato", sans-serif;
}
.font-lobster {
  font-family: "Lobster";
}
.font-mono {
  font-family: "Fira Code", "Fira Mono", ui-monospace, SFMono-Regular, Menlo,
    Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
.font-sans {
  font-family: "Roboto", ui-sans-serif, system-ui, -apple-system,
    BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans",
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
    "Noto Color Emoji";
}

フォントをローカルで提供する

デフォルトでは、プリセットはプロバイダーのCDNからフォントをフェッチします。フォントをローカルで提供する場合は、フォントをダウンロードし、@unocss/preset-web-fonts/local のプロセッサを使用して独自のサーバーから提供できます。

(TypeScriptコード)
import presetWebFonts from '@unocss/preset-web-fonts'
import { createLocalFontProcessor } from '@unocss/preset-web-fonts/local'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetWebFonts({
      provider: 'none',
      fonts: {
        sans: 'Roboto',
        mono: 'Fira Code',
      },
      // This will download the fonts and serve them locally
      processors: createLocalFontProcessor({
        // Directory to cache the fonts
        cacheDir: 'node_modules/.cache/unocss/fonts',

        // Directory to save the fonts assets
        fontAssetsDir: 'public/assets/fonts',

        // Base URL to serve the fonts from the client
        fontServeBaseUrl: '/assets/fonts'
      })
    }),
  ],
})

これにより、フォントアセットが public/assets/fonts にダウンロードされ、クライアントで /assets/fonts から提供されます。これを行う場合は、フォントのライセンスが再配布を許可していることを確認してください。ツールは法的問題について責任を負いません。

情報

この機能はNode.js専用であり、ブラウザでは機能しません。

MITライセンスでリリースされています。