Skip to content

Commit

Permalink
fix: warn when fonts can't be resolved because of an override (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerzl committed Mar 9, 2024
1 parent 8aba135 commit 5a4c37a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export default defineNuxtModule<ModuleOptions>({
if (override?.provider) {
if (override.provider in providers) {
const result = await providers[override.provider]!.resolveFontFaces!(fontFamily, defaults)
if (!result) {
const fonts = normalizeFontData(result?.fonts || [])
if (!fonts.length || !result) {
return logger.warn(`Could not produce font face declaration from \`${override.provider}\` for font family \`${fontFamily}\`.`)
}
return {
Expand All @@ -182,10 +183,16 @@ 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),
// Rewrite font source URLs to be proxied/local URLs
const fonts = normalizeFontData(result.fonts)
if (fonts.length > 0) {
return {
fallbacks: result.fallbacks || defaults.fallbacks,
fonts,
}
}
if (override) {
logger.warn(`Could not produce font face declaration for \`${fontFamily}\` with override.`)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/providers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
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
2 changes: 2 additions & 0 deletions src/providers/fontshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
numbers.push(style.weight.number)
}

if (numbers.length === 0) return []

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

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

if (weights.length === 0 || styles.length === 0) return []

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

let css = ''
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 5a4c37a

Please sign in to comment.