Skip to content

Commit

Permalink
fix: don't throw errors, but continue iterating over providers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 9, 2024
1 parent 0e35cda commit de3d126
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,13 @@ export default defineNuxtModule<ModuleOptions>({
if (provider.resolveFontFaces) {
const result = await provider.resolveFontFaces(fontFamily, defaults)
if (result) {
return {
fallbacks: result.fallbacks || defaults.fallbacks,
// Rewrite font source URLs to be proxied/local URLs
fonts: normalizeFontData(result.fonts),
const fonts = normalizeFontData(result.fonts)
if (fonts.length > 0) {
return {
fallbacks: result.fallbacks || defaults.fallbacks,
// Rewrite font source URLs to be proxied/local URLs
fonts,
}
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/providers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
const id = familyMap.get(family) as keyof typeof fonts
const font = fonts[id]!
const weights = variants.weights.filter(weight => font.weights.includes(Number(weight)))
if (weights.length == 0)
throw "No available font weights."
else if (variants.weights.length !== weights.length)
logger.warn(`Font weights \`${variants.weights}\` are not entirely available for \`${family}\` from \`bunny\`.`)
const styleMap = {
italic: 'i',
oblique: 'i',
normal: ''
}
const styles = new Set(variants.styles.map(i => styleMap[i]))
if (weights.length === 0 || styles.size === 0) return []

const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${w}${s}`))

const css = await fontAPI('/css', {
Expand Down
3 changes: 2 additions & 1 deletion src/providers/fontshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
numbers.push(style.weight.number)
}

if (numbers.length == 0) throw "No available font weights."
if (numbers.length === 0) return []

const css = await fontAPI(`/css?f[]=${font.slug + '@' + numbers.join(',')}`)

// TODO: support subsets and axes
Expand Down
8 changes: 1 addition & 7 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
? [`${variableWeight.min}..${variableWeight.max}`]
: variants.weights.filter(weight => String(weight) in font.fonts)

if (weights.length == 0)
throw "No available font weights."
else if (
(variableWeight && !(variants.weights.every(weight => Number(weight) >= variableWeight.min && Number(weight) <= variableWeight.max))) ||
(!variableWeight && variants.weights.length !== weights.length)
)
logger.warn(`Font weights \`${variants.weights}\` are not entirely available for \`${family}\` from \`google\`.`)
if (weights.length === 0 || styles.length === 0) return []

const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${s},${w}`)).sort()

Expand Down
10 changes: 10 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ describe('parsing', () => {
})
})

describe('error handling', () => {
it('handles no font details supplied', async () => {
const plugin = FontFamilyInjectionPlugin({
dev: true,
resolveFontFace: () => ({ fonts: [] })
}).raw({}, { framework: 'vite' }) as any
expect(await plugin.transform(`:root { font-family: 'Poppins', 'Arial', sans-serif }`).then((r: any) => r?.code)).toMatchInlineSnapshot(`undefined`)
})
})

const slugify = (str: string) => str.toLowerCase().replace(/[^\d\w]/g, '-')
async function transform (css: string) {
const plugin = FontFamilyInjectionPlugin({
Expand Down

0 comments on commit de3d126

Please sign in to comment.