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

Font loader support in app (#40898)

Makes sure font loader CSS ends up correctly in the Flight Manifest and Flight Client Entries.

## 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)
parent b24800be
/* target file for webpack loader */
/* target file for webpack loader */
...@@ -48,7 +48,7 @@ impl<'a> FontImportsGenerator<'a> { ...@@ -48,7 +48,7 @@ impl<'a> FontImportsGenerator<'a> {
return Some(ImportDecl { return Some(ImportDecl {
src: Box::new(Str { src: Box::new(Str {
value: JsWord::from(format!( value: JsWord::from(format!(
"{}?{}", "{}/target.css?{}",
font_function.loader, font_function.loader,
values.join(";") values.join(";")
)), )),
......
import firaCode from "@next/font/google?pages/test.tsx;Abel"; import firaCode from "@next/font/google/target.css?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter"; import inter from "@next/font/google/target.css?pages/test.tsx;Inter";
import React from 'react'; import React from 'react';
export { firaCode }; export { firaCode };
export { inter }; export { inter };
import inter1 from '@next/font/google?pages/test.tsx;Inter;{"variant":"400"}'; import inter1 from '@next/font/google/target.css?pages/test.tsx;Inter;{"variant":"400"}';
import inter2 from '@next/font/google?pages/test.tsx;Inter;{"variant":"400"}'; import inter2 from '@next/font/google/target.css?pages/test.tsx;Inter;{"variant":"400"}';
var i = 10; var i = 10;
var i2 = 20; var i2 = 20;
import a from "@next/font/google?pages/test.tsx;ABeeZee;{}"; import a from "@next/font/google/target.css?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google?pages/test.tsx;ABeeZee;{}"; import a from "@next/font/google/target.css?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google?pages/test.tsx;ABeeZee;{}"; import a from "@next/font/google/target.css?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google?pages/test.tsx;ABeeZee;{}"; import a from "@next/font/google/target.css?pages/test.tsx;ABeeZee;{}";
const a = fn({ const a = fn({
10: 'hello' 10: 'hello'
}); });
......
import inter from "@next/font/google?pages/test.tsx;Inter;{};[]"; import inter from "@next/font/google/target.css?pages/test.tsx;Inter;{};[]";
const a = fn(...{}, ...[]); const a = fn(...{}, ...[]);
import font from 'cool-fonts?pages/test.tsx;;{"prop":true}'; import font from 'cool-fonts/target.css?pages/test.tsx;;{"prop":true}';
import firaCode from "@next/font/google?pages/test.tsx;Abel"; import firaCode from "@next/font/google/target.css?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter"; import inter from "@next/font/google/target.css?pages/test.tsx;Inter";
import React from 'react'; import React from 'react';
export { firaCode }; export { firaCode };
export { inter }; export { inter };
import firaCode from "@next/font/google?pages/test.tsx;Abel"; import firaCode from "@next/font/google/target.css?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter"; import inter from "@next/font/google/target.css?pages/test.tsx;Inter";
import React from 'react'; import React from 'react';
export { firaCode }; export { firaCode };
export default inter; export default inter;
import firaCode from '@next/font/google?pages/test.tsx;Fira_Code;{"fallback":["system-ui",{"key":false},[]],"key":{"key2":{}},"preload":true,"variant":"400"}'; import firaCode from '@next/font/google/target.css?pages/test.tsx;Fira_Code;{"fallback":["system-ui",{"key":false},[]],"key":{"key2":{}},"preload":true,"variant":"400"}';
import React from 'react'; import React from 'react';
console.log(firaCode); console.log(firaCode);
import acme1 from 'cool-fonts?pages/test.tsx;Acme;{"variant":"400"}'; import acme1 from 'cool-fonts/target.css?pages/test.tsx;Acme;{"variant":"400"}';
import React from 'react'; import React from 'react';
import geo from '@next/font/google?pages/test.tsx;Geo;"test";[1.0];{"a":2.0};3.0'; import geo from '@next/font/google/target.css?pages/test.tsx;Geo;"test";[1.0];{"a":2.0};3.0';
import inter from '@next/font/google?pages/test.tsx;Inter;{"display":"swap","variant":"900"}'; import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"display":"swap","variant":"900"}';
import inter from '@next/font/google?pages/test.tsx;Inter;{"display":"swap","variant":"900"}'; import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"display":"swap","variant":"900"}';
import React from 'react'; import React from 'react';
import inter from '@next/font/google?pages/test.tsx;Inter;{"variant":"900"}'; import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"variant":"900"}';
import fira from 'cool-fonts?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}'; import fira from 'cool-fonts/target.css?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}';
import React from 'react'; import React from 'react';
import firaCode from '@next/font/google?pages/test.tsx;Fira_Code;{"fallback":["system-ui"],"variant":"400"}'; import firaCode from '@next/font/google/target.css?pages/test.tsx;Fira_Code;{"fallback":["system-ui"],"variant":"400"}';
import inter from '@next/font/google?pages/test.tsx;Inter;{"display":"swap","variant":"900"}'; import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"display":"swap","variant":"900"}';
import React from 'react'; import React from 'react';
import inter from '@next/font/google?pages/test.tsx;Inter;{"variant":"900"}'; import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"variant":"900"}';
import fira from '@next/font/google?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}'; import fira from '@next/font/google/target.css?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}';
import React from 'react'; import React from 'react';
import fira from "@next/font/google?pages/test.tsx;Fira_Code"; import fira from "@next/font/google/target.css?pages/test.tsx;Fira_Code";
...@@ -1152,6 +1152,13 @@ export default async function getBaseWebpackConfig( ...@@ -1152,6 +1152,13 @@ export default async function getBaseWebpackConfig(
}, },
} }
const fontLoaderTargets =
config.experimental.fontLoaders &&
Object.keys(config.experimental.fontLoaders).map((fontLoader) => {
const resolved = require.resolve(fontLoader)
return path.join(resolved, '../target.css')
})
let webpackConfig: webpack.Configuration = { let webpackConfig: webpack.Configuration = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined, parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
// @ts-ignore // @ts-ignore
...@@ -1883,10 +1890,12 @@ export default async function getBaseWebpackConfig( ...@@ -1883,10 +1890,12 @@ export default async function getBaseWebpackConfig(
(isClient (isClient
? new FlightManifestPlugin({ ? new FlightManifestPlugin({
dev, dev,
fontLoaderTargets,
}) })
: new FlightClientEntryPlugin({ : new FlightClientEntryPlugin({
dev, dev,
isEdgeServer, isEdgeServer,
fontLoaderTargets,
})), })),
!dev && !dev &&
isClient && isClient &&
......
import path from 'path'
import curry from 'next/dist/compiled/lodash.curry' import curry from 'next/dist/compiled/lodash.curry'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import { loader, plugin } from '../../helpers' import { loader, plugin } from '../../helpers'
...@@ -180,32 +181,11 @@ export const css = curry(async function css( ...@@ -180,32 +181,11 @@ export const css = curry(async function css(
const fns: ConfigurationFn[] = [] const fns: ConfigurationFn[] = []
// CSS cannot be imported in _document. This comes before everything because
// global CSS nor CSS modules work in said file.
fns.push(
loader({
oneOf: [
markRemovable({
test: regexLikeCss,
// Use a loose regex so we don't have to crawl the file system to
// find the real file name (if present).
issuer: /pages[\\/]_document\./,
use: {
loader: 'error-loader',
options: {
reason: getCustomDocumentError(),
},
},
}),
],
})
)
// Resolve the configured font loaders, the resolved files are noop files that next-font-loader will match // Resolve the configured font loaders, the resolved files are noop files that next-font-loader will match
let fontLoaders: [string, string][] | undefined = ctx.experimental.fontLoaders let fontLoaders: [string, string][] | undefined = ctx.experimental.fontLoaders
? Object.entries(ctx.experimental.fontLoaders).map( ? Object.entries(ctx.experimental.fontLoaders).map(
([fontLoader, fontLoaderOptions]: any) => [ ([fontLoader, fontLoaderOptions]: any) => [
require.resolve(fontLoader), path.join(require.resolve(fontLoader), '../target.css'),
fontLoaderOptions, fontLoaderOptions,
] ]
) )
...@@ -254,6 +234,27 @@ export const css = curry(async function css( ...@@ -254,6 +234,27 @@ export const css = curry(async function css(
) )
}) })
// CSS cannot be imported in _document. This comes before everything because
// global CSS nor CSS modules work in said file.
fns.push(
loader({
oneOf: [
markRemovable({
test: regexLikeCss,
// Use a loose regex so we don't have to crawl the file system to
// find the real file name (if present).
issuer: /pages[\\/]_document\./,
use: {
loader: 'error-loader',
options: {
reason: getCustomDocumentError(),
},
},
}),
],
})
)
// CSS Modules support must be enabled on the server and client so the class // CSS Modules support must be enabled on the server and client so the class
// names are available for SSR or Prerendering. // names are available for SSR or Prerendering.
if (ctx.experimental.appDir && !ctx.isProduction) { if (ctx.experimental.appDir && !ctx.isProduction) {
......
...@@ -23,6 +23,7 @@ import { isClientComponentModule } from '../loaders/utils' ...@@ -23,6 +23,7 @@ import { isClientComponentModule } from '../loaders/utils'
interface Options { interface Options {
dev: boolean dev: boolean
isEdgeServer: boolean isEdgeServer: boolean
fontLoaderTargets?: string[]
} }
const PLUGIN_NAME = 'ClientEntryPlugin' const PLUGIN_NAME = 'ClientEntryPlugin'
...@@ -38,10 +39,12 @@ const flightCSSManifest: FlightCSSManifest = {} ...@@ -38,10 +39,12 @@ const flightCSSManifest: FlightCSSManifest = {}
export class FlightClientEntryPlugin { export class FlightClientEntryPlugin {
dev: boolean dev: boolean
isEdgeServer: boolean isEdgeServer: boolean
fontLoaderTargets?: string[]
constructor(options: Options) { constructor(options: Options) {
this.dev = options.dev this.dev = options.dev
this.isEdgeServer = options.isEdgeServer this.isEdgeServer = options.isEdgeServer
this.fontLoaderTargets = options.fontLoaderTargets
} }
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
...@@ -229,12 +232,17 @@ export class FlightClientEntryPlugin { ...@@ -229,12 +232,17 @@ export class FlightClientEntryPlugin {
// Request could be undefined or '' // Request could be undefined or ''
if (!rawRequest) return if (!rawRequest) return
const isFontLoader = this.fontLoaderTargets?.some((fontLoaderTarget) =>
mod.userRequest.startsWith(`${fontLoaderTarget}?`)
)
const modRequest: string | undefined = const modRequest: string | undefined =
!rawRequest.endsWith('.css') && !rawRequest.endsWith('.css') &&
!rawRequest.startsWith('.') && !rawRequest.startsWith('.') &&
!rawRequest.startsWith('/') && !rawRequest.startsWith('/') &&
!rawRequest.startsWith(APP_DIR_ALIAS) !rawRequest.startsWith(APP_DIR_ALIAS)
? rawRequest ? isFontLoader
? mod.userRequest
: rawRequest
: mod.resourceResolveData?.path : mod.resourceResolveData?.path
// Ensure module is not walked again if it's already been visited // Ensure module is not walked again if it's already been visited
...@@ -249,7 +257,7 @@ export class FlightClientEntryPlugin { ...@@ -249,7 +257,7 @@ export class FlightClientEntryPlugin {
} }
visitedBySegment[layoutOrPageRequest].add(modRequest) visitedBySegment[layoutOrPageRequest].add(modRequest)
const isCSS = regexCSS.test(modRequest) const isCSS = isFontLoader || regexCSS.test(modRequest)
const isClientComponent = isClientComponentModule(mod) const isClientComponent = isClientComponentModule(mod)
if (isCSS) { if (isCSS) {
......
...@@ -19,6 +19,7 @@ import { isClientComponentModule } from '../loaders/utils' ...@@ -19,6 +19,7 @@ import { isClientComponentModule } from '../loaders/utils'
interface Options { interface Options {
dev: boolean dev: boolean
fontLoaderTargets?: string[]
} }
/** /**
...@@ -101,9 +102,11 @@ export function traverseModules( ...@@ -101,9 +102,11 @@ export function traverseModules(
export class FlightManifestPlugin { export class FlightManifestPlugin {
dev: Options['dev'] = false dev: Options['dev'] = false
fontLoaderTargets?: Options['fontLoaderTargets']
constructor(options: Options) { constructor(options: Options) {
this.dev = options.dev this.dev = options.dev
this.fontLoaderTargets = options.fontLoaderTargets
} }
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
...@@ -144,6 +147,7 @@ export class FlightManifestPlugin { ...@@ -144,6 +147,7 @@ export class FlightManifestPlugin {
__edge_ssr_module_mapping__: {}, __edge_ssr_module_mapping__: {},
} }
const dev = this.dev const dev = this.dev
const fontLoaderTargets = this.fontLoaderTargets
const clientRequestsSet = new Set() const clientRequestsSet = new Set()
...@@ -168,7 +172,11 @@ export class FlightManifestPlugin { ...@@ -168,7 +172,11 @@ export class FlightManifestPlugin {
id: ModuleId, id: ModuleId,
mod: webpack.NormalModule mod: webpack.NormalModule
) { ) {
const isFontLoader = fontLoaderTargets?.some((fontLoaderTarget) =>
mod.resource?.startsWith(`${fontLoaderTarget}?`)
)
const isCSSModule = const isCSSModule =
isFontLoader ||
mod.resource?.endsWith('.css') || mod.resource?.endsWith('.css') ||
mod.type === 'css/mini-extract' || mod.type === 'css/mini-extract' ||
(!!mod.loaders && (!!mod.loaders &&
......
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import cheerio from 'cheerio'
import path from 'path'
import webdriver from 'next-webdriver'
describe('app dir next-font', () => {
if ((global as any).isNextDeploy || (global as any).isNextDev) {
it('should skip next deploy for now', () => {})
return
}
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'next-font')),
dependencies: {
'@next/font': 'canary',
react: 'experimental',
'react-dom': 'experimental',
},
skipStart: true,
})
await next.start()
})
afterAll(() => next.destroy())
describe('import values', () => {
it('should have correct values at /', async () => {
const html = await renderViaHTTP(next.url, '/')
const $ = cheerio.load(html)
// layout
expect(JSON.parse($('#root-layout').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font1_.{6}'$/),
},
})
// page
expect(JSON.parse($('#root-page').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font2_.{6}'$/),
},
})
// Comp
expect(JSON.parse($('#root-comp').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font3_.{6}'$/),
fontStyle: 'italic',
fontWeight: 900,
},
})
})
it('should have correct values at /client', async () => {
const html = await renderViaHTTP(next.url, '/client')
const $ = cheerio.load(html)
// root layout
expect(JSON.parse($('#root-layout').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font1_.{6}'$/),
},
})
// layout
expect(JSON.parse($('#client-layout').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font4_.{6}'$/),
fontWeight: 100,
},
})
// page
expect(JSON.parse($('#client-page').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font5_.{6}'$/),
fontStyle: 'italic',
},
})
// Comp
expect(JSON.parse($('#client-comp').text())).toEqual({
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: expect.stringMatching(/^'__font6_.{6}'$/),
},
})
})
})
describe('computed styles', () => {
it('should have correct styles at /', async () => {
const browser = await webdriver(next.url, '/')
// layout
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-layout")).fontFamily'
)
).toMatch(/^__font1_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-layout")).fontWeight'
)
).toBe('400')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-layout")).fontStyle'
)
).toBe('normal')
// page
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-page")).fontFamily'
)
).toMatch(/^__font2_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-page")).fontWeight'
)
).toBe('400')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-page")).fontStyle'
)
).toBe('normal')
// Comp
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-comp")).fontFamily'
)
).toMatch(/^__font3_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-comp")).fontWeight'
)
).toBe('900')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-comp")).fontStyle'
)
).toBe('italic')
})
it('should have correct styles at /client', async () => {
const browser = await webdriver(next.url, '/client')
// root layout
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-layout")).fontFamily'
)
).toMatch(/^__font1_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-layout")).fontWeight'
)
).toBe('400')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#root-layout")).fontStyle'
)
).toBe('normal')
// layout
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-layout")).fontFamily'
)
).toMatch(/^__font4_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-layout")).fontWeight'
)
).toBe('100')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-layout")).fontStyle'
)
).toBe('normal')
// page
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-page")).fontFamily'
)
).toMatch(/^__font5_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-page")).fontWeight'
)
).toBe('400')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-page")).fontStyle'
)
).toBe('italic')
// Comp
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-comp")).fontFamily'
)
).toMatch(/^__font6_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-comp")).fontWeight'
)
).toBe('400')
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#client-comp")).fontStyle'
)
).toBe('normal')
})
})
})
import { font3 } from '../fonts/fonts'
export default function Component() {
return (
<p id="root-comp" className={font3.className}>
{JSON.stringify(font3)}
</p>
)
}
'client'
import { font6 } from '../../fonts/fonts'
export default function Component() {
return (
<p id="client-comp" className={font6.className}>
{JSON.stringify(font6)}
</p>
)
}
'client'
import { font4 } from '../../fonts/fonts'
export default function Root({ children }) {
return (
<>
<p id="client-layout" className={font4.className}>
{JSON.stringify(font4)}
</p>
{children}
</>
)
}
'client'
import Comp from './Comp'
import { font5 } from '../../fonts/fonts'
export default function HomePage() {
return (
<>
<p id="client-page" className={font5.className}>
{JSON.stringify(font5)}
</p>
<Comp />
</>
)
}
import { font1 } from '../fonts/fonts'
export default function Root({ children }) {
return (
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p id="root-layout" className={font1.className}>
{JSON.stringify(font1)}
</p>
{children}
</body>
</html>
)
}
import Comp from './Comp'
import { font2 } from '../fonts/fonts'
export default function HomePage() {
return (
<>
<p id="root-page" className={font2.className}>
{JSON.stringify(font2)}
</p>
<Comp />
</>
)
}
font1
\ No newline at end of file
font2
\ No newline at end of file
font3
\ No newline at end of file
font4
\ No newline at end of file
font5
\ No newline at end of file
font6
\ No newline at end of file
import localFont from '@next/font/local'
export const font1 = localFont({ src: './font1.woff2' })
export const font2 = localFont({ src: './font2.woff2' })
export const font3 = localFont({
src: './font3.woff2',
weight: '900',
style: 'italic',
})
export const font4 = localFont({ src: './font4.woff2', weight: '100' })
export const font5 = localFont({ src: './font5.woff2', style: 'italic' })
export const font6 = localFont({ src: './font6.woff2' })
module.exports = {
experimental: {
appDir: true,
legacyBrowsers: false,
browsersListForSwc: true,
fontLoaders: {
'@next/font/local': {},
},
},
}
...@@ -42,39 +42,47 @@ describe('@next/font/google', () => { ...@@ -42,39 +42,47 @@ describe('@next/font/google', () => {
// _app.js // _app.js
expect(JSON.parse($('#app-open-sans').text())).toEqual({ expect(JSON.parse($('#app-open-sans').text())).toEqual({
className: '__className_f32d04', className: expect.stringMatching(/^__className_.{6}$/),
variable: '__variable_f32d04', variable: expect.stringMatching(/^__variable_.{6}$/),
style: { style: {
fontFamily: "'__Open_Sans_f32d04', '__open-sans-fallback_f32d04'", fontFamily: expect.stringMatching(
/^'__Open_Sans_.{6}', '__open-sans-fallback_.{6}'$/
),
fontStyle: 'normal', fontStyle: 'normal',
}, },
}) })
// with-fonts.js // with-fonts.js
expect(JSON.parse($('#with-fonts-open-sans').text())).toEqual({ expect(JSON.parse($('#with-fonts-open-sans').text())).toEqual({
className: '__className_f32d04', className: expect.stringMatching(/^__className_.{6}$/),
variable: '__variable_f32d04', variable: expect.stringMatching(/^__variable_.{6}$/),
style: { style: {
fontFamily: "'__Open_Sans_f32d04', '__open-sans-fallback_f32d04'", fontFamily: expect.stringMatching(
/^'__Open_Sans_.{6}', '__open-sans-fallback_.{6}'$/
),
fontStyle: 'normal', fontStyle: 'normal',
}, },
}) })
// CompWithFonts.js // CompWithFonts.js
expect(JSON.parse($('#comp-with-fonts-inter').text())).toEqual({ expect(JSON.parse($('#comp-with-fonts-inter').text())).toEqual({
className: '__className_55e413', className: expect.stringMatching(/^__className_.{6}$/),
variable: '__variable_55e413', variable: expect.stringMatching(/^__variable_.{6}$/),
style: { style: {
fontFamily: "'__Inter_55e413', '__inter-fallback_55e413'", fontFamily: expect.stringMatching(
/^'__Inter_.{6}', '__inter-fallback_.{6}'$/
),
fontStyle: 'normal', fontStyle: 'normal',
fontWeight: 900, fontWeight: 900,
}, },
}) })
expect(JSON.parse($('#comp-with-fonts-roboto').text())).toEqual({ expect(JSON.parse($('#comp-with-fonts-roboto').text())).toEqual({
className: '__className_29a3c6', className: expect.stringMatching(/^__className_.{6}$/),
variable: '__variable_29a3c6', variable: expect.stringMatching(/^__variable_.{6}$/),
style: { style: {
fontFamily: "'__Roboto_29a3c6', '__roboto-fallback_29a3c6'", fontFamily: expect.stringMatching(
/^'__Roboto_.{6}', '__roboto-fallback_.{6}'$/
),
fontStyle: 'italic', fontStyle: 'italic',
fontWeight: 100, fontWeight: 100,
}, },
...@@ -90,7 +98,9 @@ describe('@next/font/google', () => { ...@@ -90,7 +98,9 @@ describe('@next/font/google', () => {
className: '__className_f32d04', className: '__className_f32d04',
variable: '__variable_f32d04', variable: '__variable_f32d04',
style: { style: {
fontFamily: "'__Open_Sans_f32d04', '__open-sans-fallback_f32d04'", fontFamily: expect.stringMatching(
/^'__Open_Sans_.{6}', '__open-sans-fallback_.{6}'$/
),
fontStyle: 'normal', fontStyle: 'normal',
}, },
}) })
...@@ -100,7 +110,7 @@ describe('@next/font/google', () => { ...@@ -100,7 +110,7 @@ describe('@next/font/google', () => {
className: '__className_410624', className: '__className_410624',
variable: '__variable_410624', variable: '__variable_410624',
style: { style: {
fontFamily: "'__my-font_410624'", fontFamily: expect.stringMatching(/^'__my-font_.{6}'$/),
fontStyle: 'italic', fontStyle: 'italic',
fontWeight: 100, fontWeight: 100,
}, },
...@@ -109,7 +119,7 @@ describe('@next/font/google', () => { ...@@ -109,7 +119,7 @@ describe('@next/font/google', () => {
className: '__className_3ff726', className: '__className_3ff726',
variable: '__variable_3ff726', variable: '__variable_3ff726',
style: { style: {
fontFamily: "'__my-other-font_3ff726'", fontFamily: expect.stringMatching(/^'__my-other-font_.{6}'$/),
}, },
}) })
}) })
...@@ -124,7 +134,7 @@ describe('@next/font/google', () => { ...@@ -124,7 +134,7 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#app-open-sans")).fontFamily' 'getComputedStyle(document.querySelector("#app-open-sans")).fontFamily'
) )
).toBe('__Open_Sans_f32d04, __open-sans-fallback_f32d04') ).toMatch(/^__Open_Sans_.{6}, __open-sans-fallback_.{6}$/)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#app-open-sans")).fontWeight' 'getComputedStyle(document.querySelector("#app-open-sans")).fontWeight'
...@@ -141,7 +151,7 @@ describe('@next/font/google', () => { ...@@ -141,7 +151,7 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#with-fonts-open-sans")).fontFamily' 'getComputedStyle(document.querySelector("#with-fonts-open-sans")).fontFamily'
) )
).toBe('__Open_Sans_f32d04, __open-sans-fallback_f32d04') ).toMatch(/^__Open_Sans_.{6}, __open-sans-fallback_.{6}$/)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#with-fonts-open-sans")).fontWeight' 'getComputedStyle(document.querySelector("#with-fonts-open-sans")).fontWeight'
...@@ -168,7 +178,7 @@ describe('@next/font/google', () => { ...@@ -168,7 +178,7 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-inter")).fontFamily' 'getComputedStyle(document.querySelector("#comp-with-fonts-inter")).fontFamily'
) )
).toBe('__Inter_55e413, __inter-fallback_55e413') ).toMatch(/^__Inter_.{6}, __inter-fallback_.{6}$/)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-inter")).fontWeight' 'getComputedStyle(document.querySelector("#comp-with-fonts-inter")).fontWeight'
...@@ -184,7 +194,7 @@ describe('@next/font/google', () => { ...@@ -184,7 +194,7 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-roboto")).fontFamily' 'getComputedStyle(document.querySelector("#comp-with-fonts-roboto")).fontFamily'
) )
).toBe('__Roboto_29a3c6, __roboto-fallback_29a3c6') ).toMatch(/^__Roboto_.{6}, __roboto-fallback_.{6}$/)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-roboto")).fontWeight' 'getComputedStyle(document.querySelector("#comp-with-fonts-roboto")).fontWeight'
...@@ -201,52 +211,56 @@ describe('@next/font/google', () => { ...@@ -201,52 +211,56 @@ describe('@next/font/google', () => {
const browser = await webdriver(next.url, '/variables') const browser = await webdriver(next.url, '/variables')
// Fira Code Variable // Fira Code Variable
const firaCodeRegex = /^__Fira_Code_.{6}, __fira-code-fallback_.{6}$/
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#variables-fira-code")).fontFamily' 'getComputedStyle(document.querySelector("#variables-fira-code")).fontFamily'
) )
).toBe('__Fira_Code_8d0076, __fira-code-fallback_8d0076') ).toMatch(firaCodeRegex)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-fira-code")).fontFamily' 'getComputedStyle(document.querySelector("#without-variables-fira-code")).fontFamily'
) )
).not.toBe('__Fira_Code_8d0076, __fira-code-fallback_8d0076') ).not.toMatch(firaCodeRegex)
// Albert Sant Variable Italic // Albert Sant Variable Italic
const albertSansItalicRegex = /^__Albert_Sans_.{6}$/
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#variables-albert-sans-italic")).fontFamily' 'getComputedStyle(document.querySelector("#variables-albert-sans-italic")).fontFamily'
) )
).toBe('__Albert_Sans_3a491b') ).toMatch(albertSansItalicRegex)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-albert-sans-italic")).fontFamily' 'getComputedStyle(document.querySelector("#without-variables-albert-sans-italic")).fontFamily'
) )
).not.toBe('__Albert_Sans_3a491b') ).not.toMatch(albertSansItalicRegex)
// Inter 900 // Inter 900
const inter900Regex = /^__Inter_.{6}, __inter-fallback_.{6}$/
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#variables-inter-900")).fontFamily' 'getComputedStyle(document.querySelector("#variables-inter-900")).fontFamily'
) )
).toBe('__Inter_09d70c, __inter-fallback_09d70c') ).toMatch(inter900Regex)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-inter-900")).fontFamily' 'getComputedStyle(document.querySelector("#without-variables-inter-900")).fontFamily'
) )
).not.toBe('__Inter_09d70c, __inter-fallback_09d70c') ).not.toMatch(inter900Regex)
// Roboto 100 Italic // Roboto 100 Italic
const roboto100ItalicRegex = /^__Roboto_.{6}, __roboto-fallback_.{6}$/
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#variables-roboto-100-italic")).fontFamily' 'getComputedStyle(document.querySelector("#variables-roboto-100-italic")).fontFamily'
) )
).toBe('__Roboto_29a3c6, __roboto-fallback_29a3c6') ).toMatch(roboto100ItalicRegex)
expect( expect(
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-roboto-100-italic")).fontFamily' 'getComputedStyle(document.querySelector("#without-variables-roboto-100-italic")).fontFamily'
) )
).not.toBe('__Roboto_29a3c6, __roboto-fallback_29a3c6') ).not.toMatch(roboto100ItalicRegex)
}) })
test('page using fallback fonts', async () => { test('page using fallback fonts', async () => {
...@@ -257,8 +271,8 @@ describe('@next/font/google', () => { ...@@ -257,8 +271,8 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-classname")).fontFamily' 'getComputedStyle(document.querySelector("#with-fallback-fonts-classname")).fontFamily'
) )
).toBe( ).toMatch(
'__Open_Sans_f32d04, system-ui, Arial, __open-sans-fallback_f32d04' /^__Open_Sans_.{6}, system-ui, Arial, __open-sans-fallback_.{6}$/
) )
// .style // .style
...@@ -266,8 +280,8 @@ describe('@next/font/google', () => { ...@@ -266,8 +280,8 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-style")).fontFamily' 'getComputedStyle(document.querySelector("#with-fallback-fonts-style")).fontFamily'
) )
).toBe( ).toMatch(
'__Open_Sans_f32d04, system-ui, Arial, __open-sans-fallback_f32d04' /^__Open_Sans_.{6}, system-ui, Arial, __open-sans-fallback_.{6}$/
) )
// .variable // .variable
...@@ -275,8 +289,8 @@ describe('@next/font/google', () => { ...@@ -275,8 +289,8 @@ describe('@next/font/google', () => {
await browser.eval( await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-variable")).fontFamily' 'getComputedStyle(document.querySelector("#with-fallback-fonts-variable")).fontFamily'
) )
).toBe( ).toMatch(
'__Open_Sans_f32d04, system-ui, Arial, __open-sans-fallback_f32d04' /^__Open_Sans_.{6}, system-ui, Arial, __open-sans-fallback_.{6}$/
) )
}) })
}) })
......
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