Skip to content

Commit

Permalink
Split entrypoint/route handling into separate dev and prod versions
Browse files Browse the repository at this point in the history
Currently, for `handleEntrypoints`, `handlePagesErrorRoute`, `handleRouteType`, etc, both dev and prod use cases are combined into the same functions. This leads to a lot of branching, use-case-specific arguments, etc. for minimal opportunity for shared code.

In a following PR, entrypoint writing for prod will be done through a single napi call to rust, further branching the two versions.

This splits them formally, at the cost of duplicating a handful of lines in each case. Perhaps in the future we can develop a better system for sharing code, but this makes things far clearer and easier to maintain in this moment.

Test Plan: CI
  • Loading branch information
wbinnssmith committed Jan 22, 2025
1 parent 1d0c2bb commit 4c98342
Show file tree
Hide file tree
Showing 9 changed files with 627 additions and 417 deletions.
262 changes: 243 additions & 19 deletions packages/next/src/build/handle-entrypoints.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { CustomRoutes } from '../lib/load-custom-routes'
import {
processIssues,
type EntryIssuesMap,
} from '../server/dev/turbopack-utils'
import type { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader'
import type { TurbopackManifestLoader } from '../shared/lib/turbopack/manifest-loader'
import type {
TurbopackResult,
Entrypoints as RawEntrypoints,
RawEntrypoints,
Entrypoints,
PageRoute,
AppRoute,
} from './swc/types'
import type { Entrypoints } from '../server/dev/turbopack/types'
import * as Log from './output/log'
import { getEntryKey } from '../server/dev/turbopack/entry-key'
import { getEntryKey } from '../shared/lib/turbopack/entry-key'
import {
processIssues,
type EntryIssuesMap,
} from '../shared/lib/turbopack/utils'

export async function handleEntrypoints({
entrypoints,
Expand Down Expand Up @@ -98,21 +100,243 @@ export async function handleEntrypoints({
entrypoints: currentEntrypoints,
})

if (middleware) {
const key = getEntryKey('root', 'server', 'middleware')
if (middleware) {
const key = getEntryKey('root', 'server', 'middleware')

const endpoint = middleware.endpoint

async function processMiddleware() {
const writtenEndpoint = await endpoint.writeToDisk()
processIssues(
currentEntryIssues,
key,
writtenEndpoint,
false,
logErrors
)
await manifestLoader.loadMiddlewareManifest('middleware', 'middleware')
}
await processMiddleware()
} else {
manifestLoader.deleteMiddlewareManifest(
getEntryKey('root', 'server', 'middleware')
)
}
}
}

export async function handlePagesErrorRoute({
currentEntryIssues,
entrypoints,
manifestLoader,
productionRewrites,
logErrors,
}: {
currentEntryIssues: EntryIssuesMap
entrypoints: Entrypoints
manifestLoader: TurbopackManifestLoader
productionRewrites: CustomRoutes['rewrites'] | undefined
logErrors: boolean
}) {
if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await entrypoints.global.app.writeToDisk()
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
}
await manifestLoader.loadBuildManifest('_app')
await manifestLoader.loadPagesManifest('_app')
await manifestLoader.loadFontManifest('_app')

if (entrypoints.global.document) {
const key = getEntryKey('pages', 'server', '_document')
const writtenEndpoint = await entrypoints.global.document.writeToDisk()
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
}
await manifestLoader.loadPagesManifest('_document')

if (entrypoints.global.error) {
const key = getEntryKey('pages', 'server', '_error')
const writtenEndpoint = await entrypoints.global.error.writeToDisk()
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
}

await manifestLoader.loadBuildManifest('_error')
await manifestLoader.loadPagesManifest('_error')
await manifestLoader.loadFontManifest('_error')

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites,
entrypoints,
})
}

export async function handleRouteType({
page,
route,
currentEntryIssues,
entrypoints,
manifestLoader,
productionRewrites,
logErrors,
}: {
page: string
route: PageRoute | AppRoute

currentEntryIssues: EntryIssuesMap
entrypoints: Entrypoints
manifestLoader: TurbopackManifestLoader
productionRewrites: CustomRoutes['rewrites'] | undefined
logErrors: boolean
}) {
const shouldCreateWebpackStats = process.env.TURBOPACK_STATS != null

switch (route.type) {
case 'page': {
const serverKey = getEntryKey('pages', 'server', page)

if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_app')

const writtenEndpoint = await entrypoints.global.app.writeToDisk()
processIssues(
currentEntryIssues,
key,
writtenEndpoint,
false,
logErrors
)
}
await manifestLoader.loadBuildManifest('_app')
await manifestLoader.loadPagesManifest('_app')

if (entrypoints.global.document) {
const key = getEntryKey('pages', 'server', '_document')

const writtenEndpoint = await entrypoints.global.document.writeToDisk()
processIssues(
currentEntryIssues,
key,
writtenEndpoint,
false,
logErrors
)
}
await manifestLoader.loadPagesManifest('_document')

const writtenEndpoint = await route.htmlEndpoint.writeToDisk()

const type = writtenEndpoint?.type

await manifestLoader.loadBuildManifest(page)
await manifestLoader.loadPagesManifest(page)
if (type === 'edge') {
await manifestLoader.loadMiddlewareManifest(page, 'pages')
} else {
manifestLoader.deleteMiddlewareManifest(serverKey)
}
await manifestLoader.loadFontManifest('/_app', 'pages')
await manifestLoader.loadFontManifest(page, 'pages')

if (shouldCreateWebpackStats) {
await manifestLoader.loadWebpackStats(page, 'pages')
}

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites,
entrypoints,
})

