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

Refactor entries generation in hot-reloader #34733

Merged
merged 4 commits into from Feb 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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