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

fix(preset-web-fonts): fix wrong weight sort #2729

Merged
merged 4 commits into from
Jun 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/preset-web-fonts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ export { createGoogleCompatibleProvider as createGoogleProvider } from './provid
export function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta {
if (typeof meta !== 'string') {
meta.provider = resolveProvider(meta.provider || defaultProvider)
if (meta.weights)
meta.weights = [...new Set(meta.weights.map(Number).sort((a, b) => a - b))]
return meta as ResolvedWebFontMeta
}

const [name, weights = ''] = meta.split(':')

return {
name,
weights: weights.split(/[,;]\s*/).filter(Boolean),
weights: [...new Set(weights.split(/[,;]\s*/).filter(Boolean).map(Number).sort((a, b) => a - b))],
provider: resolveProvider(defaultProvider),
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/preset-web-fonts/src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ export function createGoogleCompatibleProvider(name: WebFontsProviders, host: st
return {
name,
getImportUrl(fonts) {
const sort = (weights: string[][]) => {
const firstW = weights.map(w => w[0])
const lastW = weights.map(w => w[1])
return `${firstW.join(';')};${lastW.join(';')}`
}

const strings = fonts
.map((i) => {
let name = i.name.replace(/\s+/g, '+')
if (i.weights?.length) {
name += i.italic
? `:ital,wght@${i.weights.flatMap(i => [`0,${i}`, `1,${i}`]).sort().join(';')}`
: `:wght@${i.weights.sort().join(';')}`
? `:ital,wght@${sort(i.weights.map(w => [`0,${w}`, `1,${w}`]))}`
: `:wght@${i.weights.join(';')}`
}
return `family=${name}`
}).join('&')
Expand Down
52 changes: 52 additions & 0 deletions test/preset-web-fonts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,55 @@ test('web-fonts (inline: true)', async () => {
const { css } = await uno.generate(classes)
expect(css).toContain('@font-face')
})

test('web-fonts weigth sort', async () => {
const uno = createGenerator({
presets: [
presetMini(),
presetWebFonts({
provider: 'google',
fonts: {
mono: 'Fira Mono:1000,200',
lato: [
{
name: 'Lato',
weights: ['1000', '200'],
italic: true,
},
],
},
inlineImports: false,
}),
],
})

const { css } = await uno.generate(classes)
const importUrl = css.match(/@import url\('(.*)'\)/)![1]
expect(importUrl).toMatchInlineSnapshot('"https://fonts.googleapis.com/css2?family=Fira+Mono:wght@200;1000&family=Lato:ital,wght@0,200;0,1000;1,200;1,1000&display=swap"')
})

test('web-fonts weigth deduplicate', async () => {
const uno = createGenerator({
presets: [
presetMini(),
presetWebFonts({
provider: 'google',
fonts: {
mono: 'Fira Mono:200,1000,1000',
lato: [
{
name: 'Lato',
weights: ['1000', '200', '1000', '400'],
italic: true,
},
],
},
inlineImports: false,
}),
],
})

const { css } = await uno.generate(classes)
const importUrl = css.match(/@import url\('(.*)'\)/)![1]
expect(importUrl).toMatchInlineSnapshot('"https://fonts.googleapis.com/css2?family=Fira+Mono:wght@200;1000&family=Lato:ital,wght@0,200;0,400;0,1000;1,200;1,400;1,1000&display=swap"')
})