Skip to content

Commit

Permalink
fix: normalize weights before passing to resolveFontFaces (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerzl authored Mar 10, 2024
1 parent 57b5eca commit 2b14683
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default defineNuxtModule<ModuleOptions>({
// Custom merging for defaults - providing a value for any default will override module
// defaults entirely (to prevent array merging)
const normalizedDefaults = {
weights: options.defaults?.weights || defaultValues.weights,
weights: (options.defaults?.weights || defaultValues.weights).map(v => String(v)),
styles: options.defaults?.styles || defaultValues.styles,
subsets: options.defaults?.subsets || defaultValues.subsets,
fallbacks: Object.fromEntries(Object.entries(defaultValues.fallbacks).map(([key, value]) => [
Expand Down Expand Up @@ -155,7 +155,7 @@ export default defineNuxtModule<ModuleOptions>({
const defaults = { ...normalizedDefaults, fallbacks }
for (const key of ['weights', 'styles', 'subsets'] as const) {
if (override?.[key]) {
defaults[key as 'weights'] = override[key]!
defaults[key as 'weights'] = override[key]!.map(v => String(v))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/fontshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
const numbers: number[] = []
for (const style of font.styles) {
if (style.is_italic && !variants.styles.includes('italic')) { continue }
if (!variants.weights.includes(style.weight.number)) { continue }
if (!variants.weights.includes(String(style.weight.number))) { continue }
numbers.push(style.weight.number)
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
const variableWeight = font.axes.find(a => a.tag === 'wght')
const weights = variableWeight
? [`${variableWeight.min}..${variableWeight.max}`]
: variants.weights.filter(weight => String(weight) in font.fonts)
: variants.weights.filter(weight => weight in font.fonts)

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

Expand Down
11 changes: 8 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface FontFallback {
// }

export interface ResolveFontFacesOptions {
weights: Array<string | number>
weights: string[]
styles: Array<'normal' | 'italic' | 'oblique'>
// TODO: improve support and support unicode range
subsets: string[]
Expand Down Expand Up @@ -94,7 +94,7 @@ export interface FontFamilyOverrides {
// TODO:
// as?: string
}
export interface FontFamilyProviderOverride extends FontFamilyOverrides, Partial<ResolveFontFacesOptions> {
export interface FontFamilyProviderOverride extends FontFamilyOverrides, Partial<Omit<ResolveFontFacesOptions, 'weights'> & { weights: Array<string | number> }> {
/** The provider to use when resolving this font. */
provider?: FontProviderName
}
Expand Down Expand Up @@ -122,7 +122,12 @@ export interface ModuleOptions {
* ```
*/
families?: Array<FontFamilyManualOverride | FontFamilyProviderOverride>
defaults?: Partial<Omit<ResolveFontFacesOptions, 'fallbacks'>> & { fallbacks?: Partial<Record<GenericCSSFamily, string[]>> }
defaults?: Partial<{
weights: Array<string | number>
styles: ResolveFontFacesOptions['styles']
subsets: ResolveFontFacesOptions['subsets']
fallbacks?: Partial<Record<GenericCSSFamily, string[]>>
}>
providers?: {
google?: FontProvider | string | false
local?: FontProvider | string | false
Expand Down

0 comments on commit 2b14683

Please sign in to comment.