npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-webfonts

v1.0.1

Published

Generate @font-face declarations for your local font assets. πŸ’β€β™€οΈ

Downloads

46

Readme

Features

  • Programmatically generate @font-face declarations for your font assets. πŸ’β€β™€οΈ
  • Emit CSS files and <link> tags or an inline <style> tag.

Install

npm install --save-dev vite-plugin-webfonts

Use

This plugin accepts an Options object for configuration:

vite.config.js

import { defineConfig } from 'vite';
import webfontsPlugin from 'vite-plugin-webfonts';

export default defineConfig(() => ({
  plugins: [
    webfontsPlugin({
      fonts: [{
        // The `font-family` value used for each variant.
        family: 'Comic Sans',
        // Optionally prepend a local() directive to the `src` list for each
        // variant of this font family.
        local: 'Comic Sans MS',
        // Variants may specify any CSS rule that is valid in a @font-face
        // block. For idiomatic JavaScript, camel case keys will be converted to
        // kebab case and `font-` will be prefixed to rules as-needed. For
        // example, `featureSettings` will become `font-feature-settings` in
        // emitted CSS.
        variants: [{
          weight: 200,
          // Sources should be relative to config.root (typically where
          // index.html is). `src` may be a single string or an array of
          // strings. format() hints are inferred based on a file's extension.
          src: [
            'assets/comic-sans-light.woff',
            'assets/comic-sans-light.woff2'
          ]
        }, {
          weight: 400,
          src: [
            'assets/comic-sans-regular.woff'
            'assets/comic-sans-regular.woff2'
          ]
        }]
      }],
      // Optional. Outputs additional logging.
      verbose: true,
      // Optional. If false, the plugin will only inject a <style> tag rather
      // than CSS files.
      emitCss: false,
    })
  ]
}));

Assuming emitCss is enabled (default), the above configuration will produce the following output:

Development

index.html (excerpt)

<html>
  <head>
    <style>
      @font-face {
        font-family: 'Comic Sans';
        font-weight: 200;
        src: local('Comic Sans MS'),
             url(/assets/comic-sans-light.woff) format('woff'),
             url(/assets/comic-sans-light.woff2) format('woff2')
      }

      @font-face {
        font-family: 'Comic Sans';
        font-weight: 400;
        src: local('Comic Sans MS'),
             url(/assets/comic-sans-regular.woff) format('woff'),
             url(/assets/comic-sans-regular.woff2) format('woff2')
      }
    </style>
  </head>
</html>

Production

index.html (excerpt)

<html>
  <head>
    <link rel="stylesheet" href="fonts.css" />
  </head>
</html>

fonts.css

@font-face {
  font-family: 'Comic Sans';
  font-weight: 200;
  src: local('Comic Sans MS'),
       url(/assets/comic-sans-light.woff) format('woff'),
       url(/assets/comic-sans-light.woff2) format('woff2')
}

@font-face {
  font-family: 'Comic Sans';
  font-weight: 400;
  src: local('Comic Sans MS'),
       url(/assets/comic-sans-regular.woff) format('woff'),
       url(/assets/comic-sans-regular.woff2) format('woff2')
}

Note: Final asset names may vary according to your Vite configuration.

Advanced

If a large number of font files need to be loaded, this plugin includes a helper that allows you to generate font variants from a set of glob patterns.

For this example, let's assume we have the following files in our config.root directory:

assets/comic-sans.200.woff
assets/comic-sans.400.woff

Here, we have encoded the attributes we want to use in the font variant directly into the file name. We can then use this information to programmatically generate a variant descriptor for each file:

vite.config.js

import path from 'path';
import { defineConfig } from 'vite';
import webfontsPlugin from 'vite-plugin-webfonts';

export default defineConfig(() => ({
  plugins: [
    // Instead of passing an object to the plugin, pass a function that returns
    // a configuration object. This function will be invoked with a context
    // object that contains the `familyFromFiles` helper.
    webfontsPlugin(({ familyFromFiles }) => ({
      fonts: [
        familyFromFiles({
          family: 'Comic Sans',
          // `include` may be a string or array of strings to pass to `globby`.
          include: 'assets/comic-sans-*',
          // Each matched font file is then passed to a `variants` function,
          // which is responsible for returning a `FontVariant` object.
          variants: fontFile => {
            // This logic will vary based on the naming scheme used. In our
            // case, we have used a dot-delimited pattern that includes the
            // font's weight and format (extension). We do not use the first
            // segment of the file name.
            const [, weight, format] = path.basename(fontFile).split('.');

            // We can then simply return this information directly.
            return { weight, format };
          }
        })
      ]
    }))
  ]
}));

With the above setup, we don't need to update our Vite configuration when we add new font assets as long as we use a consistent naming scheme for our files.

See Also

If you use fonts from Google Fonts or TypeKit, check out vite-plugin-fonts.