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

@fgilde/embed-fonts

v1.0.6

Published

Injects content of font files into stylesheets by replacing the relative url() paths with base64 encoded data.

Downloads

17

Readme

Embed-Fonts

Injects content of font files into stylesheets by replacing the relative url() paths with base64 encoded data. With this tool you can easily embed fonts or include complete remote css with embedded fonts. You can use it for example as small helper for embedding Google Fonts onto your page with as less effort as possible.

Step 1 Install package

npm install --save-dev @fgilde/embed-fonts

Step 2 Create config file

Create a json config file in your structure. The default filename is embed-fonts.config.json but you can name your file as you want. If you want to use a different filename you have to specify it with the -config parameter. example: embed-fonts -config="./yourpath/yourconfigfile.json"

Sample config

{
  "placeholders": { // Placeholders are optional
    "files": ["material-icons.css", "font-awesome.min.css"]
  },
  "embed-font-files": [
    {
      "from": "./test-styles/external/{files}",
      "to": "./test-styles/external/out_{files}"
    }
  ]
}

Step 3 Use it

The command to execute is embed-fonts

To run this script before run build in package.json file, you just add prebuild in script like this:

 "scripts": {    
    "build": "...",
    "prebuild": "npm run build-fonts",
    "build-fonts": "embed-fonts -config=\"./yourpath/yourconfigfile.json\""
  },

An example from the input stylesheet:

@font-face {
  font-family: 'Test';
  src: url(fonts/test.woff) format("woff");
  font-weight: 400;
  font-style: normal;
}

An example from the generated output stylesheet:

@font-face {
  font-family: 'Test';
  src: url("data:application/font-woff;base64,ZmlsZTgK") format("woff");
  font-weight: 400;
  font-style: normal;
}

Helpfull

Because this tool uses same config as remote-dependencies you can use the same config file for both tools. And you can use the same placeholders in both tools. In combination of both tools you can download remote css, and font files and inject them into your stylesheets with fully embedded style.

Useful use case

This tool is very useful if you want to use a css with fonts from a CDN. In this we use two local files ("material-icons.css", "font-awesome.min.css") with containing local fonts from ./test-styles/external and the Open Sans font from fonts.googleapis.com

{
  "placeholders": {
    "files": ["material-icons.css", "font-awesome.min.css"]
  },
  "embed-font-files": [
    {
      "from": "https://fonts.googleapis.com/css?family=Open+Sans:300,400,700",
      "to": "./test-styles/external/my_local_open_sans_with_embedded_fonts.css"
    },
    {
      "from": "./test-styles/external/{files}",
      "to": "./test-styles/external/out_{files}"
    }
  ]
}

You can also completely use remote files. Here we create local usable styles for each of material-components-web.min.css, font-awesome.min.css, Material+Icons adn Open+Sans This produces the 4 output files css_family_open_sans_300_400_700.css, font_awesome_min_css.css, icon_family_material_icons.css, material_components_web_min_css.css

{
  "placeholders": {
    "localDir": "./test-styles/external",
    "files": [
      "https://unpkg.com/[email protected]/dist/material-components-web.min.css",
      "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css",
      "https://fonts.googleapis.com/icon?family=Material+Icons",
      "https://fonts.googleapis.com/css?family=Open+Sans:300,400,700"
    ]
  },
  "mappings": [
    {
      "minify": true, // Set this to true to minify the output
      "from": "{files}",
      "to": "{localDir}"
    }
  ]
}

If you dont want to have a file foreach input, you can also bundle all mappings into one file.

{
  "placeholders": {
    "localDir": "./test-styles/external",
    "files": [
      "https://unpkg.com/[email protected]/dist/material-components-web.min.css",
      "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css",
      "https://fonts.googleapis.com/icon?family=Material+Icons",
      "https://fonts.googleapis.com/css?family=Open+Sans:300,400,700"
    ]
  },
  "bundledOutputFile" : "{localDir}/_all_bundle.css", // bundle all to one file
  "mappings": [
    {
      "minify": true, // Set this to true to minify the output
      "from": "{files}"
      // dont provide a to, so the output will be bundled into the bundledOutputFile only
    }
  ]
}

If you like this tool, please star it on Github and share it with your friends

If not, you can give a star anyway and let me know what I can improve to make it better for you.

Links

Github Repository | NPM Package