Skip to content

Commit

Permalink
Refactoring in @next/font (#43848)
Browse files Browse the repository at this point in the history
Some refactoring, renaming and fixed comments in @next/font

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
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`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
hanneslund committed Dec 8, 2022
1 parent 589f090 commit df8dde7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
31 changes: 14 additions & 17 deletions packages/font/src/google/loader.ts
Expand Up @@ -5,7 +5,6 @@ import { calculateSizeAdjustValues } from 'next/dist/server/font-utils'
import * as Log from 'next/dist/build/output/log'
// @ts-ignore
import chalk from 'next/dist/compiled/chalk'
// @ts-ignore
import {
fetchCSSFromGoogleFonts,
fetchFontFile,
Expand Down Expand Up @@ -94,6 +93,17 @@ const downloadGoogleFonts: FontLoader = async ({
}
}

const result = {
fallbackFonts: fallback,
weight:
weights.length === 1 && weights[0] !== 'variable'
? weights[0]
: undefined,
style: styles.length === 1 ? styles[0] : undefined,
variable,
adjustFontFallback: adjustFontFallbackMetrics,
}

try {
const hasCachedCSS = cssCache.has(url)
let fontFaceDeclarations = hasCachedCSS
Expand Down Expand Up @@ -156,7 +166,7 @@ const downloadGoogleFonts: FontLoader = async ({
}

const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(googleFontFileUrl)![1]
// Emit font file to .next/static/fonts
// Emit font file to .next/static/media
const selfHostedFileUrl = emitFontFile(
fontFileBuffer,
ext,
Expand All @@ -180,15 +190,8 @@ const downloadGoogleFonts: FontLoader = async ({
}

return {
...result,
css: updatedCssResponse,
fallbackFonts: fallback,
weight:
weights.length === 1 && weights[0] !== 'variable'
? weights[0]
: undefined,
style: styles.length === 1 ? styles[0] : undefined,
variable,
adjustFontFallback: adjustFontFallbackMetrics,
}
} catch (err) {
loaderContext.cacheable(false)
Expand All @@ -213,14 +216,8 @@ const downloadGoogleFonts: FontLoader = async ({
css += '\n}'

return {
...result,
css,
fallbackFonts: fallback,
weight:
weights.length === 1 && weights[0] !== 'variable'
? weights[0]
: undefined,
style: styles.length === 1 ? styles[0] : undefined,
variable,
}
} else {
throw err
Expand Down
9 changes: 6 additions & 3 deletions packages/next/build/webpack/config/blocks/css/index.ts
Expand Up @@ -4,7 +4,7 @@ import { webpack } from 'next/dist/compiled/webpack/webpack'
import { loader, plugin } from '../../helpers'
import { ConfigurationContext, ConfigurationFn, pipe } from '../../utils'
import { getCssModuleLoader, getGlobalCssLoader } from './loaders'
import { getFontLoader } from './loaders/font-loader'
import { getNextFontLoader } from './loaders/next-font'
import {
getCustomDocumentError,
getGlobalImportError,
Expand Down Expand Up @@ -188,7 +188,6 @@ export const css = curry(async function css(
])
: undefined

// Font loaders cannot be imported in _document.
fontLoaders?.forEach(([fontLoaderPath, fontLoaderOptions]) => {
// Matches the resolved font loaders noop files to run next-font-loader
fns.push(
Expand All @@ -197,7 +196,11 @@ export const css = curry(async function css(
markRemovable({
sideEffects: false,
test: fontLoaderPath,
use: getFontLoader(ctx, lazyPostCSSInitializer, fontLoaderOptions),
use: getNextFontLoader(
ctx,
lazyPostCSSInitializer,
fontLoaderOptions
),
}),
],
})
Expand Down
Expand Up @@ -3,7 +3,7 @@ import { ConfigurationContext } from '../../../utils'
import { getClientStyleLoader } from './client'
import { cssFileResolve } from './file-resolve'

export function getFontLoader(
export function getNextFontLoader(
ctx: ConfigurationContext,
postcss: any,
fontLoaderOptions: any
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/webpack/loaders/next-font-loader/index.ts
Expand Up @@ -4,7 +4,7 @@ import { promises as fs } from 'fs'
import path from 'path'
import chalk from 'next/dist/compiled/chalk'
import loaderUtils from 'next/dist/compiled/loader-utils3'
import postcssFontLoaderPlugn from './postcss-font-loader'
import postcssNextFontPlugin from './postcss-next-font'
import { promisify } from 'util'
import { CONFIG_FILES } from '../../../../shared/lib/constants'

Expand Down Expand Up @@ -110,7 +110,7 @@ export default async function nextFontLoader(this: any) {
)
// Add CSS classes, exports and make the font-family localy scoped by turning it unguessable
const result = await postcss(
postcssFontLoaderPlugn({
postcssNextFontPlugin({
exports,
fontFamilyHash,
fallbackFonts,
Expand Down
@@ -1,7 +1,7 @@
import type { AdjustFontFallback } from '../../../../font'
import postcss, { Declaration } from 'postcss'

const postcssFontLoaderPlugn = ({
const postcssNextFontPlugin = ({
exports,
fontFamilyHash,
fallbackFonts = [],
Expand All @@ -19,7 +19,7 @@ const postcssFontLoaderPlugn = ({
style?: string
}) => {
return {
postcssPlugin: 'postcss-font-loader',
postcssPlugin: 'postcss-next-font',
Once(root: any) {
let fontFamily: string | undefined

Expand All @@ -32,7 +32,7 @@ const postcssFontLoaderPlugn = ({
return `'__${family.replace(/ /g, '_')}_${fontFamilyHash}'`
}

// Hash font-family name
// Hash font-family names
for (const node of root.nodes) {
if (node.type === 'atrule' && node.name === 'font-face') {
const familyNode = node.nodes.find(
Expand Down Expand Up @@ -118,6 +118,7 @@ const postcssFontLoaderPlugn = ({
...(adjustFontFallbackFamily ? [adjustFontFallbackFamily] : []),
...fallbackFonts,
].join(', ')

// Add class with family, weight and style
const classRule = new postcss.Rule({ selector: '.className' })
classRule.nodes = [
Expand Down Expand Up @@ -171,6 +172,6 @@ const postcssFontLoaderPlugn = ({
}
}

postcssFontLoaderPlugn.postcss = true
postcssNextFontPlugin.postcss = true

export default postcssFontLoaderPlugn
export default postcssNextFontPlugin

0 comments on commit df8dde7

Please sign in to comment.