Skip to content

Commit

Permalink
Refactor entries generation in hot-reloader (#34733)
Browse files Browse the repository at this point in the history
* refactor entries generation of hot-reloader

* change parameter name
  • Loading branch information
shuding committed Feb 24, 2022
1 parent fbb8536 commit ceecf91
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/entries.ts
Expand Up @@ -117,6 +117,8 @@ export function createEntrypoints(
Object.keys(config.publicRuntimeConfig).length > 0 ||
Object.keys(config.serverRuntimeConfig).length > 0

const edgeRuntime = config.experimental.runtime === 'edge'

const defaultServerlessOptions = {
absoluteAppPath: pages['/_app'],
absoluteDocumentPath: pages['/_document'],
Expand Down Expand Up @@ -157,8 +159,6 @@ export function createEntrypoints(
const isCustomError = isCustomErrorPage(page)
const isFlight = isFlightPage(config, absolutePagePath)

const edgeRuntime = config.experimental.runtime === 'edge'

if (page.match(MIDDLEWARE_ROUTE)) {
const loaderOpts: MiddlewareLoaderOptions = {
absolutePagePath: pages[page],
Expand Down
10 changes: 5 additions & 5 deletions packages/next/build/utils.ts
Expand Up @@ -1111,23 +1111,23 @@ export function getRawPageExtensions(pageExtensions: string[]): string[] {

export function isFlightPage(
nextConfig: NextConfigComplete,
pagePath: string
filePath: string
): boolean {
if (
!(
nextConfig.experimental.serverComponents &&
nextConfig.experimental.runtime
)
)
) {
return false
}

const rawPageExtensions = getRawPageExtensions(
nextConfig.pageExtensions || []
)
const isRscPage = rawPageExtensions.some((ext) => {
return new RegExp(`\\.server\\.${ext}$`).test(pagePath)
return rawPageExtensions.some((ext) => {
return filePath.endsWith(`.server.${ext}`)
})
return isRscPage
}

export function getUnresolvedModuleFromError(
Expand Down
61 changes: 31 additions & 30 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -477,8 +477,6 @@ export default class HotReloader {
return
}

const isApiRoute = page.match(API_ROUTE)

if (!isClientCompilation && isMiddleware) {
return
}
Expand All @@ -491,18 +489,15 @@ export default class HotReloader {
return
}

const isApiRoute = page.match(API_ROUTE)
const isCustomError = isCustomErrorPage(page)
const isReserved = isReservedPage(page)
const isServerComponent =
this.hasServerComponents &&
isFlightPage(this.config, absolutePagePath)
const isEdgeSSRPage = this.runtime === 'edge' && !isApiRoute

if (
isNodeServerCompilation &&
this.runtime === 'edge' &&
!isApiRoute &&
!isCustomError
) {
if (isNodeServerCompilation && isEdgeSSRPage && !isCustomError) {
return
}

Expand All @@ -512,28 +507,34 @@ export default class HotReloader {
absolutePagePath,
}

if (isClientCompilation && isMiddleware) {
entrypoints[bundlePath] = finalizeEntrypoint({
name: bundlePath,
value: `next-middleware-loader?${stringify(pageLoaderOpts)}!`,
isServer: false,
isMiddleware: true,
})
} else if (isClientCompilation) {
entrypoints[bundlePath] = finalizeEntrypoint({
name: bundlePath,
value: `next-client-pages-loader?${stringify(pageLoaderOpts)}!`,
isServer: false,
})
if (isClientCompilation) {
if (isMiddleware) {
// Middleware
entrypoints[bundlePath] = finalizeEntrypoint({
name: bundlePath,
value: `next-middleware-loader?${stringify(pageLoaderOpts)}!`,
isServer: false,
isMiddleware: true,
})
} else {
// A page route
entrypoints[bundlePath] = finalizeEntrypoint({
name: bundlePath,
value: `next-client-pages-loader?${stringify(
pageLoaderOpts
)}!`,
isServer: false,
})

if (isServerComponent) {
ssrEntries.set(bundlePath, { requireFlightManifest: true })
} else if (
this.runtime === 'edge' &&
!isReserved &&
!isCustomError
) {
ssrEntries.set(bundlePath, { requireFlightManifest: false })
// Tell the middleware plugin of the client compilation
// that this route is a page.
if (isEdgeSSRPage) {
if (isServerComponent) {
ssrEntries.set(bundlePath, { requireFlightManifest: true })
} else if (!isCustomError && !isReserved) {
ssrEntries.set(bundlePath, { requireFlightManifest: false })
}
}
}
} else if (isEdgeServerCompilation) {
if (!isReserved) {
Expand All @@ -555,7 +556,7 @@ export default class HotReloader {
isEdgeServer: true,
})
}
} else {
} else if (isNodeServerCompilation) {
let request = relative(config.context!, absolutePagePath)
if (!isAbsolute(request) && !request.startsWith('../')) {
request = `./${request}`
Expand Down

0 comments on commit ceecf91

Please sign in to comment.