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

webpack-font-preload-plugin-fixed

v2.0.0-beta.0

Published

A webpack plugin to allow preloading or prefetching of fonts.

Downloads

743

Readme

npm

webpack-font-preload-plugin

A webpack plugin to allow preloading or prefetching of fonts.

Introduction

The preload value of the <link> element's rel attribute lets you declare fetch requests in the HTML's <head>, specifying resources that your page will need very soon, which you want to start loading early in the page lifecycle, before browsers' main rendering machinery kicks in. This ensures they are available earlier and are less likely to block the page's render, improving performance.

This plugin specifically targets fonts used with the application which are bundled using webpack. The plugin would add <link> tags in the begining of <head> of your html:

<link rel="preload" href="/font1.woff" as="font" crossorigin />
<link rel="preload" href="/font2.woff" as="font" crossorigin />

Getting Started

To begin, you'll need to install webpack-font-preload-plugin:

$ npm install webpack-font-preload-plugin --save-dev

Then add the plugin to your webpack config. For example:

webpack.config.js

const FontPreloadPlugin = require("webpack-font-preload-plugin");

module.exports = {
  plugins: [new FontPreloadPlugin()],
};

And run webpack via your preferred method.

Options

index

Type: string Default: index.html Optional: true

Name of the index file which needs modification.

// in your webpack.config.js
new FontPreloadPlugin({
  index: "index.html",
});

extensions

Type: string[] Default: ['woff', 'woff2', 'ttf', 'eot'] Optional: true

Default font extensions which should be used.

// in your webpack.config.js
new FontPreloadPlugin({
  extensions: ["woff", "ttf", "eot"],
});

crossorigin

Type: boolean Default: true Optional: true

Is the font request crossorigin or not.

// in your webpack.config.js
new FontPreloadPlugin({
  crossorigin: true,
});

loadType

Type: string Default: preload Optional: true

Type of load. It can be either preload or prefetch.

// in your webpack.config.js
new FontPreloadPlugin({
  loadType: "preload",
});

insertBefore

Type: string Default: head > title Optional: true

The selector for node before which the preload/prefetch links should be added.

// in your webpack.config.js
new FontPreloadPlugin({
  // Add the preload statements before any other <link> tag present in html
  insertBefore: "head > link:nth-child(1)",
});

replaceCallback

Type: Function Default: undefined Optional: true

Callback for doing custom manipulations to index.html for special use cases like templating or server side rendering. This callback would be passed an object as parameter with 2 keys:

  • indexSource: Full source string of the index.html.
  • linksAsString: <link> tags for preloading fonts as a string.

The consuming app can use this information to generate the final index.html and must return an updated string which would be used as index.html after webpack build.

// in your webpack.config.js
new FontPreloadPlugin({
  replaceCallback: ({ indexSource, linksAsString }) => {
    return indexSource.replace("{{{links}}}", linksAsString);
  },
});

filter

Type: string Default: undefined Optional: true

Expression for allowing more granular filtering of the font assets for doing a preload/prefetch. The filter is applied on the font assets selected by the extensions option. If the filter is a string, all the font assets which contain the string as part of the name are included in the preload and rest are ignored. In case filter is regex, the font asset's name is tested to match the regex for allowing preload. If you don't pass this option, all the font assets will be preloaded.

// To only preload font's which have string `app-font` as part of there name.
new FontPreloadPlugin({
  filter: "app-font",
});

// To preload fonts which start with `mui` or `app` in there name.
new FontPreloadPlugin({
  filter: /^mui|^app/i,
});

License

MIT