processIssues(
currentEntryIssues,
serverKey,
writtenEndpoint,
false,
logErrors
)

break
}
case 'page-api': {
const key = getEntryKey('pages', 'server', page)

const endpoint = middleware.endpoint
const writtenEndpoint = await route.endpoint.writeToDisk()

const type = writtenEndpoint.type

await manifestLoader.loadPagesManifest(page)
if (type === 'edge') {
await manifestLoader.loadMiddlewareManifest(page, 'pages')
} else {
manifestLoader.deleteMiddlewareManifest(key)
}

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites,
entrypoints,
})

processIssues(currentEntryIssues, key, writtenEndpoint, true, logErrors)

break
}
case 'app-page': {
const key = getEntryKey('app', 'server', page)
const writtenEndpoint = await route.htmlEndpoint.writeToDisk()
const type = writtenEndpoint.type

if (type === 'edge') {
await manifestLoader.loadMiddlewareManifest(page, 'app')
} else {
manifestLoader.deleteMiddlewareManifest(key)
}

await manifestLoader.loadAppBuildManifest(page)
await manifestLoader.loadBuildManifest(page, 'app')
await manifestLoader.loadAppPathsManifest(page)
await manifestLoader.loadActionManifest(page)
await manifestLoader.loadFontManifest(page, 'app')

if (shouldCreateWebpackStats) {
await manifestLoader.loadWebpackStats(page, 'app')
}

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites,
entrypoints,
})

async function processMiddleware() {
const writtenEndpoint = await endpoint.writeToDisk()
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
await manifestLoader.loadMiddlewareManifest('middleware', 'middleware')

break
}
await processMiddleware()
case 'app-route': {
const key = getEntryKey('app', 'server', page)
const writtenEndpoint = await route.endpoint.writeToDisk()
const type = writtenEndpoint.type

} else {
manifestLoader.deleteMiddlewareManifest(
getEntryKey('root', 'server', 'middleware')
)
await manifestLoader.loadAppPathsManifest(page)

if (type === 'edge') {
await manifestLoader.loadMiddlewareManifest(page, 'app')
} else {
manifestLoader.deleteMiddlewareManifest(key)
}

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites,
entrypoints,
})
processIssues(currentEntryIssues, key, writtenEndpoint, true, logErrors)

break
}
default: {
throw new Error(`unknown route type ${(route as any).type} for ${page}`)
}
}
}
36 changes: 14 additions & 22 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,7 @@ import {
import { getStartServerInfo, logStartInfo } from '../server/lib/app-info-log'
import type { NextEnabledDirectories } from '../server/base-server'
import { hasCustomExportOutput } from '../export/utils'
import {
getTurbopackJsConfig,
type EntryIssuesMap,
handleRouteType,
handlePagesErrorRoute,
formatIssue,
isRelevantWarning,
isPersistentCachingEnabled,
} from '../server/dev/turbopack-utils'
import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader'
import type { Entrypoints } from '../server/dev/turbopack/types'
import { TurbopackManifestLoader } from '../shared/lib/turbopack/manifest-loader'
import { buildCustomRoute } from '../lib/build-custom-route'
import { createProgress } from './progress'
import { traceMemoryUsage } from '../lib/memory/trace'
Expand All @@ -216,7 +206,19 @@ import {
} from '../server/lib/utils'
import { InvariantError } from '../shared/lib/invariant-error'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
import { handleEntrypoints } from './handle-entrypoints'
import {
handleEntrypoints,
handlePagesErrorRoute,
handleRouteType,
} from './handle-entrypoints'
import type { Entrypoints } from './swc/types'
import {
formatIssue,
getTurbopackJsConfig,
isPersistentCachingEnabled,
isRelevantWarning,
type EntryIssuesMap,
} from '../shared/lib/turbopack/utils'

type Fallback = null | boolean | string

Expand Down Expand Up @@ -1488,7 +1490,6 @@ export default async function build(
currentEntrypoints,
currentEntryIssues,
manifestLoader,
devRewrites: undefined,
productionRewrites: customRoutes.rewrites,
logErrors: false,
})
Expand Down Expand Up @@ -1529,15 +1530,11 @@ export default async function build(
for (const [page, route] of currentEntrypoints.page) {
enqueue(() =>
handleRouteType({
dev,
page,
pathname: page,
route,

currentEntryIssues,
entrypoints: currentEntrypoints,
manifestLoader,
devRewrites: undefined,
productionRewrites: customRoutes.rewrites,
logErrors: false,
})
Expand All @@ -1549,13 +1546,10 @@ export default async function build(
enqueue(() =>
handleRouteType({
page,
dev: false,
pathname: normalizeAppPath(page),
route,
currentEntryIssues,
entrypoints: currentEntrypoints,
manifestLoader,
devRewrites: undefined,
productionRewrites: customRoutes.rewrites,
logErrors: false,
})
Expand All @@ -1564,11 +1558,9 @@ export default async function build(

enqueue(() =>
handlePagesErrorRoute({
dev: false,
currentEntryIssues,
entrypoints: currentEntrypoints,
manifestLoader,
devRewrites: undefined,
productionRewrites: customRoutes.rewrites,
logErrors: false,
})
Expand Down
Loading

0 comments on commit 4c98342

Please sign in to comment.