ef7de2a93f
Enable setting font specific subsets in the call. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
85 lines
2.3 KiB
JavaScript
85 lines
2.3 KiB
JavaScript
const fs = require('fs/promises')
|
|
const path = require('path')
|
|
const fetch = require('node-fetch')
|
|
|
|
;(async () => {
|
|
const { familyMetadataList } = await fetch(
|
|
'https://fonts.google.com/metadata/fonts'
|
|
).then((r) => r.json())
|
|
|
|
let fontFunctions = `/**
|
|
* This is an autogenerated file by scripts/update-google-fonts.js
|
|
*/
|
|
import type { FontModule } from 'next/font'
|
|
type Display = 'auto'|'block'|'swap'|'fallback'|'optional'
|
|
type CssVariable = \`--\${string}\`
|
|
`
|
|
const fontData = {}
|
|
for (let { family, fonts, axes, subsets } of familyMetadataList) {
|
|
subsets = subsets.filter((subset) => subset !== 'menu')
|
|
const weights = new Set()
|
|
const styles = new Set()
|
|
|
|
for (const variant of Object.keys(fonts)) {
|
|
if (variant.endsWith('i')) {
|
|
styles.add('italic')
|
|
weights.add(variant.slice(0, -1))
|
|
continue
|
|
} else {
|
|
styles.add('normal')
|
|
weights.add(variant)
|
|
}
|
|
}
|
|
|
|
const hasVariableFont = axes.length > 0
|
|
|
|
let optionalAxes
|
|
if (hasVariableFont) {
|
|
weights.add('variable')
|
|
|
|
const nonWeightAxes = axes.filter(({ tag }) => tag !== 'wght')
|
|
if (nonWeightAxes.length > 0) {
|
|
optionalAxes = nonWeightAxes
|
|
}
|
|
}
|
|
|
|
fontData[family] = {
|
|
weights: [...weights],
|
|
styles: [...styles],
|
|
axes: hasVariableFont ? axes : undefined,
|
|
}
|
|
const optionalIfVariableFont = hasVariableFont ? '?' : ''
|
|
fontFunctions += `export declare function ${family.replaceAll(
|
|
' ',
|
|
'_'
|
|
)}(options${optionalIfVariableFont}: {
|
|
weight${optionalIfVariableFont}:${[...weights]
|
|
.map((weight) => `"${weight}"`)
|
|
.join('|')}
|
|
style?: ${[...styles].map((style) => `"${style}"`).join('|')}
|
|
display?:Display
|
|
variable?: CssVariable
|
|
preload?:boolean
|
|
fallback?: string[]
|
|
adjustFontFallback?: boolean
|
|
subsets?: Array<${subsets.map((subset) => `"${subset}"`).join('|')}>
|
|
${
|
|
optionalAxes
|
|
? `axes?:(${optionalAxes.map(({ tag }) => `'${tag}'`).join('|')})[]`
|
|
: ''
|
|
}
|
|
}):FontModule
|
|
`
|
|
}
|
|
|
|
await Promise.all([
|
|
fs.writeFile(
|
|
path.join(__dirname, '../packages/font/src/google/index.ts'),
|
|
fontFunctions
|
|
),
|
|
fs.writeFile(
|
|
path.join(__dirname, '../packages/font/src/google/font-data.json'),
|
|
JSON.stringify(fontData, null, 2)
|
|
),
|
|
])
|
|
})()
|