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

@font-face/node

v1.1.0

Published

Generate @font-face CSS stylesheets like a pro !!

Downloads

23

Readme

@font-face/node

Generate @font-face CSS stylesheet like a pro !!

Install

Using NPM:

npm install @font-face/node --save-dev

Motivation

  • Lets admit that it is very tedious job to manually generate @font-face CSS rules with corret URL sources for a given font files stored in local project directory.

  • In order to apply fonts across all the browsers, we may need to maintain more that one font format.

  • Online interactive web directory for fonts, like Google Fonts, auto generates CSS stylesheet with @font-face rules, making it quite popular amoung front-ends developers.

  • For locally stored font files, @font-face/node is an attempt to auto generate @font-face rules with given configurations to relieve developers from manually configuring fonts for every project.

  • @font-face/node scans input font directory (provided in configs) and produces appropriate src CSS descriptor for various font formats like eot, woff, woff2 and svg

  • @font-face/node also copies font files to build / dist directory provided in configs.

  • Utilize output font-face.css with webpack loaders to include fonts in output bundle.

Usage

@font-face/node can be invoked from CLI or API

CLI usage

Create a configuration file in the root project directory.
@font-face/node uses cosmiconfig to read a configuration which can be configured with any of the following names:

  • .font-facerc
  • .font-facerc.json
  • .font-facerc.yaml
  • .font-facerc.yml
  • .font-facerc.js
  • font-face.config.js
  • fontFace in package.json

And then simply execute:

$ font-face

OR using NPX command:

$ npx font-face

Example

Lets assume we have following directory structure:

- Root
    * font-face.config.js
    * package.json
    - node_modules
    - src
        - fonts
            * Roboto-Light.ttf
            * Roboto-Light.woff2
            * Roboto-Medium.ttf
            * Roboto-Medium.woff2
            * Roboto-Regular.ttf
            * Roboto-Regular.woff2

- indicates directory
* indicates file

We have defined configurations as follows:

// font-face.config.js

module.exports = {
    input: {
        dir: './src/fonts',
    },
    output: {
        dir: './dist',
        resourceDir: './dist/fontResources',
        cssFileName: 'font-face.css',
    },
    fonts: [
        {
            name: 'Roboto',
            weight: 200,
            style: 'normal',
            file: 'Roboto-Light',
        },
        {
            name: 'Roboto',
            weight: 400,
            style: 'normal',
            file: 'Roboto-Regular',
        },
        {
            name: 'Roboto',
            weight: 600,
            style: 'bold',
            file: 'Roboto-Medium',
        },
    ],
};

Upon executing font-face command we get following directory structure:

- Root
    * font-face.config.js
    * package.json
    - node_modules
    - dist
        * font-face.css
        - fontResources
            * Roboto-Light.ttf
            * Roboto-Light.woff2
            * Roboto-Medium.ttf
            * Roboto-Medium.woff2
            * Roboto-Regular.ttf
            * Roboto-Regular.woff2
    - src
        - fonts
            * Roboto-Light.ttf
            * Roboto-Light.woff2
            * Roboto-Medium.ttf
            * Roboto-Medium.woff2
            * Roboto-Regular.ttf
            * Roboto-Regular.woff2

Contents of font-face.css:

    @font-face { 
        font-family: 'Roboto'; 
        font-weight: 200; 
        font-style: normal; 
        src: url('fonts/Roboto-Light.woff2') format('woff2') , 
            url('fonts/Roboto-Light.ttf') format('truetype'); 
    }
    @font-face { 
        font-family: 'Roboto'; 
        font-weight: 400; 
        font-style: normal; 
        src: url('fonts/Roboto-Regular.woff2') format('woff2') , 
            url('fonts/Roboto-Regular.ttf') format('truetype'); 
    }
    @font-face { 
        font-family: 'Roboto'; 
        font-weight: 600; 
        font-style: bold; 
        src: url('fonts/Roboto-Medium.woff2') format('woff2') ,
            url('fonts/Roboto-Medium.ttf') format('truetype'); 
    }

Please note:

  1. Actual output does not include newlines, for verbosity it has been added here.
  2. Depending on your platform , ie win32 or posix, appropriate path with appropriate slash will be generated.

API usage

@font-face/node exports a function which will generate CSS file and copy fonts to dist / build folder. Above similar output could be produced using node API as shown below:

    const fontFaceNode = require('@font-face/node');

    fontFaceNode({
        input: {
            dir: './src/fonts',
        },
        output: {
            dir: './dist',
            resourceDir: './dist/fontResources',
            cssFileName: 'font-face.css',
        },
        fonts: [
            {
                name: 'Roboto',
                weight: 200,
                style: 'normal',
                file: 'Roboto-Light',
            },
            {
                name: 'Roboto',
                weight: 400,
                style: 'normal',
                file: 'Roboto-Regular',
            },
            {
                name: 'Roboto',
                weight: 600,
                style: 'bold',
                file: 'Roboto-Medium',
            },
        ],
    });

Options

input.dir

Type: String
Input font directory.

output.dir

Type: String
Output directory for generated CSS file. Though this option is redundant but this has been added to make it consistend with input.dir.

output.resourceDir

Type: String
Default: <input.dir>/resources
Fonts will be copied from input.dir to here. Usually this must be sub-directory within your build / dist directory.

output.cssFileName

Type: String
Default: font-face.css
Output CSS file name.

output.fonts

Type: List
List of font configs.
Following options are available under font config:

  • name - Mandatory font-family name.
  • weight - font-weight
  • display - font-display
  • stretch - font-stretch
  • style - font-style
  • variant - font-variant
  • unicodeRange - unicode-range
  • featureSettings - font-feature-settings
  • variationSettings - font-variation-settings

Also see

@font-face/core - Core module to create CSS @font-face rule.
@font-face/browser - Include fonts dynamically through configs in browser.