cato / webpack-font-preload-plugin

A webpack plugin to allow preloading or prefetch of fonts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Name of the index file which needs modification.

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

extensions

Type: Array Default: ['woff', 'ttf', 'eot']

Default font extensions which should be used.

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

crossorigin

Type: Boolean Default: true

Is the font request crossorigin or not.

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

loadType

Type: String Default: preload

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

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)',
});

License

MIT

About

A webpack plugin to allow preloading or prefetch of fonts.

License:MIT License


Languages

Language:JavaScript 100.0%