aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/preloadFontsVitePlugin.ts
blob: bc6f8eaf77354880bb823c2edea6942b84ed6a2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import micromatch from 'micromatch';
import type { PluginOption } from 'vite';

export default function preloadFontsVitePlugin(
  fontsGlob: string | string[],
): PluginOption {
  return {
    name: 'refinery-preload-fonts',
    apply: 'build',
    enforce: 'post',
    transformIndexHtml(_html, { bundle }) {
      return micromatch(Object.keys(bundle ?? {}), fontsGlob).map((href) => ({
        tag: 'link',
        attrs: {
          href,
          rel: 'preload',
          type: 'font/woff2',
          as: 'font',
          crossorigin: 'anonymous',
        },
      }));
    },
  };
}