Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn when font weights are not available #35

Merged
merged 5 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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