MCPcopy
hub / github.com/vercel/next.js / nextFontLocalFontLoader

Function nextFontLocalFontLoader

packages/font/src/local/loader.ts:15–112  ·  view source on GitHub ↗
({
  functionName,
  variableName,
  data,
  emitFontFile,
  resolve,
  loaderContext,
})

Source from the content-addressed store, hash-verified

13import { validateLocalFontFunctionCall } from './validate-local-font-function-call'
14
15const nextFontLocalFontLoader: FontLoader = async ({
16 functionName,
17 variableName,
18 data,
19 emitFontFile,
20 resolve,
21 loaderContext,
22}) => {
23 const {
24 src,
25 display,
26 fallback,
27 preload,
28 variable,
29 adjustFontFallback,
30 declarations,
31 weight: defaultWeight,
32 style: defaultStyle,
33 } = validateLocalFontFunctionCall(functionName, data[0])
34
35 // Load all font files and emit them to the .next output directory
36 // Also generate a @font-face CSS for each font file
37 const fontFiles = await Promise.all(
38 src.map(async ({ path, style, weight, ext, format }) => {
39 const resolved = await resolve(path)
40 const fileBuffer = await promisify(loaderContext.fs.readFile)(resolved)
41 const fontUrl = emitFontFile(
42 fileBuffer,
43 ext,
44 preload,
45 typeof adjustFontFallback === 'undefined' || !!adjustFontFallback
46 )
47
48 // Try to load font metadata from the font file using fontkit.
49 // The data is used to calculate the fallback font override values.
50 let fontMetadata: any
51 try {
52 fontMetadata = fontFromBuffer?.(fileBuffer)
53 } catch (e) {
54 console.error(`Failed to load font file: ${resolved}\n${e}`)
55 }
56
57 // Check if `font-family` is explicitly defined in `declarations`
58 const hasCustomFontFamily = declarations?.some(
59 ({ prop }) => prop === 'font-family'
60 )
61
62 // Get all values that should be added to the @font-face declaration
63 const fontFaceProperties = [
64 ...(declarations
65 ? declarations.map(({ prop, value }) => [prop, value])
66 : []),
67 ...(hasCustomFontFamily ? [] : [['font-family', variableName]]),
68 ['src', `url(${fontUrl}) format('${format}')`],
69 ['font-display', display],
70 ...((weight ?? defaultWeight)
71 ? [['font-weight', weight ?? defaultWeight]]
72 : []),

Callers 1

loader.test.tsFile · 0.85

Calls 10

allMethod · 0.80
someMethod · 0.80
errorMethod · 0.65
resolveFunction · 0.50
promisifyFunction · 0.50
mapMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected