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

More hot-reloader-turbopack refactors (#62055)

## What?

Follow-up to #61993

More code moved to be used for builds in the future.

<!-- 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-2492
parent 9d495499
...@@ -166,8 +166,25 @@ import { hasCustomExportOutput } from '../export/utils' ...@@ -166,8 +166,25 @@ import { hasCustomExportOutput } from '../export/utils'
import { interopDefault } from '../lib/interop-default' import { interopDefault } from '../lib/interop-default'
import { formatDynamicImportPath } from '../lib/format-dynamic-import-path' import { formatDynamicImportPath } from '../lib/format-dynamic-import-path'
import { isInterceptionRouteAppPath } from '../server/future/helpers/interception-routes' import { isInterceptionRouteAppPath } from '../server/future/helpers/interception-routes'
import { getTurbopackJsConfig } from '../server/dev/turbopack-utils' import {
getTurbopackJsConfig,
handleEntrypoints,
type GlobalEntrypoints,
type CurrentEntrypoints,
type CurrentIssues,
type BuildManifests,
type AppBuildManifests,
type PagesManifests,
type AppPathsManifests,
type MiddlewareManifests,
type ActionManifests,
type FontManifests,
type LoadableManifests,
handleRouteType,
writeManifests,
} from '../server/dev/turbopack-utils'
import { buildCustomRoute } from '../lib/build-custom-route' import { buildCustomRoute } from '../lib/build-custom-route'
import type { FontManifest } from '../server/font-utils'
interface ExperimentalBypassForInfo { interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[] experimentalBypassFor?: RouteHas[]
...@@ -1317,7 +1334,7 @@ export default async function build( ...@@ -1317,7 +1334,7 @@ export default async function build(
const project = await bindings.turbo.createProject({ const project = await bindings.turbo.createProject({
projectPath: dir, projectPath: dir,
rootPath: config.experimental.outputFileTracingRoot || dir, rootPath: config.experimental.outputFileTracingRoot || dir,
nextConfig: config.nextConfig, nextConfig: config,
jsConfig: await getTurbopackJsConfig(dir, config), jsConfig: await getTurbopackJsConfig(dir, config),
watch: false, watch: false,
env: process.env as Record<string, string>, env: process.env as Record<string, string>,
...@@ -1326,7 +1343,8 @@ export default async function build( ...@@ -1326,7 +1343,8 @@ export default async function build(
allowedRevalidateHeaderKeys: undefined, allowedRevalidateHeaderKeys: undefined,
clientRouterFilters: undefined, clientRouterFilters: undefined,
config, config,
dev: false, // When passing `false` you get `react.jsxDEV is not a function`
dev: true,
distDir, distDir,
fetchCacheKeyPrefix: undefined, fetchCacheKeyPrefix: undefined,
hasRewrites, hasRewrites,
...@@ -1335,10 +1353,132 @@ export default async function build( ...@@ -1335,10 +1353,132 @@ export default async function build(
}), }),
}) })
await fs.mkdir(path.join(distDir, 'server'), { recursive: true })
await fs.mkdir(path.join(distDir, 'static/development'), {
recursive: true,
})
await fs.writeFile(
path.join(distDir, 'package.json'),
JSON.stringify(
{
type: 'commonjs',
},
null,
2
)
)
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const entrypointsSubscription = project.entrypointsSubscribe() const entrypointsSubscription = project.entrypointsSubscribe()
// for await (const entrypoints of entrypointsSubscription) { const globalEntrypoints: GlobalEntrypoints = {
// } app: undefined,
document: undefined,
error: undefined,
}
const currentEntrypoints: CurrentEntrypoints = new Map()
const currentIssues: CurrentIssues = new Map()
const buildManifests: BuildManifests = new Map()
const appBuildManifests: AppBuildManifests = new Map()
const pagesManifests: PagesManifests = new Map()
const appPathsManifests: AppPathsManifests = new Map()
const middlewareManifests: MiddlewareManifests = new Map()
const actionManifests: ActionManifests = new Map()
const fontManifests: FontManifests = new Map()
const loadableManifests: LoadableManifests = new Map()
// TODO: implement this
const emptyRewritesObjToBeImplemented = {
beforeFiles: [],
afterFiles: [],
fallback: [],
}
for await (const entrypoints of entrypointsSubscription) {
await handleEntrypoints({
rewrites: emptyRewritesObjToBeImplemented,
nextConfig: config,
entrypoints,
serverFields: undefined,
propagateServerField: undefined,
distDir,
globalEntrypoints,
currentEntrypoints,
changeSubscriptions: undefined,
changeSubscription: undefined,
clearChangeSubscription: undefined,
sendHmr: undefined,
startBuilding: undefined,
handleRequireCacheClearing: undefined,
prevMiddleware: undefined,
currentIssues,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
})
const promises = []
for (const [page, route] of currentEntrypoints) {
promises.push(
handleRouteType({
rewrites: emptyRewritesObjToBeImplemented,
distDir,
globalEntrypoints,
currentIssues,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
currentEntrypoints,
handleRequireCacheClearing: undefined,
changeSubscription: undefined,
readyIds: undefined,
page,
route,
})
)
}
await Promise.all(promises)
break
}
await writeManifests({
rewrites: emptyRewritesObjToBeImplemented,
distDir,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
currentEntrypoints,
})
// Temporary
await writeBuildId(distDir, buildId)
const prerenderManifest: Readonly<PrerenderManifest> = {
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: previewProps,
}
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") throw new Error("next build doesn't support turbopack yet")
} }
let buildTraceContext: undefined | BuildTraceContext let buildTraceContext: undefined | BuildTraceContext
......
...@@ -34,7 +34,10 @@ import { ...@@ -34,7 +34,10 @@ import {
type ClientBuildManifest, type ClientBuildManifest,
srcEmptySsgManifest, srcEmptySsgManifest,
} from '../../build/webpack/plugins/build-manifest-plugin' } from '../../build/webpack/plugins/build-manifest-plugin'
import type { SetupOpts } from '../lib/router-utils/setup-dev-bundler' import type {
ServerFields,
SetupOpts,
} from '../lib/router-utils/setup-dev-bundler'
import { isInterceptionRouteRewrite } from '../../lib/generate-interception-routes-rewrites' import { isInterceptionRouteRewrite } from '../../lib/generate-interception-routes-rewrites'
import type { import type {
Issue, Issue,
...@@ -43,6 +46,7 @@ import type { ...@@ -43,6 +46,7 @@ import type {
StyledString, StyledString,
Endpoint, Endpoint,
WrittenEndpoint, WrittenEndpoint,
Entrypoints,
} from '../../build/swc' } from '../../build/swc'
import { import {
MAGIC_IDENTIFIER_REGEX, MAGIC_IDENTIFIER_REGEX,
...@@ -53,6 +57,8 @@ import { ...@@ -53,6 +57,8 @@ import {
HMR_ACTIONS_SENT_TO_BROWSER, HMR_ACTIONS_SENT_TO_BROWSER,
type HMR_ACTION_TYPES, type HMR_ACTION_TYPES,
} from './hot-reloader-types' } from './hot-reloader-types'
import * as Log from '../../build/output/log'
import type { PropagateToWorkersField } from '../lib/router-utils/types'
export interface InstrumentationDefinition { export interface InstrumentationDefinition {
files: string[] files: string[]
...@@ -266,6 +272,10 @@ export type ActionManifests = Map<string, ActionManifest> ...@@ -266,6 +272,10 @@ export type ActionManifests = Map<string, ActionManifest>
export type FontManifests = Map<string, NextFontManifest> export type FontManifests = Map<string, NextFontManifest>
export type LoadableManifests = Map<string, LoadableManifest> export type LoadableManifests = Map<string, LoadableManifest>
export type CurrentEntrypoints = Map<string, Route> export type CurrentEntrypoints = Map<string, Route>
export type ChangeSubscriptions = Map<
string,
Promise<AsyncIterableIterator<TurbopackResult>>
>
export async function loadMiddlewareManifest( export async function loadMiddlewareManifest(
distDir: string, distDir: string,
...@@ -788,6 +798,19 @@ export type ChangeSubscription = ( ...@@ -788,6 +798,19 @@ export type ChangeSubscription = (
) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void ) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void
) => Promise<void> ) => Promise<void>
export type ClearChangeSubscription = (
page: string,
type: 'server' | 'client'
) => Promise<void>
export type SendHmr = (id: string, payload: HMR_ACTION_TYPES) => void
export type StartBuilding = (
id: string,
requestUrl: string | undefined,
forceRebuild: boolean
) => () => void
export type ReadyIds = Set<string> export type ReadyIds = Set<string>
export async function handleRouteType({ export async function handleRouteType({
...@@ -823,9 +846,9 @@ export async function handleRouteType({ ...@@ -823,9 +846,9 @@ export async function handleRouteType({
fontManifests: FontManifests fontManifests: FontManifests
loadableManifests: LoadableManifests loadableManifests: LoadableManifests
currentEntrypoints: CurrentEntrypoints currentEntrypoints: CurrentEntrypoints
handleRequireCacheClearing: HandleRequireCacheClearing | undefined handleRequireCacheClearing: HandleRequireCacheClearing | undefined // Dev
changeSubscription: ChangeSubscription | undefined changeSubscription: ChangeSubscription | undefined // Dev
readyIds: ReadyIds readyIds: ReadyIds | undefined // Dev
page: string page: string
route: Route route: Route
}) { }) {
...@@ -890,7 +913,7 @@ export async function handleRouteType({ ...@@ -890,7 +913,7 @@ export async function handleRouteType({
route.dataEndpoint, route.dataEndpoint,
(pageName) => { (pageName) => {
// Report the next compilation again // Report the next compilation again
readyIds.delete(page) readyIds?.delete(page)
return { return {
event: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES, event: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES,
pages: [pageName], pages: [pageName],
...@@ -970,7 +993,7 @@ export async function handleRouteType({ ...@@ -970,7 +993,7 @@ export async function handleRouteType({
return return
} }
// Report the next compilation again // Report the next compilation again
readyIds.delete(page) readyIds?.delete(page)
return { return {
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES, action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
} }
...@@ -1048,3 +1071,238 @@ export async function handleRouteType({ ...@@ -1048,3 +1071,238 @@ export async function handleRouteType({
} }
} }
} }
export async function handleEntrypoints({
rewrites,
nextConfig,
entrypoints,
serverFields,
propagateServerField,
distDir,
globalEntrypoints,
currentEntrypoints,
changeSubscriptions,
changeSubscription,
clearChangeSubscription,
sendHmr,
startBuilding,
handleRequireCacheClearing,
prevMiddleware,
currentIssues,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
}: {
rewrites: SetupOpts['fsChecker']['rewrites']
nextConfig: NextConfigComplete
entrypoints: TurbopackResult<Entrypoints>
serverFields: ServerFields | undefined
propagateServerField:
| ((field: PropagateToWorkersField, args: any) => Promise<void>)
| undefined
distDir: string
globalEntrypoints: GlobalEntrypoints
currentEntrypoints: CurrentEntrypoints
changeSubscriptions: ChangeSubscriptions | undefined
changeSubscription: ChangeSubscription | undefined
clearChangeSubscription: ClearChangeSubscription | undefined
sendHmr: SendHmr | undefined
startBuilding: StartBuilding | undefined
handleRequireCacheClearing: HandleRequireCacheClearing | undefined
prevMiddleware: boolean | undefined
currentIssues: CurrentIssues
buildManifests: BuildManifests
appBuildManifests: AppBuildManifests
pagesManifests: PagesManifests
appPathsManifests: AppPathsManifests
middlewareManifests: MiddlewareManifests
actionManifests: ActionManifests
fontManifests: FontManifests
loadableManifests: LoadableManifests
}) {
globalEntrypoints.app = entrypoints.pagesAppEndpoint
globalEntrypoints.document = entrypoints.pagesDocumentEndpoint
globalEntrypoints.error = entrypoints.pagesErrorEndpoint
currentEntrypoints.clear()
for (const [pathname, route] of entrypoints.routes) {
switch (route.type) {
case 'page':
case 'page-api':
case 'app-page':
case 'app-route': {
currentEntrypoints.set(pathname, route)
break
}
default:
Log.info(`skipping ${pathname} (${route.type})`)
break
}
}
if (changeSubscriptions) {
for (const [pathname, subscriptionPromise] of changeSubscriptions) {
if (pathname === '') {
// middleware is handled below
continue
}
if (!currentEntrypoints.has(pathname)) {
const subscription = await subscriptionPromise
subscription.return?.()
changeSubscriptions.delete(pathname)
}
}
}
const { middleware, instrumentation } = entrypoints
// We check for explicit true/false, since it's initialized to
// undefined during the first loop (middlewareChanges event is
// unnecessary during the first serve)
if (prevMiddleware === true && !middleware) {
// Went from middleware to no middleware
await clearChangeSubscription?.('middleware', 'server')
sendHmr?.('middleware', {
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
})
} else if (prevMiddleware === false && middleware) {
// Went from no middleware to middleware
sendHmr?.('middleware', {
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
})
}
if (nextConfig.experimental.instrumentationHook && instrumentation) {
const processInstrumentation = async (
displayName: string,
name: string,
prop: 'nodeJs' | 'edge'
) => {
const writtenEndpoint = await instrumentation[prop].writeToDisk()
handleRequireCacheClearing?.(displayName, writtenEndpoint)
processIssues(currentIssues, name, writtenEndpoint)
}
await processInstrumentation(
'instrumentation (node.js)',
'instrumentation.nodeJs',
'nodeJs'
)
await processInstrumentation(
'instrumentation (edge)',
'instrumentation.edge',
'edge'
)
await loadMiddlewareManifest(
distDir,
middlewareManifests,
'instrumentation',
'instrumentation'
)
await writeManifests({
rewrites: rewrites,
distDir,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
currentEntrypoints,
})
if (serverFields && propagateServerField) {
serverFields.actualInstrumentationHookFile = '/instrumentation'
await propagateServerField(
'actualInstrumentationHookFile',
serverFields.actualInstrumentationHookFile
)
}
} else {
if (serverFields && propagateServerField) {
serverFields.actualInstrumentationHookFile = undefined
await propagateServerField(
'actualInstrumentationHookFile',
serverFields.actualInstrumentationHookFile
)
}
}
if (middleware) {
const processMiddleware = async () => {
const writtenEndpoint = await middleware.endpoint.writeToDisk()
handleRequireCacheClearing?.('middleware', writtenEndpoint)
processIssues(currentIssues, 'middleware', writtenEndpoint)
await loadMiddlewareManifest(
distDir,
middlewareManifests,
'middleware',
'middleware'
)
if (serverFields) {
serverFields.middleware = {
match: null as any,
page: '/',
matchers:
middlewareManifests.get('middleware')?.middleware['/'].matchers,
}
}
}
await processMiddleware()
changeSubscription?.(
'middleware',
'server',
false,
middleware.endpoint,
async () => {
const finishBuilding = startBuilding?.('middleware', undefined, true)
await processMiddleware()
if (serverFields && propagateServerField) {
await propagateServerField(
'actualMiddlewareFile',
serverFields.actualMiddlewareFile
)
await propagateServerField('middleware', serverFields.middleware)
}
await writeManifests({
rewrites: rewrites,
distDir,
buildManifests,
appBuildManifests,
pagesManifests,
appPathsManifests,
middlewareManifests,
actionManifests,
fontManifests,
loadableManifests,
currentEntrypoints,
})
finishBuilding?.()
return { event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES }
}
)
prevMiddleware = true
} else {
middlewareManifests.delete('middleware')
if (serverFields) {
serverFields.actualMiddlewareFile = undefined
serverFields.middleware = undefined
prevMiddleware = false
}
}
if (serverFields && propagateServerField) {
await propagateServerField(
'actualMiddlewareFile',
serverFields.actualMiddlewareFile
)
await propagateServerField('middleware', serverFields.middleware)
}
}
...@@ -17,7 +17,7 @@ createNextDescribe( ...@@ -17,7 +17,7 @@ createNextDescribe(
{ pathname: '/adapter-hooks/2', keyValue: 'value' }, { pathname: '/adapter-hooks/2', keyValue: 'value' },
{ pathname: '/adapter-hooks/1/account', keyValue: 'value' }, { pathname: '/adapter-hooks/1/account', keyValue: 'value' },
])( ])(
'should have the correct hooks', 'should have the correct hooks at $pathname',
async ({ pathname, keyValue = '' }) => { async ({ pathname, keyValue = '' }) => {
const browser = await next.browser( const browser = await next.browser(
pathname + (keyValue ? `?key=${keyValue}` : '') pathname + (keyValue ? `?key=${keyValue}` : '')
......
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