Unverified Commit e1a2d132 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure Turbopack writes font optimization manifest (#62079)

## What?

Even though we don't support the legacy font optimization (what came
before `next/font`) the manifest is read during builds, so we have to
write an empty version of it.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2498
parent bf5ddd4c
......@@ -50,7 +50,7 @@ import {
CLIENT_STATIC_FILES_PATH,
EXPORT_DETAIL,
EXPORT_MARKER,
FONT_MANIFEST,
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST,
IMAGES_MANIFEST,
PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD,
......@@ -182,9 +182,9 @@ import {
type LoadableManifests,
handleRouteType,
writeManifests,
handlePagesErrorRoute,
} from '../server/dev/turbopack-utils'
import { buildCustomRoute } from '../lib/build-custom-route'
import type { FontManifest } from '../server/font-utils'
interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[]
......@@ -1300,7 +1300,10 @@ export default async function build(
: []),
REACT_LOADABLE_MANIFEST,
config.optimizeFonts
? path.join(SERVER_DIRECTORY, FONT_MANIFEST)
? path.join(
SERVER_DIRECTORY,
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST
)
: null,
BUILD_ID_FILE,
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.js'),
......@@ -1326,10 +1329,18 @@ export default async function build(
return serverFilesManifest
})
async function turbopackBuild(): Promise<never> {
async function turbopackBuild(): Promise<{
duration: number
buildTraceContext: undefined
}> {
if (!process.env.TURBOPACK || !process.env.TURBOPACK_BUILD) {
throw new Error("next build doesn't support turbopack yet")
}
// TODO: Without NODE_ENV=development React will error that the RSC payload was rendered using development React while renderToHTML is called on the production React.
// This is caused by Turbopack not having the production build option yet.
// @ts-expect-error
process.env.NODE_ENV = 'development'
const startTime = process.hrtime()
const bindings = await loadBindings(config?.experimental?.useWasmBinary)
const project = await bindings.turbo.createProject({
projectPath: dir,
......@@ -1354,7 +1365,7 @@ export default async function build(
})
await fs.mkdir(path.join(distDir, 'server'), { recursive: true })
await fs.mkdir(path.join(distDir, 'static/development'), {
await fs.mkdir(path.join(distDir, 'static', buildId), {
recursive: true,
})
await fs.writeFile(
......@@ -1402,6 +1413,7 @@ export default async function build(
serverFields: undefined,
propagateServerField: undefined,
distDir,
buildId,
globalEntrypoints,
currentEntrypoints,
changeSubscriptions: undefined,
......@@ -1428,6 +1440,7 @@ export default async function build(
handleRouteType({
rewrites: emptyRewritesObjToBeImplemented,
distDir,
buildId,
globalEntrypoints,
currentIssues,
buildManifests,
......@@ -1447,12 +1460,34 @@ export default async function build(
})
)
}
promises.push(
handlePagesErrorRoute({
rewrites: emptyRewritesObjToBeImplemented,
globalEntrypoints,
currentIssues,
distDir,
buildId,
buildManifests,
pagesManifests,
fontManifests,
appBuildManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
loadableManifests,
currentEntrypoints,
handleRequireCacheClearing: undefined,
changeSubscription: undefined,
})
)
await Promise.all(promises)
break
}
await writeManifests({
rewrites: emptyRewritesObjToBeImplemented,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -1463,23 +1498,10 @@ export default async function build(
loadableManifests,
currentEntrypoints,
})
// Temporary
await writeBuildId(distDir, buildId)
const prerenderManifest: Readonly<PrerenderManifest> = {
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: previewProps,
return {
duration: process.hrtime(startTime)[0],
buildTraceContext: undefined,
}
await writePrerenderManifest(distDir, prerenderManifest)
await writeManifest<FontManifest>(
path.join(distDir, 'server', FONT_MANIFEST),
[]
)
// End temporary
throw new Error("next build doesn't support turbopack yet")
}
let buildTraceContext: undefined | BuildTraceContext
let buildTracesPromise: Promise<any> | undefined = undefined
......@@ -1578,7 +1600,7 @@ export default async function build(
})
)
} else {
const { duration: webpackBuildDuration, ...rest } = turboNextBuild
const { duration: compilerDuration, ...rest } = turboNextBuild
? await turbopackBuild()
: await webpackBuild(useBuildWorker, null)
......@@ -1586,7 +1608,7 @@ export default async function build(
telemetry.record(
eventBuildCompleted(pagesPaths, {
durationInSeconds: webpackBuildDuration,
durationInSeconds: compilerDuration,
totalAppPagesCount,
})
)
......
......@@ -11,7 +11,7 @@ import type { FontManifest } from '../../../server/font-utils'
import postcss from 'postcss'
import minifier from 'next/dist/compiled/cssnano-simple'
import {
FONT_MANIFEST,
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST,
OPTIMIZED_FONT_PROVIDERS,
} from '../../../shared/lib/constants'
import * as Log from '../../output/log'
......@@ -151,7 +151,7 @@ export class FontStylesheetGatheringPlugin {
if (buildInfo) {
buildInfo.valueDependencies.set(
FONT_MANIFEST,
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST,
this.gatheredStylesheets
)
}
......@@ -187,8 +187,9 @@ export class FontStylesheetGatheringPlugin {
const fontUrls = new Set<string>()
modules.forEach((module: any) => {
const fontDependencies =
module?.buildInfo?.valueDependencies?.get(FONT_MANIFEST)
const fontDependencies = module?.buildInfo?.valueDependencies?.get(
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST
)
if (fontDependencies) {
fontDependencies.forEach((v: string) => fontUrls.add(v))
}
......@@ -229,9 +230,10 @@ export class FontStylesheetGatheringPlugin {
}
// @ts-expect-error invalid assets type
compilation.assets[FONT_MANIFEST] = new sources.RawSource(
JSON.stringify(this.manifestContent, null, ' ')
)
compilation.assets[AUTOMATIC_FONT_OPTIMIZATION_MANIFEST] =
new sources.RawSource(
JSON.stringify(this.manifestContent, null, ' ')
)
modulesFinished()
}
......@@ -246,9 +248,10 @@ export class FontStylesheetGatheringPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets: any) => {
assets['../' + FONT_MANIFEST] = new sources.RawSource(
JSON.stringify(this.manifestContent, null, ' ')
)
assets['../' + AUTOMATIC_FONT_OPTIMIZATION_MANIFEST] =
new sources.RawSource(
JSON.stringify(this.manifestContent, null, ' ')
)
}
)
})
......
......@@ -48,9 +48,6 @@ import {
type FontManifests,
type LoadableManifests,
writeManifests,
loadBuildManifest,
loadPagesManifest,
loadFontManifest,
type CurrentEntrypoints,
type CurrentIssues,
processIssues,
......@@ -67,6 +64,7 @@ import {
type SendHmr,
type StartBuilding,
type ChangeSubscriptions,
handlePagesErrorRoute,
} from './turbopack-utils'
import {
propagateServerField,
......@@ -86,6 +84,7 @@ export async function createHotReloaderTurbopack(
serverFields: ServerFields,
distDir: string
): Promise<NextJsHotReloaderInterface> {
const buildId = 'development'
const { nextConfig, dir } = opts
const { loadBindings } =
......@@ -389,6 +388,7 @@ export async function createHotReloaderTurbopack(
await propagateServerField(opts, key, value)
},
distDir,
buildId,
globalEntrypoints,
currentEntrypoints,
changeSubscriptions,
......@@ -424,7 +424,7 @@ export async function createHotReloaderTurbopack(
// Write empty manifests
await mkdir(join(distDir, 'server'), { recursive: true })
await mkdir(join(distDir, 'static/development'), { recursive: true })
await mkdir(join(distDir, 'static', buildId), { recursive: true })
await writeFile(
join(distDir, 'package.json'),
JSON.stringify(
......@@ -439,6 +439,7 @@ export async function createHotReloaderTurbopack(
await writeManifests({
rewrites: opts.fsChecker.rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -649,53 +650,23 @@ export async function createHotReloaderTurbopack(
if (page === '/_error') {
let finishBuilding = startBuilding(page, requestUrl, false)
try {
if (globalEntrypoints.app) {
const writtenEndpoint = await globalEntrypoints.app.writeToDisk()
handleRequireCacheClearing('_app', writtenEndpoint)
processIssues(currentIssues, '_app', writtenEndpoint)
}
await loadBuildManifest(distDir, buildManifests, '_app')
await loadPagesManifest(distDir, pagesManifests, '_app')
await loadFontManifest(distDir, fontManifests, '_app')
if (globalEntrypoints.document) {
const writtenEndpoint =
await globalEntrypoints.document.writeToDisk()
handleRequireCacheClearing('_document', writtenEndpoint)
changeSubscription(
'_document',
'server',
false,
globalEntrypoints.document,
() => {
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
}
)
processIssues(currentIssues, '_document', writtenEndpoint)
}
await loadPagesManifest(distDir, pagesManifests, '_document')
if (globalEntrypoints.error) {
const writtenEndpoint = await globalEntrypoints.error.writeToDisk()
handleRequireCacheClearing('_error', writtenEndpoint)
processIssues(currentIssues, page, writtenEndpoint)
}
await loadBuildManifest(distDir, buildManifests, '_error')
await loadPagesManifest(distDir, pagesManifests, '_error')
await loadFontManifest(distDir, fontManifests, '_error')
await writeManifests({
await handlePagesErrorRoute({
rewrites: opts.fsChecker.rewrites,
globalEntrypoints,
currentIssues,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
fontManifests,
appBuildManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
currentEntrypoints,
handleRequireCacheClearing,
changeSubscription,
})
} finally {
finishBuilding()
......@@ -735,6 +706,7 @@ export async function createHotReloaderTurbopack(
await handleRouteType({
rewrites: opts.fsChecker.rewrites,
distDir,
buildId,
globalEntrypoints,
currentIssues,
buildManifests,
......
......@@ -26,6 +26,7 @@ import {
MIDDLEWARE_BUILD_MANIFEST,
INTERCEPTION_ROUTE_REWRITE_MANIFEST,
MIDDLEWARE_REACT_LOADABLE_MANIFEST,
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST,
} from '../../shared/lib/constants'
import { writeFileAtomic } from '../../lib/fs/write-atomic'
import { deleteCache } from '../../build/webpack/plugins/nextjs-require-cache-hot-reloader'
......@@ -289,7 +290,7 @@ export async function loadMiddlewareManifest(
)
}
export async function loadBuildManifest(
async function loadBuildManifest(
distDir: string,
buildManifests: BuildManifests,
pageName: string,
......@@ -312,7 +313,7 @@ async function loadAppBuildManifest(
)
}
export async function loadPagesManifest(
async function loadPagesManifest(
distDir: string,
pagesManifests: PagesManifests,
pageName: string
......@@ -351,7 +352,7 @@ async function loadActionManifest(
)
}
export async function loadFontManifest(
async function loadFontManifest(
distDir: string,
fontManifests: FontManifests,
pageName: string,
......@@ -382,6 +383,7 @@ async function loadLoadableManifest(
async function writeBuildManifest(
distDir: string,
buildId: string,
buildManifests: BuildManifests,
currentEntrypoints: CurrentEntrypoints,
rewrites: SetupOpts['fsChecker']['rewrites']
......@@ -437,11 +439,11 @@ async function writeBuildManifest(
content
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
await writeFileAtomic(
join(distDir, 'static', 'development', '_buildManifest.js'),
join(distDir, 'static', buildId, '_buildManifest.js'),
buildManifestJs
)
await writeFileAtomic(
join(distDir, 'static', 'development', '_ssgManifest.js'),
join(distDir, 'static', buildId, '_ssgManifest.js'),
srcEmptySsgManifest
)
}
......@@ -543,7 +545,7 @@ async function writeActionManifest(
)
}
async function writeFontManifest(
async function writeNextFontManifest(
distDir: string,
fontManifests: FontManifests
): Promise<void> {
......@@ -565,6 +567,19 @@ async function writeFontManifest(
)
}
/**
* Turbopack doesn't support this functionality, so it writes an empty manifest.
*/
async function writeAutomaticFontOptimizationManifest(distDir: string) {
const manifestPath = join(
distDir,
'server',
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST
)
await writeFileAtomic(manifestPath, JSON.stringify([]))
}
async function writeLoadableManifest(
distDir: string,
loadableManifests: LoadableManifests
......@@ -591,6 +606,7 @@ async function writeLoadableManifest(
export async function writeManifests({
rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -603,6 +619,7 @@ export async function writeManifests({
}: {
rewrites: SetupOpts['fsChecker']['rewrites']
distDir: string
buildId: string
buildManifests: BuildManifests
appBuildManifests: AppBuildManifests
pagesManifests: PagesManifests
......@@ -615,6 +632,7 @@ export async function writeManifests({
}): Promise<void> {
await writeBuildManifest(
distDir,
buildId,
buildManifests,
currentEntrypoints,
rewrites
......@@ -624,9 +642,10 @@ export async function writeManifests({
await writeAppPathsManifest(distDir, appPathsManifests)
await writeMiddlewareManifest(distDir, middlewareManifests)
await writeActionManifest(distDir, actionManifests)
await writeFontManifest(distDir, fontManifests)
await writeNextFontManifest(distDir, fontManifests)
await writeLoadableManifest(distDir, loadableManifests)
await writeFallbackBuildManifest(distDir, buildManifests)
await writeAutomaticFontOptimizationManifest(distDir)
}
class ModuleBuildError extends Error {}
......@@ -816,6 +835,7 @@ export type ReadyIds = Set<string>
export async function handleRouteType({
rewrites,
distDir,
buildId,
globalEntrypoints,
currentIssues,
buildManifests,
......@@ -835,6 +855,7 @@ export async function handleRouteType({
}: {
rewrites: SetupOpts['fsChecker']['rewrites']
distDir: string
buildId: string
globalEntrypoints: GlobalEntrypoints
currentIssues: CurrentIssues
buildManifests: BuildManifests
......@@ -893,6 +914,7 @@ export async function handleRouteType({
await writeManifests({
rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -962,6 +984,7 @@ export async function handleRouteType({
await writeManifests({
rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -1016,6 +1039,7 @@ export async function handleRouteType({
await writeManifests({
rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -1052,6 +1076,7 @@ export async function handleRouteType({
await writeManifests({
rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -1079,6 +1104,7 @@ export async function handleEntrypoints({
serverFields,
propagateServerField,
distDir,
buildId,
globalEntrypoints,
currentEntrypoints,
changeSubscriptions,
......@@ -1106,6 +1132,7 @@ export async function handleEntrypoints({
| ((field: PropagateToWorkersField, args: any) => Promise<void>)
| undefined
distDir: string
buildId: string
globalEntrypoints: GlobalEntrypoints
currentEntrypoints: CurrentEntrypoints
changeSubscriptions: ChangeSubscriptions | undefined
......@@ -1207,6 +1234,7 @@ export async function handleEntrypoints({
await writeManifests({
rewrites: rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -1274,6 +1302,7 @@ export async function handleEntrypoints({
await writeManifests({
rewrites: rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
......@@ -1306,3 +1335,88 @@ export async function handleEntrypoints({
await propagateServerField('middleware', serverFields.middleware)
}
}
export async function handlePagesErrorRoute({
rewrites,
globalEntrypoints,
currentIssues,
distDir,
buildId,
buildManifests,
pagesManifests,
fontManifests,
appBuildManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
loadableManifests,
currentEntrypoints,
handleRequireCacheClearing,
changeSubscription,
}: {
rewrites: SetupOpts['fsChecker']['rewrites']
globalEntrypoints: GlobalEntrypoints
currentIssues: CurrentIssues
distDir: string
buildId: string
buildManifests: BuildManifests
pagesManifests: PagesManifests
fontManifests: FontManifests
appBuildManifests: AppBuildManifests
appPathsManifests: AppPathsManifests
middlewareManifests: MiddlewareManifests
actionManifests: ActionManifests
loadableManifests: LoadableManifests
currentEntrypoints: CurrentEntrypoints
handleRequireCacheClearing: HandleRequireCacheClearing | undefined
changeSubscription: ChangeSubscription | undefined
}) {
if (globalEntrypoints.app) {
const writtenEndpoint = await globalEntrypoints.app.writeToDisk()
handleRequireCacheClearing?.('_app', writtenEndpoint)
processIssues(currentIssues, '_app', writtenEndpoint)
}
await loadBuildManifest(distDir, buildManifests, '_app')
await loadPagesManifest(distDir, pagesManifests, '_app')
await loadFontManifest(distDir, fontManifests, '_app')
if (globalEntrypoints.document) {
const writtenEndpoint = await globalEntrypoints.document.writeToDisk()
handleRequireCacheClearing?.('_document', writtenEndpoint)
changeSubscription?.(
'_document',
'server',
false,
globalEntrypoints.document,
() => {
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
}
)
processIssues(currentIssues, '_document', writtenEndpoint)
}
await loadPagesManifest(distDir, pagesManifests, '_document')
if (globalEntrypoints.error) {
const writtenEndpoint = await globalEntrypoints.error.writeToDisk()
handleRequireCacheClearing?.('_error', writtenEndpoint)
processIssues(currentIssues, '/_error', writtenEndpoint)
}
await loadBuildManifest(distDir, buildManifests, '_error')
await loadPagesManifest(distDir, pagesManifests, '_error')
await loadFontManifest(distDir, fontManifests, '_error')
await writeManifests({
rewrites,
distDir,
buildId,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
currentEntrypoints,
})
}
import path from 'path'
import {
FONT_MANIFEST,
AUTOMATIC_FONT_OPTIMIZATION_MANIFEST,
PAGES_MANIFEST,
SERVER_DIRECTORY,
APP_PATHS_MANIFEST,
......@@ -134,7 +134,7 @@ export function requirePage(
export function requireFontManifest(distDir: string) {
const serverBuildPath = path.join(distDir, SERVER_DIRECTORY)
const fontManifest = loadManifest(
path.join(serverBuildPath, FONT_MANIFEST)
path.join(serverBuildPath, AUTOMATIC_FONT_OPTIMIZATION_MANIFEST)
) as FontManifest
return fontManifest
}
......@@ -57,7 +57,7 @@ export const DEV_CLIENT_PAGES_MANIFEST = '_devPagesManifest.json'
export const MIDDLEWARE_MANIFEST = 'middleware-manifest.json'
export const DEV_MIDDLEWARE_MANIFEST = '_devMiddlewareManifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
export const FONT_MANIFEST = 'font-manifest.json'
export const AUTOMATIC_FONT_OPTIMIZATION_MANIFEST = 'font-manifest.json'
export const SERVER_DIRECTORY = 'server'
export const CONFIG_FILES = ['next.config.js', 'next.config.mjs']
export const BUILD_ID_FILE = 'BUILD_ID'
......
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