Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hot-reloader interface #54629

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/next/src/server/dev/hot-reloader-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { UrlObject } from 'url'
import type { Duplex } from 'stream'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type getBaseWebpackConfig from '../../build/webpack-config'
import type { RouteMatch } from '../future/route-matches/route-match'

export interface NextJsHotReloaderInterface {
activeConfigs?: Array<Awaited<ReturnType<typeof getBaseWebpackConfig>>>
serverStats: webpack.Stats | null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webpack.Stats is probably hard to implemented for non webpack implementations

Copy link
Member Author

@timneutkens timneutkens Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used in logErrorWithOriginalStack, we'll want to replace the usage of serverStats and edgeServerStats for this.

edgeServerStats: webpack.Stats | null
run(
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlObject
): Promise<{ finished?: true }>

setHmrServerError(error: Error | null): void
clearHmrServerError(): void
start(): Promise<void>
stop(): Promise<void>
send(action?: string | any, ...args: any[]): void
getCompilationErrors(page: string): Promise<any[]>
onHMR(req: IncomingMessage, _socket: Duplex, head: Buffer): void
invalidate({
reloadAfterInvalidation,
}: {
reloadAfterInvalidation: boolean
}): void
buildFallbackError(): Promise<void>
ensurePage({
page,
clientOnly,
appPaths,
match,
isApp,
}: {
page: string
clientOnly: boolean
appPaths?: string[] | null
isApp?: boolean
match?: RouteMatch
}): Promise<void>
}
18 changes: 9 additions & 9 deletions packages/next/src/server/dev/hot-reloader-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { NextConfigComplete } from '../config-shared'
import type { CustomRoutes } from '../../lib/load-custom-routes'
import type { Duplex } from 'stream'
import type { Telemetry } from '../../telemetry/storage'
import type { IncomingMessage, ServerResponse } from 'http'
import type { UrlObject } from 'url'

import { webpack, StringXor } from 'next/dist/compiled/webpack/webpack'
import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
import { IncomingMessage, ServerResponse } from 'http'
import { WebpackHotMiddleware } from './hot-middleware'
import { join, relative, isAbsolute, posix } from 'path'
import { UrlObject } from 'url'
import {
createEntrypoints,
createPagesMapping,
Expand Down Expand Up @@ -67,6 +67,7 @@ import { isAPIRoute } from '../../lib/is-api-route'
import { getRouteLoaderEntry } from '../../build/webpack/loaders/next-route-loader'
import { isInternalComponent } from '../../lib/is-internal-component'
import { RouteKind } from '../future/route-kind'
import { NextJsHotReloaderInterface } from './hot-reloader-types'

const MILLISECONDS_IN_NANOSECOND = 1_000_000

Expand Down Expand Up @@ -174,7 +175,7 @@ function erroredPages(compilation: webpack.Compilation) {
return failedPages
}

export default class HotReloader {
export default class HotReloader implements NextJsHotReloaderInterface {
private hasAmpEntrypoints: boolean
private hasAppRouterEntrypoints: boolean
private hasPagesRouterEntrypoints: boolean
Expand All @@ -185,10 +186,7 @@ export default class HotReloader {
private distDir: string
private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete
public hasServerComponents: boolean
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this variable was not used so removed it 👍

public clientStats: webpack.Stats | null
public serverStats: webpack.Stats | null
public edgeServerStats: webpack.Stats | null
private clientStats: webpack.Stats | null
private clientError: Error | null = null
private serverError: Error | null = null
private hmrServerError: Error | null = null
Expand All @@ -209,6 +207,9 @@ export default class HotReloader {
installed: '0.0.0',
}
private reloadAfterInvalidation: boolean = false

public serverStats: webpack.Stats | null
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the public variables together so that they're easier to spot.

public edgeServerStats: webpack.Stats | null
public multiCompiler?: webpack.MultiCompiler
public activeConfigs?: Array<
UnwrapPromise<ReturnType<typeof getBaseWebpackConfig>>
Expand Down Expand Up @@ -252,7 +253,6 @@ export default class HotReloader {
this.telemetry = telemetry

this.config = config
this.hasServerComponents = !!this.appDir
this.previewProps = previewProps
this.rewrites = rewrites
this.hotReloaderSpan = trace('hot-reloader', undefined, {
Expand Down Expand Up @@ -346,7 +346,7 @@ export default class HotReloader {
}
}

public async refreshServerComponents(): Promise<void> {
protected async refreshServerComponents(): Promise<void> {
this.send({
action: 'serverComponentChanges',
// TODO: granular reloading of changes
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import { MiddlewareManifest } from '../../../build/webpack/plugins/middleware-pl
import { devPageFiles } from '../../../build/webpack/plugins/next-types-plugin/shared'
import type { RenderWorkers } from '../router-server'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types'

type SetupOpts = {
renderWorkers: RenderWorkers
Expand Down Expand Up @@ -160,7 +161,7 @@ async function startWatcher(opts: SetupOpts) {
>[]
} = {}

let hotReloader: InstanceType<typeof HotReloader>
let hotReloader: NextJsHotReloaderInterface

if (opts.turbo) {
const { loadBindings } =
Expand Down