Unverified Commit 12641968 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Add next font package (#40227)

For some context:
https://vercel.slack.com/archives/CGU8HUTUH/p1662124179102509

Adds `@next/font` package.

`scripts/update-google-fonts.js` generates functions and metadata for
all available google fonts. The metadata is used in `loader.ts` to
validate font options (from #40221). It then fetches the CSS from google
fonts, downloads the font files and emits them as static assets.

The actual integration with `packages/next` and integration tests
depends on #40221, will follow up with new PR.
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 97b31873
...@@ -63,6 +63,7 @@ ...@@ -63,6 +63,7 @@
"@next/bundle-analyzer": "workspace:*", "@next/bundle-analyzer": "workspace:*",
"@next/env": "workspace:*", "@next/env": "workspace:*",
"@next/eslint-plugin-next": "workspace:*", "@next/eslint-plugin-next": "workspace:*",
"@next/font": "workspace:*",
"@next/mdx": "workspace:*", "@next/mdx": "workspace:*",
"@next/plugin-storybook": "workspace:*", "@next/plugin-storybook": "workspace:*",
"@next/polyfill-module": "workspace:*", "@next/polyfill-module": "workspace:*",
......
# `@next/font`
Experimental `@next/font` package
export * from '../dist/google'
throw new Error('@next/font/google is not correctly setup')
export { default } from '../dist/google/loader'
module.exports = require('../dist/google/loader')
{
"name": "@next/font",
"version": "12.3.2-canary.0",
"repository": {
"url": "vercel/next.js",
"directory": "packages/font"
},
"files": [
"dist",
"google"
],
"license": "MIT",
"scripts": {
"build": "rm -rf dist && tsc -d -p tsconfig.json",
"prepublishOnly": "cd ../../ && turbo run build",
"dev": "tsc -d -w -p tsconfig.json",
"typescript": "tsec --noEmit -p tsconfig.json"
}
}
This diff is collapsed.
This diff is collapsed.
// @ts-ignore
import fetch from 'next/dist/compiled/node-fetch'
// @ts-ignore
import { calculateOverrideCSS } from 'next/dist/server/font-utils'
import {
fetchCSSFromGoogleFonts,
getFontAxes,
getUrl,
validateData,
} from './utils'
type FontLoaderOptions = {
functionName: string
data: any[]
config: any
emitFontFile: (content: Buffer, ext: string, preload: boolean) => string
}
export default async function downloadGoogleFonts({
functionName,
data,
config,
emitFontFile,
}: FontLoaderOptions) {
if (!config?.subsets) {
throw new Error(
'Please specify subsets for `@next/font/google` in your `next.config.js`'
)
}
const {
fontFamily,
weight,
style,
display,
preload,
selectedVariableAxes,
fallback,
adjustFontFallback,
} = validateData(functionName, data)
const fontAxes = getFontAxes(fontFamily, weight, style, selectedVariableAxes)
const url = getUrl(fontFamily, fontAxes, display)
const fontFaceDeclarations = await fetchCSSFromGoogleFonts(url, fontFamily)
// Find font files to download
const fontFiles: Array<{
googleFontFileUrl: string
preloadFontFile: boolean
}> = []
let currentSubset = ''
for (const line of fontFaceDeclarations.split('\n')) {
// Each @font-face has the subset above it in a comment
const newSubset = /\/\* (.+?) \*\//.exec(line)?.[1]
if (newSubset) {
currentSubset = newSubset
} else {
const googleFontFileUrl = /src: url\((.+?)\)/.exec(line)?.[1]
if (googleFontFileUrl) {
fontFiles.push({
googleFontFileUrl,
preloadFontFile: !!preload && config.subsets.includes(currentSubset),
})
}
}
}
// Download font files
const downloadedFiles = await Promise.all(
fontFiles.map(async ({ googleFontFileUrl, preloadFontFile }) => {
let fontFileBuffer: Buffer
if (process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES) {
fontFileBuffer = Buffer.from(googleFontFileUrl)
} else {
const arrayBuffer = await fetch(googleFontFileUrl).then((r: any) =>
r.arrayBuffer()
)
fontFileBuffer = Buffer.from(arrayBuffer)
}
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(googleFontFileUrl)![1]
// Emit font file to .next/static/fonts
const selfHostedFileUrl = emitFontFile(
fontFileBuffer,
ext,
preloadFontFile
)
return {
googleFontFileUrl,
selfHostedFileUrl,
}
})
)
// Replace @font-face sources with self-hosted files
let updatedCssResponse = fontFaceDeclarations
for (const { googleFontFileUrl, selfHostedFileUrl } of downloadedFiles) {
updatedCssResponse = updatedCssResponse.replace(
googleFontFileUrl,
selfHostedFileUrl
)
}
// Add fallback font
if (adjustFontFallback) {
try {
updatedCssResponse += calculateOverrideCSS(
fontFamily,
require('next/dist/server/google-font-metrics.json')
)
} catch (e) {
console.log('Error getting font override values - ', e)
}
}
return {
css: updatedCssResponse,
fallbackFonts: fallback,
}
}
// @ts-ignore
import fetch from 'next/dist/compiled/node-fetch'
import fontData from './font-data.json'
const allowedDisplayValues = ['auto', 'block', 'swap', 'fallback', 'optional']
const formatValues = (values: string[]) =>
values.map((val) => `\`${val}\``).join(', ')
type FontOptions = {
fontFamily: string
weight: string
style: string
display: string
preload: boolean
selectedVariableAxes?: string[]
fallback?: string[]
adjustFontFallback: boolean
}
export function validateData(functionName: string, data: any): FontOptions {
let {
variant,
display = 'optional',
preload = true,
axes,
fallback,
adjustFontFallback = true,
} = data[0] || ({} as any)
if (functionName === '') {
throw new Error(`@next/font/google has no default export`)
}
const fontFamily = functionName.replace(/_/g, ' ')
const fontVariants = (fontData as any)[fontFamily]?.variants
if (!fontVariants) {
throw new Error(`Unknown font \`${fontFamily}\``)
}
// Set variable as default, throw if not available
if (!variant) {
if (fontVariants.includes('variable')) {
variant = 'variable'
} else {
throw new Error(
`Missing variant for font \`${fontFamily}\`.\nAvailable variants: ${formatValues(
fontVariants
)}`
)
}
}
if (!fontVariants.includes(variant)) {
throw new Error(
`Unknown variant \`${variant}\` for font \`${fontFamily}\`.\nAvailable variants: ${formatValues(
fontVariants
)}`
)
}
if (!allowedDisplayValues.includes(display)) {
throw new Error(
`Invalid display value \`${display}\` for font \`${fontFamily}\`.\nAvailable display values: ${formatValues(
allowedDisplayValues
)}`
)
}
const [weight, style] = variant.split('-')
if (weight !== 'variable' && axes) {
throw new Error('Axes can only be defined for variable fonts')
}
return {
fontFamily,
weight,
style,
display,
preload,
selectedVariableAxes: axes,
fallback,
adjustFontFallback,
}
}
export function getUrl(
fontFamily: string,
axes: [string, string][],
display: string
) {
// Google api requires the axes to be sorted, starting with lowercase words
axes.sort(([a], [b]) => {
const aIsLowercase = a.charCodeAt(0) > 96
const bIsLowercase = b.charCodeAt(0) > 96
if (aIsLowercase && !bIsLowercase) return -1
if (bIsLowercase && !aIsLowercase) return 1
return a > b ? 1 : -1
})
return `https://fonts.googleapis.com/css2?family=${fontFamily.replace(
/ /g,
'+'
)}:${axes.map(([key]) => key).join(',')}@${axes
.map(([, val]) => val)
.join(',')}&display=${display}`
}
export async function fetchCSSFromGoogleFonts(url: string, fontFamily: string) {
let mockedResponse: string | undefined
if (process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES) {
const mockFile = require(process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES)
mockedResponse = mockFile[url]
if (!mockedResponse) {
throw new Error('Missing mocked response for URL: ' + url)
}
}
let cssResponse
if (mockedResponse) {
cssResponse = mockedResponse
} else {
const res = await fetch(url, {
headers: {
// The file format is based off of the user agent, make sure woff2 files are fetched
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
},
})
if (!res.ok) {
throw new Error(`Failed to fetch font \`${fontFamily}\`.\nURL: ${url}`)
}
cssResponse = await res.text()
}
return cssResponse
}
export function getFontAxes(
fontFamily: string,
weight: string,
style: string,
selectedVariableAxes?: string[]
): [string, string][] {
const allAxes: Array<{ tag: string; min: number; max: number }> = (
fontData as any
)[fontFamily].axes
const italicAxis: [string, string][] =
style === 'italic' ? [['ital', '1']] : []
if (weight === 'variable') {
if (selectedVariableAxes) {
const defineAbleAxes: string[] = allAxes
.map(({ tag }) => tag)
.filter((tag) => tag !== 'wght')
if (defineAbleAxes.length === 0) {
throw new Error(`Font \`${fontFamily}\` has no definable \`axes\``)
}
if (!Array.isArray(selectedVariableAxes)) {
throw new Error(
`Invalid axes value for font \`${fontFamily}\`, expected an array of axes.\nAvailable axes: ${formatValues(
defineAbleAxes
)}`
)
}
selectedVariableAxes.forEach((key) => {
if (!defineAbleAxes.some((tag) => tag === key)) {
throw new Error(
`Invalid axes value \`${key}\` for font \`${fontFamily}\`.\nAvailable axes: ${formatValues(
defineAbleAxes
)}`
)
}
})
}
const variableAxes: [string, string][] = allAxes
.filter(
({ tag }) => tag === 'wght' || selectedVariableAxes?.includes(tag)
)
.map(({ tag, min, max }) => [tag, `${min}..${max}`])
return [...italicAxis, ...variableAxes]
} else {
return [...italicAxis, ['wght', weight]]
}
}
{
"compilerOptions": {
"strict": true,
"resolveJsonModule": true,
"module": "commonjs",
"esModuleInterop": true,
"target": "es2019",
"outDir": "dist",
"rootDir": "src"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
...@@ -98,7 +98,7 @@ function parseGoogleFontName(css: string): Array<string> { ...@@ -98,7 +98,7 @@ function parseGoogleFontName(css: string): Array<string> {
return [...fontNames] return [...fontNames]
} }
function calculateOverrideCSS(font: string, fontMetrics: any) { export function calculateOverrideCSS(font: string, fontMetrics: any) {
const fontName = font.toLowerCase().trim().replace(/ /g, '-') const fontName = font.toLowerCase().trim().replace(/ /g, '-')
const fontKey = font.toLowerCase().trim().replace(/ /g, '') const fontKey = font.toLowerCase().trim().replace(/ /g, '')
const { category, ascentOverride, descentOverride, lineGapOverride } = const { category, ascentOverride, descentOverride, lineGapOverride } =
......
...@@ -24,6 +24,7 @@ importers: ...@@ -24,6 +24,7 @@ importers:
'@next/bundle-analyzer': workspace:* '@next/bundle-analyzer': workspace:*
'@next/env': workspace:* '@next/env': workspace:*
'@next/eslint-plugin-next': workspace:* '@next/eslint-plugin-next': workspace:*
'@next/font': workspace:*
'@next/mdx': workspace:* '@next/mdx': workspace:*
'@next/plugin-storybook': workspace:* '@next/plugin-storybook': workspace:*
'@next/polyfill-module': workspace:* '@next/polyfill-module': workspace:*
...@@ -180,6 +181,7 @@ importers: ...@@ -180,6 +181,7 @@ importers:
'@next/bundle-analyzer': link:packages/next-bundle-analyzer '@next/bundle-analyzer': link:packages/next-bundle-analyzer
'@next/env': link:packages/next-env '@next/env': link:packages/next-env
'@next/eslint-plugin-next': link:packages/eslint-plugin-next '@next/eslint-plugin-next': link:packages/eslint-plugin-next
'@next/font': link:packages/font
'@next/mdx': link:packages/next-mdx '@next/mdx': link:packages/next-mdx
'@next/plugin-storybook': link:packages/next-plugin-storybook '@next/plugin-storybook': link:packages/next-plugin-storybook
'@next/polyfill-module': link:packages/next-polyfill-module '@next/polyfill-module': link:packages/next-polyfill-module
...@@ -561,8 +563,8 @@ importers: ...@@ -561,8 +563,8 @@ importers:
source-map: 0.6.1 source-map: 0.6.1
stream-browserify: 3.0.0 stream-browserify: 3.0.0
stream-http: 3.1.1 stream-http: 3.1.1
string_decoder: 1.3.0
string-hash: 1.1.3 string-hash: 1.1.3
string_decoder: 1.3.0
strip-ansi: 6.0.0 strip-ansi: 6.0.0
styled-jsx: 5.0.7 styled-jsx: 5.0.7
tar: 6.1.11 tar: 6.1.11
...@@ -750,8 +752,8 @@ importers: ...@@ -750,8 +752,8 @@ importers:
source-map: 0.6.1 source-map: 0.6.1
stream-browserify: 3.0.0 stream-browserify: 3.0.0
stream-http: 3.1.1 stream-http: 3.1.1
string_decoder: 1.3.0
string-hash: 1.1.3 string-hash: 1.1.3
string_decoder: 1.3.0
strip-ansi: 6.0.0 strip-ansi: 6.0.0
tar: 6.1.11 tar: 6.1.11
taskr: 1.1.0 taskr: 1.1.0
...@@ -11224,8 +11226,8 @@ packages: ...@@ -11224,8 +11226,8 @@ packages:
engines: { node: '>=10' } engines: { node: '>=10' }
hasBin: true hasBin: true
dependencies: dependencies:
is-text-path: 1.0.1
JSONStream: 1.3.5 JSONStream: 1.3.5
is-text-path: 1.0.1
lodash: 4.17.21 lodash: 4.17.21
meow: 8.1.2 meow: 8.1.2
split2: 2.2.0 split2: 2.2.0
......
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 = `type Display = 'auto'|'block'|'swap'|'fallback'|'optional'
type FontModule = { className: string, variable: string, style: { fontFamily: string, fontWeight?: number, fontStyle?: string } }
`
const fontData = {}
for (let { family, fonts, axes } of familyMetadataList) {
let hasItalic = false
const variants = Object.keys(fonts).map((variant) => {
if (variant.endsWith('i')) {
hasItalic = true
return `${variant.slice(0, 3)}-italic`
}
return variant
})
const hasVariableFont = axes.length > 0
let optionalAxes
if (hasVariableFont) {
variants.push('variable')
if (hasItalic) {
variants.push('variable-italic')
}
const nonWeightAxes = axes.filter(({ tag }) => tag !== 'wght')
if (nonWeightAxes.length > 0) {
optionalAxes = nonWeightAxes
}
}
fontData[family] = {
variants,
axes: hasVariableFont ? axes : undefined,
}
const optionalIfVariableFont = hasVariableFont ? '?' : ''
fontFunctions += `export declare function ${family.replaceAll(
' ',
'_'
)}(options${optionalIfVariableFont}: {
variant${optionalIfVariableFont}:${variants
.map((variant) => `"${variant}"`)
.join('|')}
display?:Display,
preload?:boolean,
fallback?: string[]
adjustFontFallback?: boolean
${
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)
),
])
})()
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment