Skip to content

Commit 9cfa836

Browse files
feat: add fontsource font provider
1 parent e62f9c7 commit 9cfa836

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/loaders/custom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ export function customLoader(options: CustomFonts, root: string) {
276276
// --- Generate `<link>` tags.
277277
// --- We can not do a prefetch and a preload for the same files.
278278
if (preload && prefetch) {
279-
console.warn('vite-plugin-fonts: Prefetch and a Preload options can not be used together.')
280-
console.warn('vite-plugin-fonts: The prefetch stand for a lower priority for the resource (maybe we will need it in a future page) whereas preload is for the current page, so we can not have both.')
279+
console.warn('unplugin-fonts: Prefetch and a Preload options can not be used together.')
280+
console.warn('unplugin-fonts: The prefetch stand for a lower priority for the resource (maybe we will need it in a future page) whereas preload is for the current page, so we can not have both.')
281281
}
282282
if (preload || prefetch)
283283
tags.push(...hrefs.map(createFontFaceLink(prefetch, injectTo)))

src/loaders/fontsource.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

22
export interface FontsourceFontFamily {
33
name: string
4+
variables?: ('variable' | 'variable-italic' | 'variable-full' | 'variable-full-italic')[]
45
weights: (100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900)[]
56
styles?: ('italic' | 'normal')[]
67
subset?: string
78
}
89
export interface FontsourceFonts {
910
families: (string | FontsourceFontFamily)[]
10-
text?: string
11-
display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
11+
// text?: string
12+
// display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
1213
}
1314

1415
export function fontsourceVirtualModule(options?: FontsourceFonts) {
@@ -24,15 +25,23 @@ export function fontsourceVirtualModule(options?: FontsourceFonts) {
2425
} else {
2526
const {
2627
name,
28+
variables,
2729
weights,
2830
styles,
2931
subset
3032
} = family
3133

32-
3334
const subsetPrefix = subset ? `${subset}-` : ''
3435

35-
if (weights) {
36+
if (variables) {
37+
if (weights) {
38+
console.warn('unplugin-fonts: Variable fonts do not support weights. Ignoring weights.')
39+
}
40+
41+
for (const variable of variables) {
42+
source.push(`@import "@fontsource/${name.toLowerCase()}/${subsetPrefix}${variable}.css";`)
43+
}
44+
} else if (weights) {
3645
for (const weight of weights) {
3746
if (styles) {
3847
for (const style of styles) {

0 commit comments

Comments
 (0)