Unverified Commit bebda113 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Remove @next/font resolving for font loaders (#65601)

Remove font loaders resolving for `@next/font`, users should directly
rely on `next/font`, they're intended to be removed.

Also removed the legacy tests
parent 85162c89
......@@ -382,14 +382,7 @@ export function getLoaderSWCOptions({
esm: !!esm,
})
baseOptions.fontLoaders = {
fontLoaders: [
'next/font/local',
'next/font/google',
// TODO: remove this in the next major version
'@next/font/local',
'@next/font/google',
],
fontLoaders: ['next/font/local', 'next/font/google'],
relativeFilePathFromRoot,
}
baseOptions.cjsRequireOptimizer = {
......
......@@ -211,9 +211,6 @@ export const css = curry(async function css(
const nextFontLoaders: Array<[string | RegExp, string, any?]> = [
[require.resolve('next/font/google/target.css'), googleLoader],
[require.resolve('next/font/local/target.css'), localLoader],
// TODO: remove this in the next major version
[/node_modules[\\/]@next[\\/]font[\\/]google[\\/]target.css/, googleLoader],
[/node_modules[\\/]@next[\\/]font[\\/]local[\\/]target.css/, localLoader],
]
nextFontLoaders.forEach(([fontLoaderTarget, fontLoaderPath]) => {
......
export default function Root({ children }) {
return (
<html>
<head>
<title>Hello World</title>
</head>
<body>{children}</body>
</html>
)
}
import styles from './styles.module.css'
export default function Page() {
return (
<>
<p className={styles.p}>hello world</p>
</>
)
}
import localFont from '@next/font/local'
const componentFont = localFont({
src: './comp-font-merriweather.woff2',
preload: false,
})
export default function Component() {
return (
<p className={componentFont.className}>{JSON.stringify(componentFont)}</p>
)
}
import Comp from './comp'
export default function Page() {
return <Comp />
}
import localFont from '@next/font/local'
const layoutFont = localFont({
src: './layout-font-rubik.woff2',
preload: false,
})
export default function Layout({ children }) {
return (
<>
<p className={layoutFont.className}>{JSON.stringify(layoutFont)}</p>
{children}
</>
)
}
'use client'
import localFont from '@next/font/local'
const pageFont = localFont({
src: './page-font-ubuntu-regular.woff2',
preload: false,
})
export default function Page() {
return (
<>
<p className={pageFont.className}>{JSON.stringify(pageFont)}</p>
</>
)
}
import { font3 } from '../../fonts'
export default function Component() {
return (
<p id="root-comp" className={font3.className}>
{JSON.stringify(font3)}
</p>
)
}
'use client'
import { font6 } from '../../../fonts'
export default function Component() {
return (
<p id="client-comp" className={font6.className}>
{JSON.stringify(font6)}
</p>
)
}
'use client'
import { font4 } from '../../../fonts'
export default function Root({ children }) {
return (
<>
<p id="client-layout" className={font4.className}>
{JSON.stringify(font4)}
</p>
{children}
</>
)
}
'use client'
import Comp from './Comp'
import { font5 } from '../../../fonts'
export default function HomePage() {
return (
<>
<p id="client-page" className={font5.className}>
{JSON.stringify(font5)}
</p>
<Comp />
</>
)
}
import localFont from '@next/font/local'
const layoutFont = localFont({ src: './layout-font-nunito-sans.woff2' })
export default function Layout({ children }) {
return (
<>
<p id="layout-with-fonts" className={layoutFont.className}>
{JSON.stringify(layoutFont)}
</p>
{children}
</>
)
}
export default function HomePage() {
return <p>Only layout</p>
}
import { font1 } from '../../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>
)
}
'use client'
export default function Layout({ children }) {
return children
}
import localFont from '@next/font/local'
const pageFont = localFont({ src: './page-font-raleway-thin.woff2' })
export default function HomePage() {
return (
<p id="page-with-fonts" className={pageFont.className}>
{JSON.stringify(pageFont)}
</p>
)
}
import Comp from './Comp'
import { font1, font2 } from '../../fonts'
export default function HomePage() {
return (
<>
<p className={font1.className}>Hello world</p>
<p id="root-page" className={font2.className}>
{JSON.stringify(font2)}
</p>
<Comp />
</>
)
}
export const runtime = 'experimental-edge'
import { font1 } from 'my-font'
export default function HomePage() {
return (
<p id="third-party-page" className={font1.className}>
{JSON.stringify(font1)}
</p>
)
}
import localFont from '@next/font/local'
export const font = localFont({ src: './font-noto-sans.woff2' })
import { font } from './font'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<p className={font.className}>LAYOUT1</p>
{children}
</body>
</html>
)
}
import { font } from '../font'
export default function Page() {
return (
<p id="page-with-same-font" className={font.className}>
Page with same font
</p>
)
}
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/navigation/page-with-same-font">
Go to page with same font
</Link>
</>
)
}
import { Inter, Roboto } from '@next/font/google'
const inter = Inter({
weight: '900',
display: 'swap',
preload: false,
subsets: ['latin'],
})
const roboto = Roboto({
weight: '100',
style: 'italic',
display: 'swap',
preload: true,
subsets: ['vietnamese'],
})
export default function Component() {
return (
<>
<div id="comp-with-fonts-inter" className={inter.className}>
{JSON.stringify(inter)}
</div>
<div id="comp-with-fonts-roboto" style={roboto.style}>
{JSON.stringify(roboto)}
</div>
</>
)
}
import { Open_Sans } from '@next/font/google'
const openSans = Open_Sans({ variable: '--open-sans', subsets: ['latin'] })
function MyApp({ Component, pageProps }) {
return (
<>
<div id="app-open-sans" className={openSans.className}>
{JSON.stringify(openSans)}
</div>
<Component {...pageProps} />
</>
)
}
export { openSans }
export default MyApp
import { Single_Day } from '@next/font/google'
const singleDay = Single_Day({ weight: '400' })
export default function FontWithoutPreloadableSubsets() {
return <p className={singleDay.className}>{JSON.stringify(singleDay)}</p>
}
import { Nabla } from '@next/font/google'
const nabla = Nabla({ subsets: ['latin'] })
export default function VariableFontWithoutWeightRange() {
return (
<p id="nabla" className={nabla.className}>
{JSON.stringify(nabla)}
</p>
)
}
import { Fira_Code, Roboto } from '@next/font/google'
import localFont from '@next/font/local'
const firaCode = Fira_Code({ variable: '--fira-code', subsets: ['latin'] })
const roboto = Roboto({
weight: '100',
style: 'italic',
display: 'swap',
preload: true,
variable: '--roboto-100-italic',
subsets: ['latin'],
})
const myFont = localFont({
src: '../fonts/my-font.woff2',
preload: false,
variable: '--my-font',
subsets: ['latin'],
})
export default function WithFonts() {
return (
<>
{/* Fira Code Variable */}
<div
id="variables-fira-code"
className={firaCode.variable}
style={{ fontFamily: 'var(--fira-code)' }}
>
With variables
</div>
<div
id="without-variables-fira-code"
style={{ fontFamily: 'var(--fira-code)' }}
>
Without variables
</div>
{/* Roboto 100 Italic */}
<div
id="variables-roboto-100-italic"
className={roboto.variable}
style={{ fontFamily: 'var(--roboto-100-italic)' }}
>
With variables
</div>
<div
id="without-variables-roboto-100-italic"
style={{ fontFamily: 'var(--roboto-100-italic)' }}
>
Without variables
</div>
{/* Local font */}
<div
id="variables-local-font"
className={myFont.variable}
style={{ fontFamily: 'var(--my-font)' }}
>
With variables
</div>
<div
id="without-variables-local-font"
style={{ fontFamily: 'var(--my-font)' }}
>
Without variables
</div>
</>
)
}
import localFont from '@next/font/local'
import { Open_Sans } from '@next/font/google'
const openSans = Open_Sans({
fallback: ['system-ui', 'Arial'],
variable: '--open-sans',
adjustFontFallback: false,
subsets: ['latin'],
})
const myFont = localFont({
fallback: ['system-ui', 'Arial'],
src: '../fonts/my-font.woff2',
adjustFontFallback: false,
})
export default function WithFonts() {
return (
<>
<div id="with-fallback-fonts-classname" className={openSans.className}>
{JSON.stringify(openSans)}
</div>
<div id="with-fallback-fonts-style" style={openSans.style}>
{JSON.stringify(openSans)}
</div>
<div
id="with-fallback-fonts-variable"
style={{ fontFamily: 'var(--open-sans)' }}
className={openSans.variable}
>
{JSON.stringify(openSans)}
</div>
<div id="with-fallback-local" className={myFont.className}>
{JSON.stringify(myFont)}
</div>
</>
)
}
import CompWithFonts from '../components/CompWithFonts'
import { openSans } from './_app'
export default function WithFonts() {
return (
<>
<CompWithFonts />
<div id="with-fonts-open-sans" className={openSans.className}>
{JSON.stringify(openSans)}
</div>
<div id="with-fonts-open-sans-style" style={openSans.style} />
</>
)
}
import { Fraunces, Indie_Flower, Roboto } from '@next/font/google'
const indieFlower = Indie_Flower({
weight: '400',
preload: false,
subsets: ['latin'],
})
const fraunces = Fraunces({ weight: '400', preload: false, subsets: ['latin'] })
const robotoMultiple = Roboto({
weight: ['900', '100'],
style: ['normal', 'italic'],
subsets: ['latin'],
})
const frauncesMultiple = Fraunces({
style: ['italic', 'normal'],
axes: ['SOFT', 'WONK', 'opsz'],
subsets: ['latin'],
})
const frauncesMultipleWeights = Fraunces({
weight: ['100', '400', '900'],
subsets: ['latin'],
})
export default function WithFonts() {
return (
<>
<div id="first-google-font" className={indieFlower.className}>
{JSON.stringify(indieFlower)}
</div>
<div id="second-google-font" className={fraunces.className}>
{JSON.stringify(fraunces)}
</div>
<div id="multiple-roboto" className={robotoMultiple.className}>
{JSON.stringify(robotoMultiple)}
</div>
<div id="multiple-fraunces" className={frauncesMultiple.className}>
{JSON.stringify(frauncesMultiple)}
</div>
<div
id="multiple-weights-fraunces"
className={frauncesMultipleWeights.className}
>
{JSON.stringify(frauncesMultipleWeights)}
</div>
</>
)
}
import localFont from '@next/font/local'
const myFont1 = localFont({
src: '../fonts/my-font.woff2',
style: 'italic',
weight: '100',
fallback: ['system-ui'],
adjustFontFallback: 'Times New Roman',
})
const myFont2 = localFont({
src: '../fonts/my-other-font.woff2',
preload: false,
variable: '--my-font',
})
const roboto = localFont({
preload: false,
src: [
{
path: '../fonts/roboto/roboto-100-italic.woff2',
weight: '100',
style: 'italic',
},
{
path: '../fonts/my-font.woff2',
weight: '100',
style: 'normal',
},
{
path: '../fonts/my-font.woff2',
weight: '400',
style: 'italic',
},
{
path: '../fonts/roboto/roboto-400.woff2',
weight: '400',
},
{
path: '../fonts/my-font.woff2',
style: 'italic',
},
{
path: '../fonts/my-font.woff2',
weight: '900',
style: 'normal',
},
],
})
const robotoVar1 = localFont({
preload: false,
src: [
{
path: '../fonts/roboto/roboto-400.woff2',
weight: '100 300',
style: 'normal',
},
{
path: '../fonts/my-font.woff2',
weight: '100 300',
style: 'italic',
},
{
path: '../fonts/my-font.woff2',
weight: '500 900',
style: 'italic',
},
{
path: '../fonts/my-font.woff2',
weight: '500 900',
style: 'normal',
},
],
})
const robotoVar2 = localFont({
preload: false,
src: [
{
path: '../fonts/roboto/roboto-900.woff2',
weight: '100 900',
style: 'italic',
},
{
path: '../fonts/roboto/roboto-400.woff2',
weight: '100 900',
style: 'normal',
},
{
path: '../fonts/my-font.woff2',
weight: '300 399',
style: 'italic',
},
{
path: '../fonts/my-font.woff2',
weight: '401 500',
style: 'normal',
},
],
})
const robotoWithPreload = localFont({
src: [
{
path: '../fonts/roboto/roboto-100.woff2',
weight: '100',
style: 'normal',
},
{
path: '../fonts/roboto/roboto-900-italic.woff2',
weight: '900',
style: 'italic',
},
{
path: '../fonts/roboto/roboto-100.woff2',
weight: '100',
style: 'normal',
},
{
path: '../fonts/roboto/roboto-100-italic.woff2',
weight: '900',
style: 'italic',
},
],
})
export default function WithFonts() {
return (
<>
<div id="first-local-font" className={myFont1.className}>
{JSON.stringify(myFont1)}
</div>
<div id="second-local-font" className={myFont2.className}>
{JSON.stringify(myFont2)}
</div>
<div id="roboto-local-font" className={roboto.className}>
{JSON.stringify(roboto)}
</div>
<div id="roboto-local-font-var1" className={robotoVar1.className}>
{JSON.stringify(robotoVar1)}
</div>
<div id="roboto-local-font-var2" className={robotoVar2.className}>
{JSON.stringify(robotoVar2)}
</div>
<div
id="roboto-local-font-preload"
className={robotoWithPreload.className}
>
{JSON.stringify(robotoWithPreload)}
</div>
</>
)
}
export default function WithoutFonts() {
return <p id="hello-world">Hello world</p>
}
This diff is collapsed.
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { findAllTelemetryEvents } from 'next-test-utils'
import { join } from 'path'
const mockedGoogleFontResponses = require.resolve(
'./google-font-mocked-responses.js'
)
describe('@next/font used telemetry', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'telemetry-old/pages')),
},
dependencies: {
'@next/font': 'canary',
},
env: {
NEXT_FONT_GOOGLE_MOCKED_RESPONSES: mockedGoogleFontResponses,
NEXT_TELEMETRY_DEBUG: '1',
},
})
})
afterAll(() => next.destroy())
it('should send @next/font/google and @next/font/local usage event', async () => {
const events = findAllTelemetryEvents(
next.cliOutput,
'NEXT_BUILD_FEATURE_USAGE'
)
expect(events).toContainEqual({
featureName: '@next/font/google',
invocationCount: 1,
})
expect(events).toContainEqual({
featureName: '@next/font/local',
invocationCount: 1,
})
})
})
describe('@next/font unused telemetry', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'telemetry/pages-unused')),
},
dependencies: {
'@next/font': 'canary',
},
env: {
NEXT_FONT_GOOGLE_MOCKED_RESPONSES: mockedGoogleFontResponses,
NEXT_TELEMETRY_DEBUG: '1',
},
})
})
afterAll(() => next.destroy())
it('should not send @next/font/google and @next/font/local usage event', async () => {
const events = findAllTelemetryEvents(
next.cliOutput,
'NEXT_BUILD_FEATURE_USAGE'
)
expect(events).toContainEqual({
featureName: '@next/font/google',
invocationCount: 0,
})
expect(events).toContainEqual({
featureName: '@next/font/local',
invocationCount: 0,
})
})
})
export default function Page() {
return <p>Hello world</p>
}
import localFont from '@next/font/local'
const myFont = localFont({ src: './my-font.woff2' })
function MyApp({ Component, pageProps }) {
return (
<div className={myFont.className}>
<Component {...pageProps} />
</div>
)
}
export default MyApp
import { Open_Sans } from '@next/font/google'
const openSans = Open_Sans({ subsets: ['latin'] })
export default function Page() {
return (
<>
<p>Hello world 1</p>
<p className={openSans.className}>Hello world 2</p>
</>
)
}
......@@ -2097,22 +2097,22 @@
},
"test/e2e/app-dir/next-font/next-font.test.ts": {
"passed": [
"app dir - next/font app app dir - next-font computed styles should have correct styles at /",
"app dir - next/font app app dir - next-font computed styles should have correct styles at /client",
"app dir - next/font app app dir - next-font import values should have correct values at /",
"app dir - next/font app app dir - next-font import values should have correct values at /client",
"app dir - next/font app app dir - next-font import values should transform code in node_modules",
"app dir - next/font app app dir - next-font navigation should not have duplicate preload tags on navigation",
"app dir - next/font app app dir - next-font preconnect should add preconnect when preloading is disabled in component",
"app dir - next/font app app dir - next-font preconnect should add preconnect when preloading is disabled in layout",
"app dir - next/font app app dir - next-font preconnect should not preconnect when css is used but no fonts",
"app dir - next/font app app dir - next-font preload should preload correctly with client components",
"app dir - next/font app app dir - next-font preload should preload correctly with layout using fonts",
"app dir - next/font app app dir - next-font preload should preload correctly with page using fonts"
],
"failed": [
"app dir - next/font app app dir - next-font preconnect should add preconnect when preloading is disabled in page",
"app dir - next/font app app dir - next-font preload should preload correctly with server components"
"app dir - next/font app dir - next-font computed styles should have correct styles at /",
"app dir - next/font app dir - next-font computed styles should have correct styles at /client",
"app dir - next/font app dir - next-font import values should have correct values at /",
"app dir - next/font app dir - next-font import values should have correct values at /client",
"app dir - next/font app dir - next-font import values should transform code in node_modules",
"app dir - next/font app dir - next-font navigation should not have duplicate preload tags on navigation",
"app dir - next/font app dir - next-font preconnect should add preconnect when preloading is disabled in component",
"app dir - next/font app dir - next-font preconnect should add preconnect when preloading is disabled in layout",
"app dir - next/font app dir - next-font preconnect should not preconnect when css is used but no fonts",
"app dir - next/font app dir - next-font preload should preload correctly with client components",
"app dir - next/font app dir - next-font preload should preload correctly with layout using fonts",
"app dir - next/font app dir - next-font preload should preload correctly with page using fonts"
],
"failed": [
"app dir - next/font app dir - next-font preconnect should add preconnect when preloading is disabled in page",
"app dir - next/font app dir - next-font preload should preload correctly with server components"
],
"pending": [],
"flakey": [],
......@@ -4290,15 +4290,15 @@
},
"test/e2e/app-dir/next-font/next-font.test.ts": {
"passed": [
"app dir - next/font app app dir - next-font computed styles should have correct styles at /",
"app dir - next/font app app dir - next-font computed styles should have correct styles at /client",
"app dir - next/font app app dir - next-font import values should have correct values at /",
"app dir - next/font app app dir - next-font import values should have correct values at /client",
"app dir - next/font app app dir - next-font import values should transform code in node_modules",
"app dir - next/font app app dir - next-font navigation should not have duplicate preload tags on navigation"
"app dir - next/font app dir - next-font computed styles should have correct styles at /",
"app dir - next/font app dir - next-font computed styles should have correct styles at /client",
"app dir - next/font app dir - next-font import values should have correct values at /",
"app dir - next/font app dir - next-font import values should have correct values at /client",
"app dir - next/font app dir - next-font import values should transform code in node_modules",
"app dir - next/font app dir - next-font navigation should not have duplicate preload tags on navigation"
],
"failed": [
"app dir - next/font app app dir - next-font Dev errors should recover on font loader error"
"app dir - next/font app dir - next-font Dev errors should recover on font loader error"
],
"pending": [],
"flakey": [],
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