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> {
return Some(ImportDecl {
src: Box::new(Str {
value: JsWord::from(format!(
"{}?{}",
"{}/target.css?{}",
font_function.loader,
values.join(";")
)),
......
import firaCode from "@next/font/google?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter";
import firaCode from "@next/font/google/target.css?pages/test.tsx;Abel";
import inter from "@next/font/google/target.css?pages/test.tsx;Inter";
import React from 'react';
export { firaCode };
export { inter };
import inter1 from '@next/font/google?pages/test.tsx;Inter;{"variant":"400"}';
import inter2 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/target.css?pages/test.tsx;Inter;{"variant":"400"}';
var i = 10;
var i2 = 20;
import a from "@next/font/google?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google?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/target.css?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google/target.css?pages/test.tsx;ABeeZee;{}";
import a from "@next/font/google/target.css?pages/test.tsx;ABeeZee;{}";
const a = fn({
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(...{}, ...[]);
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 inter from "@next/font/google?pages/test.tsx;Inter";
import firaCode from "@next/font/google/target.css?pages/test.tsx;Abel";
import inter from "@next/font/google/target.css?pages/test.tsx;Inter";
import React from 'react';
export { firaCode };
export { inter };
import firaCode from "@next/font/google?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter";
import firaCode from "@next/font/google/target.css?pages/test.tsx;Abel";
import inter from "@next/font/google/target.css?pages/test.tsx;Inter";
import React from 'react';
export { firaCode };
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';
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 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?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/target.css?pages/test.tsx;Inter;{"display":"swap","variant":"900"}';
import React from 'react';
import inter from '@next/font/google?pages/test.tsx;Inter;{"variant":"900"}';
import fira from 'cool-fonts?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}';
import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"variant":"900"}';
import fira from 'cool-fonts/target.css?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}';
import React from 'react';
import firaCode from '@next/font/google?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 firaCode from '@next/font/google/target.css?pages/test.tsx;Fira_Code;{"fallback":["system-ui"],"variant":"400"}';
import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"display":"swap","variant":"900"}';
import React from 'react';
import inter from '@next/font/google?pages/test.tsx;Inter;{"variant":"900"}';
import fira from '@next/font/google?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}';
import inter from '@next/font/google/target.css?pages/test.tsx;Inter;{"variant":"900"}';
import fira from '@next/font/google/target.css?pages/test.tsx;Fira_Code;{"display":"swap","variant":"400"}';
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(
},
}
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 = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
// @ts-ignore
......@@ -1883,10 +1890,12 @@ export default async function getBaseWebpackConfig(
(isClient
? new FlightManifestPlugin({
dev,
fontLoaderTargets,
})
: new FlightClientEntryPlugin({
dev,
isEdgeServer,
fontLoaderTargets,
})),
!dev &&
isClient &&
......
import path from 'path'
import curry from 'next/dist/compiled/lodash.curry'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { loader, plugin } from '../../helpers'
......@@ -180,32 +181,11 @@ export const css = curry(async function css(
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
let fontLoaders: [string, string][] | undefined = ctx.experimental.fontLoaders
? Object.entries(ctx.experimental.fontLoaders).map(
([fontLoader, fontLoaderOptions]: any) => [
require.resolve(fontLoader),
path.join(require.resolve(fontLoader), '../target.css'),
fontLoaderOptions,
]
)
......@@ -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
// names are available for SSR or Prerendering.
if (ctx.experimental.appDir && !ctx.isProduction) {
......
......@@ -23,6 +23,7 @@ import { isClientComponentModule } from '../loaders/utils'
interface Options {
dev: boolean
isEdgeServer: boolean
fontLoaderTargets?: string[]
}
const PLUGIN_NAME = 'ClientEntryPlugin'
......@@ -38,10 +39,12 @@ const flightCSSManifest: FlightCSSManifest = {}
export class FlightClientEntryPlugin {
dev: boolean
isEdgeServer: boolean
fontLoaderTargets?: string[]
constructor(options: Options) {
this.dev = options.dev
this.isEdgeServer = options.isEdgeServer
this.fontLoaderTargets = options.fontLoaderTargets
}
apply(compiler: webpack.Compiler) {
......@@ -229,12 +232,17 @@ export class FlightClientEntryPlugin {
// Request could be undefined or ''
if (!rawRequest) return
const isFontLoader = this.fontLoaderTargets?.some((fontLoaderTarget) =>
mod.userRequest.startsWith(`${fontLoaderTarget}?`)
)
const modRequest: string | undefined =
!rawRequest.endsWith('.css') &&
!rawRequest.startsWith('.') &&
!rawRequest.startsWith('/') &&
!rawRequest.startsWith(APP_DIR_ALIAS)
? rawRequest
? isFontLoader
? mod.userRequest
: rawRequest
: mod.resourceResolveData?.path
// Ensure module is not walked again if it's already been visited
......@@ -249,7 +257,7 @@ export class FlightClientEntryPlugin {
}
visitedBySegment[layoutOrPageRequest].add(modRequest)
const isCSS = regexCSS.test(modRequest)
const isCSS = isFontLoader || regexCSS.test(modRequest)
const isClientComponent = isClientComponentModule(mod)
if (isCSS) {
......
......@@ -19,6 +19,7 @@ import { isClientComponentModule } from '../loaders/utils'
interface Options {
dev: boolean
fontLoaderTargets?: string[]
}
/**
......@@ -101,9 +102,11 @@ export function traverseModules(
export class FlightManifestPlugin {
dev: Options['dev'] = false
fontLoaderTargets?: Options['fontLoaderTargets']
constructor(options: Options) {
this.dev = options.dev
this.fontLoaderTargets = options.fontLoaderTargets
}
apply(compiler: webpack.Compiler) {
......@@ -144,6 +147,7 @@ export class FlightManifestPlugin {
__edge_ssr_module_mapping__: {},
}
const dev = this.dev
const fontLoaderTargets = this.fontLoaderTargets
const clientRequestsSet = new Set()
......@@ -168,7 +172,11 @@ export class FlightManifestPlugin {
id: ModuleId,
mod: webpack.NormalModule
) {
const isFontLoader = fontLoaderTargets?.some((fontLoaderTarget) =>
mod.resource?.startsWith(`${fontLoaderTarget}?`)
)
const isCSSModule =
isFontLoader ||
mod.resource?.endsWith('.css') ||
mod.type === 'css/mini-extract' ||
(!!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', () => {
// _app.js
expect(JSON.parse($('#app-open-sans').text())).toEqual({
className: '__className_f32d04',
variable: '__variable_f32d04',
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: "'__Open_Sans_f32d04', '__open-sans-fallback_f32d04'",
fontFamily: expect.stringMatching(
/^'__Open_Sans_.{6}', '__open-sans-fallback_.{6}'$/
),
fontStyle: 'normal',
},
})
// with-fonts.js
expect(JSON.parse($('#with-fonts-open-sans').text())).toEqual({
className: '__className_f32d04',
variable: '__variable_f32d04',
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: "'__Open_Sans_f32d04', '__open-sans-fallback_f32d04'",
fontFamily: expect.stringMatching(
/^'__Open_Sans_.{6}', '__open-sans-fallback_.{6}'$/
),
fontStyle: 'normal',
},
})
// CompWithFonts.js
expect(JSON.parse($('#comp-with-fonts-inter').text())).toEqual({
className: '__className_55e413',
variable: '__variable_55e413',
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: "'__Inter_55e413', '__inter-fallback_55e413'",
fontFamily: expect.stringMatching(
/^'__Inter_.{6}', '__inter-fallback_.{6}'$/
),
fontStyle: 'normal',
fontWeight: 900,
},
})
expect(JSON.parse($('#comp-with-fonts-roboto').text())).toEqual({
className: '__className_29a3c6',
variable: '__variable_29a3c6',
className: expect.stringMatching(/^__className_.{6}$/),
variable: expect.stringMatching(/^__variable_.{6}$/),
style: {
fontFamily: "'__Roboto_29a3c6', '__roboto-fallback_29a3c6'",
fontFamily: expect.stringMatching(
/^'__Roboto_.{6}', '__roboto-fallback_.{6}'$/
),
fontStyle: 'italic',
fontWeight: 100,
},
......@@ -90,7 +98,9 @@ describe('@next/font/google', () => {
className: '__className_f32d04',
variable: '__variable_f32d04',
style: {
fontFamily: "'__Open_Sans_f32d04', '__open-sans-fallback_f32d04'",
fontFamily: expect.stringMatching(
/^'__Open_Sans_.{6}', '__open-sans-fallback_.{6}'$/
),
fontStyle: 'normal',
},
})
......@@ -100,7 +110,7 @@ describe('@next/font/google', () => {
className: '__className_410624',
variable: '__variable_410624',
style: {
fontFamily: "'__my-font_410624'",
fontFamily: expect.stringMatching(/^'__my-font_.{6}'$/),
fontStyle: 'italic',
fontWeight: 100,
},
......@@ -109,7 +119,7 @@ describe('@next/font/google', () => {
className: '__className_3ff726',
variable: '__variable_3ff726',
style: {
fontFamily: "'__my-other-font_3ff726'",
fontFamily: expect.stringMatching(/^'__my-other-font_.{6}'$/),
},
})
})
......@@ -124,7 +134,7 @@ describe('@next/font/google', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#app-open-sans")).fontFamily'
)
).toBe('__Open_Sans_f32d04, __open-sans-fallback_f32d04')
).toMatch(/^__Open_Sans_.{6}, __open-sans-fallback_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#app-open-sans")).fontWeight'
......@@ -141,7 +151,7 @@ describe('@next/font/google', () => {
await browser.eval(
'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(
await browser.eval(
'getComputedStyle(document.querySelector("#with-fonts-open-sans")).fontWeight'
......@@ -168,7 +178,7 @@ describe('@next/font/google', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-inter")).fontFamily'
)
).toBe('__Inter_55e413, __inter-fallback_55e413')
).toMatch(/^__Inter_.{6}, __inter-fallback_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-inter")).fontWeight'
......@@ -184,7 +194,7 @@ describe('@next/font/google', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-roboto")).fontFamily'
)
).toBe('__Roboto_29a3c6, __roboto-fallback_29a3c6')
).toMatch(/^__Roboto_.{6}, __roboto-fallback_.{6}$/)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#comp-with-fonts-roboto")).fontWeight'
......@@ -201,52 +211,56 @@ describe('@next/font/google', () => {
const browser = await webdriver(next.url, '/variables')
// Fira Code Variable
const firaCodeRegex = /^__Fira_Code_.{6}, __fira-code-fallback_.{6}$/
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#variables-fira-code")).fontFamily'
)
).toBe('__Fira_Code_8d0076, __fira-code-fallback_8d0076')
).toMatch(firaCodeRegex)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-fira-code")).fontFamily'
)
).not.toBe('__Fira_Code_8d0076, __fira-code-fallback_8d0076')
).not.toMatch(firaCodeRegex)
// Albert Sant Variable Italic
const albertSansItalicRegex = /^__Albert_Sans_.{6}$/
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#variables-albert-sans-italic")).fontFamily'
)
).toBe('__Albert_Sans_3a491b')
).toMatch(albertSansItalicRegex)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-albert-sans-italic")).fontFamily'
)
).not.toBe('__Albert_Sans_3a491b')
).not.toMatch(albertSansItalicRegex)
// Inter 900
const inter900Regex = /^__Inter_.{6}, __inter-fallback_.{6}$/
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#variables-inter-900")).fontFamily'
)
).toBe('__Inter_09d70c, __inter-fallback_09d70c')
).toMatch(inter900Regex)
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#without-variables-inter-900")).fontFamily'
)
).not.toBe('__Inter_09d70c, __inter-fallback_09d70c')
).not.toMatch(inter900Regex)
// Roboto 100 Italic
const roboto100ItalicRegex = /^__Roboto_.{6}, __roboto-fallback_.{6}$/
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#variables-roboto-100-italic")).fontFamily'
)
).toBe('__Roboto_29a3c6, __roboto-fallback_29a3c6')
).toMatch(roboto100ItalicRegex)
expect(
await browser.eval(
'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 () => {
......@@ -257,8 +271,8 @@ describe('@next/font/google', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-classname")).fontFamily'
)
).toBe(
'__Open_Sans_f32d04, system-ui, Arial, __open-sans-fallback_f32d04'
).toMatch(
/^__Open_Sans_.{6}, system-ui, Arial, __open-sans-fallback_.{6}$/
)
// .style
......@@ -266,8 +280,8 @@ describe('@next/font/google', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-style")).fontFamily'
)
).toBe(
'__Open_Sans_f32d04, system-ui, Arial, __open-sans-fallback_f32d04'
).toMatch(
/^__Open_Sans_.{6}, system-ui, Arial, __open-sans-fallback_.{6}$/
)
// .variable
......@@ -275,8 +289,8 @@ describe('@next/font/google', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-variable")).fontFamily'
)
).toBe(
'__Open_Sans_f32d04, system-ui, Arial, __open-sans-fallback_f32d04'
).toMatch(
/^__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