Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves font related types to `next/font` so they can be reused in font loaders. Adds an argument to font loaders, the relative path from the app root to the module consuming the loader. Needed for resolving local files relative to the module calling it. Also used to improve error message. Adds `@next/font/local` font loader. Similar to `@next/font/google` but used to host locally downloaded font files.
- Loading branch information
Hannes Bornö
committed
Sep 22, 2022
1 parent
77c8a2c
commit 75bbf00
Showing
46 changed files
with
803 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '../dist/local/index' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new Error('@next/font/local is not correctly setup') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '../dist/local/loader' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../dist/local/loader') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
}, | ||
"files": [ | ||
"dist", | ||
"google" | ||
"google", | ||
"local" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import type { FontModule } from 'next/font' | ||
type Display = 'auto' | 'block' | 'swap' | 'fallback' | 'optional' | ||
type LocalFont = { | ||
src: string | Array<{ file: string; unicodeRange: string }> | ||
display?: Display | ||
weight?: string | ||
style?: string | ||
fallback?: string[] | ||
preload?: boolean | ||
|
||
ascentOverride?: string | ||
descentOverride?: string | ||
fontStretch?: string | ||
fontVariant?: string | ||
fontFeatureSettings?: string | ||
fontVariationSettings?: string | ||
lineGapOverride?: string | ||
sizeAdjust?: string | ||
} | ||
|
||
export default function localFont(options: LocalFont): FontModule { | ||
throw new Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { FontLoader } from 'next/font' | ||
|
||
import { promisify } from 'util' | ||
import { validateData } from './utils' | ||
|
||
const fetchFonts: FontLoader = async ({ | ||
functionName, | ||
data, | ||
emitFontFile, | ||
resolve, | ||
fs, | ||
}) => { | ||
const { | ||
family, | ||
files, | ||
display, | ||
weight, | ||
style, | ||
fallback, | ||
preload, | ||
ascentOverride, | ||
descentOverride, | ||
lineGapOverride, | ||
fontStretch, | ||
fontFeatureSettings, | ||
sizeAdjust, | ||
} = validateData(functionName, data) | ||
|
||
const fontFaces = await Promise.all( | ||
files.map(async ({ file, ext, format, unicodeRange }) => { | ||
const resolved = await resolve(file) | ||
const fileBuffer = await promisify(fs.readFile)(resolved) | ||
|
||
const fontUrl = emitFontFile(fileBuffer, ext, preload) | ||
|
||
const fontFaceProperties = [ | ||
['font-family', `'${family}'`], | ||
['src', `url(${fontUrl}) format('${format}')`], | ||
['font-display', display], | ||
...(weight ? [['font-weight', weight]] : []), | ||
...(style ? [['font-style', style]] : []), | ||
...(ascentOverride ? [['ascent-override', ascentOverride]] : []), | ||
...(descentOverride ? [['descent-override', descentOverride]] : []), | ||
...(lineGapOverride ? [['line-gap-override', lineGapOverride]] : []), | ||
...(fontStretch ? [['font-stretch', fontStretch]] : []), | ||
...(fontFeatureSettings | ||
? [['font-feature-settings', fontFeatureSettings]] | ||
: []), | ||
...(sizeAdjust ? [['size-adjust', sizeAdjust]] : []), | ||
...(unicodeRange ? [['unicode-range', unicodeRange]] : ''), | ||
] | ||
|
||
return `@font-face { | ||
${fontFaceProperties | ||
.map(([property, value]) => `${property}: ${value};`) | ||
.join('\n')} | ||
}` | ||
}) | ||
) | ||
|
||
return { | ||
css: fontFaces.join('\n'), | ||
fallbackFonts: fallback, | ||
} | ||
} | ||
|
||
export default fetchFonts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const allowedDisplayValues = ['auto', 'block', 'swap', 'fallback', 'optional'] | ||
|
||
const formatValues = (values: string[]) => | ||
values.map((val) => `\`${val}\``).join(', ') | ||
|
||
const extToFormat = { | ||
woff: 'woff', | ||
woff2: 'woff2', | ||
ttf: 'truetype', | ||
otf: 'opentype', | ||
eot: 'embedded-opentype', | ||
} | ||
|
||
type FontOptions = { | ||
family: string | ||
files: Array<{ | ||
file: string | ||
ext: string | ||
format: string | ||
unicodeRange?: string | ||
}> | ||
display: string | ||
weight?: string | ||
style?: string | ||
fallback?: string[] | ||
preload: boolean | ||
ascentOverride?: string | ||
descentOverride?: string | ||
fontStretch?: string | ||
fontVariant?: string | ||
fontFeatureSettings?: string | ||
fontVariationSettings?: string | ||
lineGapOverride?: string | ||
sizeAdjust?: string | ||
} | ||
export function validateData(functionName: string, data: any): FontOptions { | ||
if (functionName) { | ||
throw new Error(`@next/font/local has no named exports`) | ||
} | ||
let { | ||
src, | ||
display = 'optional', | ||
weight, | ||
style, | ||
fallback, | ||
preload = true, | ||
ascentOverride, | ||
descentOverride, | ||
fontStretch, | ||
fontVariant, | ||
fontFeatureSettings, | ||
fontVariationSettings, | ||
lineGapOverride, | ||
sizeAdjust, | ||
} = data[0] || ({} as any) | ||
|
||
if (!allowedDisplayValues.includes(display)) { | ||
throw new Error( | ||
`Invalid display value \`${display}\`.\nAvailable display values: ${formatValues( | ||
allowedDisplayValues | ||
)}` | ||
) | ||
} | ||
|
||
const srcArray = Array.isArray(src) ? src : [{ file: src }] | ||
|
||
if (srcArray.length === 0) { | ||
throw new Error('Src must contain one or more files') | ||
} | ||
|
||
const files = srcArray.map(({ file, unicodeRange }) => { | ||
if (!file) { | ||
throw new Error('Src array objects must have a `file` property') | ||
} | ||
if (srcArray.length > 1 && !unicodeRange) { | ||
throw new Error( | ||
"Files must have a unicode-range if there's more than one" | ||
) | ||
} | ||
|
||
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(file)?.[1] | ||
if (!ext) { | ||
throw new Error(`Unexpected file \`${file}\``) | ||
} | ||
return { | ||
file, | ||
unicodeRange, | ||
ext, | ||
format: extToFormat[ext as 'woff' | 'woff2' | 'eot' | 'ttf' | 'otf'], | ||
} | ||
}) | ||
|
||
const family = /.+\/(.+?)\./.exec(files[0].file)![1] | ||
|
||
return { | ||
family, | ||
files, | ||
display, | ||
weight, | ||
style, | ||
fallback, | ||
preload, | ||
ascentOverride, | ||
descentOverride, | ||
fontStretch, | ||
fontVariant, | ||
fontFeatureSettings, | ||
fontVariationSettings, | ||
lineGapOverride, | ||
sizeAdjust, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/next-swc/crates/core/tests/errors/next-font-loaders/not-const/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import inter1 from '@next/font/google?Inter;{"variant":"400"}'; | ||
import inter2 from '@next/font/google?Inter;{"variant":"400"}'; | ||
import inter1 from '@next/font/google?pages/test.tsx;Inter;{"variant":"400"}'; | ||
import inter2 from '@next/font/google?pages/test.tsx;Inter;{"variant":"400"}'; | ||
var i = 10; | ||
var i2 = 20; |
Oops, something went wrong.