Unverified Commit d41f8a5e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Update terminal tree view for switchable runtime (#35394)

* Update terminal tree view for switchable runtime

* share PageRuntime type

* fix lint
parent 4f6bb5e4
import type { PageRuntime, NextConfigComplete } from '../server/config-shared'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import fs from 'fs' import fs from 'fs'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import { posix, join } from 'path' import { posix, join } from 'path'
...@@ -12,11 +14,9 @@ import { MiddlewareLoaderOptions } from './webpack/loaders/next-middleware-loade ...@@ -12,11 +14,9 @@ import { MiddlewareLoaderOptions } from './webpack/loaders/next-middleware-loade
import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-loader' import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-loader'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader' import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { LoadedEnvFiles } from '@next/env' import { LoadedEnvFiles } from '@next/env'
import { NextConfigComplete } from '../server/config-shared'
import { parse } from '../build/swc' import { parse } from '../build/swc'
import { isCustomErrorPage, isFlightPage, isReservedPage } from './utils' import { isCustomErrorPage, isFlightPage, isReservedPage } from './utils'
import { ssrEntries } from './webpack/plugins/middleware-plugin' import { ssrEntries } from './webpack/plugins/middleware-plugin'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import { import {
MIDDLEWARE_RUNTIME_WEBPACK, MIDDLEWARE_RUNTIME_WEBPACK,
MIDDLEWARE_SSR_RUNTIME_WEBPACK, MIDDLEWARE_SSR_RUNTIME_WEBPACK,
...@@ -99,17 +99,14 @@ type Entrypoints = { ...@@ -99,17 +99,14 @@ type Entrypoints = {
edgeServer: webpack5.EntryObject edgeServer: webpack5.EntryObject
} }
const cachedPageRuntimeConfig = new Map< const cachedPageRuntimeConfig = new Map<string, [number, PageRuntime]>()
string,
[number, 'nodejs' | 'edge' | undefined]
>()
// @TODO: We should limit the maximum concurrency of this function as there // @TODO: We should limit the maximum concurrency of this function as there
// could be thousands of pages existing. // could be thousands of pages existing.
export async function getPageRuntime( export async function getPageRuntime(
pageFilePath: string, pageFilePath: string,
globalRuntimeFallback?: 'nodejs' | 'edge' globalRuntimeFallback?: 'nodejs' | 'edge'
): Promise<'nodejs' | 'edge' | undefined> { ): Promise<PageRuntime> {
const cached = cachedPageRuntimeConfig.get(pageFilePath) const cached = cachedPageRuntimeConfig.get(pageFilePath)
if (cached) { if (cached) {
return cached[1] return cached[1]
...@@ -129,7 +126,7 @@ export async function getPageRuntime( ...@@ -129,7 +126,7 @@ export async function getPageRuntime(
// discussion: // discussion:
// https://github.com/vercel/next.js/discussions/34179 // https://github.com/vercel/next.js/discussions/34179
let isRuntimeRequired: boolean = false let isRuntimeRequired: boolean = false
let pageRuntime: 'nodejs' | 'edge' | undefined = undefined let pageRuntime: PageRuntime = undefined
// Since these configurations should always be static analyzable, we can // Since these configurations should always be static analyzable, we can
// skip these cases that "runtime" and "gSP", "gSSP" are not included in the // skip these cases that "runtime" and "gSP", "gSSP" are not included in the
......
...@@ -969,7 +969,7 @@ export default async function build( ...@@ -969,7 +969,7 @@ export default async function build(
join(pagesDir, pagePath), join(pagesDir, pagePath),
config.experimental.runtime config.experimental.runtime
) )
: null : undefined
if ( if (
!isMiddlewareRoute && !isMiddlewareRoute &&
...@@ -1084,14 +1084,13 @@ export default async function build( ...@@ -1084,14 +1084,13 @@ export default async function build(
totalSize: allSize, totalSize: allSize,
static: isStatic, static: isStatic,
isSsg, isSsg,
isWebSsr:
hasConcurrentFeatures &&
!isMiddlewareRoute &&
!isReservedPage(page) &&
!isCustomErrorPage(page),
isHybridAmp, isHybridAmp,
ssgPageRoutes, ssgPageRoutes,
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
runtime:
!isReservedPage(page) && !isCustomErrorPage(page)
? pageRuntime
: undefined,
pageDuration: undefined, pageDuration: undefined,
ssgPageDurations: undefined, ssgPageDurations: undefined,
}) })
......
import type { NextConfigComplete, PageRuntime } from '../server/config-shared'
import '../server/node-polyfill-fetch' import '../server/node-polyfill-fetch'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import getGzipSize from 'next/dist/compiled/gzip-size' import getGzipSize from 'next/dist/compiled/gzip-size'
...@@ -37,7 +39,6 @@ import * as Log from './output/log' ...@@ -37,7 +39,6 @@ import * as Log from './output/log'
import { loadComponents } from '../server/load-components' import { loadComponents } from '../server/load-components'
import { trace } from '../trace' import { trace } from '../trace'
import { setHttpAgentOptions } from '../server/config' import { setHttpAgentOptions } from '../server/config'
import { NextConfigComplete } from '../server/config-shared'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
import { Sema } from 'next/dist/compiled/async-sema' import { Sema } from 'next/dist/compiled/async-sema'
...@@ -77,11 +78,11 @@ export interface PageInfo { ...@@ -77,11 +78,11 @@ export interface PageInfo {
totalSize: number totalSize: number
static: boolean static: boolean
isSsg: boolean isSsg: boolean
isWebSsr: boolean
ssgPageRoutes: string[] | null ssgPageRoutes: string[] | null
initialRevalidateSeconds: number | false initialRevalidateSeconds: number | false
pageDuration: number | undefined pageDuration: number | undefined
ssgPageDurations: number[] | undefined ssgPageDurations: number[] | undefined
runtime: PageRuntime
} }
export async function printTreeView( export async function printTreeView(
...@@ -195,12 +196,12 @@ export async function printTreeView( ...@@ -195,12 +196,12 @@ export async function printTreeView(
? ' ' ? ' '
: item.endsWith('/_middleware') : item.endsWith('/_middleware')
? 'ƒ' ? 'ƒ'
: pageInfo?.isWebSsr
? 'ℇ'
: pageInfo?.static : pageInfo?.static
? '○' ? '○'
: pageInfo?.isSsg : pageInfo?.isSsg
? '●' ? '●'
: pageInfo?.runtime === 'edge'
? 'ℇ'
: 'λ' : 'λ'
usedSymbols.add(symbol) usedSymbols.add(symbol)
......
...@@ -7,6 +7,8 @@ import { ...@@ -7,6 +7,8 @@ import {
imageConfigDefault, imageConfigDefault,
} from '../shared/lib/image-config' } from '../shared/lib/image-config'
export type PageRuntime = 'nodejs' | 'edge' | undefined
export type NextConfigComplete = Required<NextConfig> & { export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete> images: Required<ImageConfigComplete>
typescript: Required<TypeScriptConfig> typescript: Required<TypeScriptConfig>
...@@ -101,7 +103,7 @@ export interface ExperimentalConfig { ...@@ -101,7 +103,7 @@ export interface ExperimentalConfig {
craCompat?: boolean craCompat?: boolean
esmExternals?: boolean | 'loose' esmExternals?: boolean | 'loose'
isrMemoryCacheSize?: number isrMemoryCacheSize?: number
runtime?: 'nodejs' | 'edge' runtime?: Exclude<PageRuntime, undefined>
serverComponents?: boolean serverComponents?: boolean
fullySpecified?: boolean fullySpecified?: boolean
urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp'] urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp']
......
/* eslint-env jest */ /* eslint-env jest */
import { join } from 'path' import { join } from 'path'
import {
// File,
nextBuild as _nextBuild,
nextStart as _nextStart,
} from 'next-test-utils'
import { findPort, killApp, renderViaHTTP } from 'next-test-utils' import { findPort, killApp, renderViaHTTP } from 'next-test-utils'
import { nextBuild, nextStart } from './utils'
const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')]
const appDir = join(__dirname, '../switchable-runtime') const appDir = join(__dirname, '../switchable-runtime')
// const nextConfig = new File(join(appDir, 'next.config.js'))
async function nextBuild(dir, options) {
return await _nextBuild(dir, [], {
...options,
stdout: true,
stderr: true,
nodeArgs,
})
}
async function nextStart(dir, port) { function splitLines(text) {
return await _nextStart(dir, port, { return text
stdout: true, .split(/\r?\n/g)
stderr: true, .map((str) => str.trim())
nodeArgs, .filter(Boolean)
})
} }
async function testRoute(appPort, url, { isStatic, isEdge }) { async function testRoute(appPort, url, { isStatic, isEdge }) {
...@@ -54,7 +36,8 @@ describe('Without global runtime configuration', () => { ...@@ -54,7 +36,8 @@ describe('Without global runtime configuration', () => {
beforeAll(async () => { beforeAll(async () => {
context.appPort = await findPort() context.appPort = await findPort()
const { stderr } = await nextBuild(context.appDir) const { stdout, stderr } = await nextBuild(context.appDir)
context.stdout = stdout
context.stderr = stderr context.stderr = stderr
context.server = await nextStart(context.appDir, context.appPort) context.server = await nextStart(context.appDir, context.appPort)
}) })
...@@ -124,4 +107,26 @@ describe('Without global runtime configuration', () => { ...@@ -124,4 +107,26 @@ describe('Without global runtime configuration', () => {
isEdge: true, isEdge: true,
}) })
}) })
it('should display correct tree view with page types in terminal', async () => {
const stdoutLines = splitLines(context.stdout).filter((line) =>
/^[┌├└/]/.test(line)
)
const expectedOutputLines = splitLines(`
┌ λ /404
├ ℇ /edge
├ ℇ /edge-rsc
├ ○ /node
├ ○ /node-rsc
├ ● /node-rsc-ssg
├ λ /node-rsc-ssr
├ ● /node-ssg
├ λ /node-ssr
└ ○ /static
`)
const isMatched = expectedOutputLines.every((line, index) =>
stdoutLines[index].startsWith(line)
)
expect(isMatched).toBe(true)
})
}) })
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