Skip to content

Commit 8f40d28

Browse files
committed
fix: handle multiple sources and only try reading woff/woff2/ttf files
1 parent 386211c commit 8f40d28

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

src/css.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ import {
77
whitespace,
88
} from 'magic-regexp'
99

10-
export const parseFontFace = (
10+
export function* parseFontFace(
1111
css: string
12-
): { family?: string; source?: string } => {
12+
): Generator<{ family?: string; source?: string }> {
1313
const fontFamily = css.match(FAMILY_RE)?.groups.fontFamily
14-
const src = css.match(SOURCE_RE)?.groups.src
15-
1614
const family = withoutQuotes(fontFamily?.split(',')[0] || '')
17-
const source = withoutQuotes(
18-
src
19-
?.split(',')
20-
.map(source => source.match(URL_RE)?.groups.url)
21-
.filter(Boolean)[0] || ''
22-
)
2315

24-
return { family, source }
16+
for (const match of css.matchAll(SOURCE_RE)) {
17+
const sources = match.groups.src?.split(',') || []
18+
for (const entry of sources) {
19+
for (const url of entry.matchAll(URL_RE)) {
20+
const source = withoutQuotes(url.groups?.url || '')
21+
if (source) {
22+
yield { family, source }
23+
}
24+
}
25+
}
26+
}
2527
}
2628

2729
export const generateOverrideName = (name: string) => {
@@ -91,9 +93,11 @@ const FAMILY_RE = createRegExp(
9193
const SOURCE_RE = createRegExp(
9294
exactly('src:')
9395
.and(whitespace.optionally())
94-
.and(charNotIn(';}').times.any().as('src'))
96+
.and(charNotIn(';}').times.any().as('src')),
97+
['g']
9598
)
9699

97100
const URL_RE = createRegExp(
98-
exactly('url(').and(charNotIn(')').times.any().as('url')).and(')')
101+
exactly('url(').and(charNotIn(')').times.any().as('url')).and(')'),
102+
['g']
99103
)

src/transform.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ interface FontaineTransformOptions {
2222
sourcemap?: boolean
2323
}
2424

25+
const supportedExtensions = ['woff2', 'woff', 'ttf']
26+
2527
export const FontaineTransform = createUnplugin(
2628
(options: FontaineTransformOptions) => {
2729
const cssContext = (options.css = options.css || {})
@@ -54,20 +56,23 @@ export const FontaineTransform = createUnplugin(
5456

5557
faceRanges.push([match.index, match.index + matchContent.length])
5658

57-
const { family, source } = parseFontFace(matchContent)
58-
if (!family) continue
59-
60-
const metrics =
61-
(await getMetricsForFamily(family)) ||
62-
(source && (await readMetricsFromId(source, id).catch(() => null)))
63-
64-
if (metrics) {
65-
const fontFace = generateFontFace(metrics, {
66-
name: overrideName(family),
67-
fallbacks: options.fallbacks,
68-
})
69-
cssContext.value += fontFace
70-
s.appendLeft(match.index, fontFace)
59+
for (const { family, source } of parseFontFace(matchContent)) {
60+
if (!family) continue
61+
if (!supportedExtensions.some(e => source?.endsWith(e))) continue
62+
63+
const metrics =
64+
(await getMetricsForFamily(family)) ||
65+
(source &&
66+
(await readMetricsFromId(source, id).catch(() => null)))
67+
68+
if (metrics) {
69+
const fontFace = generateFontFace(metrics, {
70+
name: overrideName(family),
71+
fallbacks: options.fallbacks,
72+
})
73+
cssContext.value += fontFace
74+
s.appendLeft(match.index, fontFace)
75+
}
7176
}
7277
}
7378

0 commit comments

Comments
 (0)