Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webpack-font-preload-plugin

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-font-preload-plugin

A webpack plugin to allow preloading or prefetching of fonts.

  • 1.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
120K
increased by11.27%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 17 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc