From de51ef17230f1a2f6b76ebe8bee27d68b25495a8 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Tue, 2 Jan 2024 10:34:47 +0100 Subject: [PATCH] Add MappedPages type --- packages/next/src/build/build-context.ts | 20 +++++++------------ packages/next/src/build/entries.ts | 17 ++++++++++------ packages/next/src/build/index.ts | 8 +++++--- .../loaders/next-route-loader/index.ts | 3 ++- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/next/src/build/build-context.ts b/packages/next/src/build/build-context.ts index e7cf889c29d61..ea184a14290fa 100644 --- a/packages/next/src/build/build-context.ts +++ b/packages/next/src/build/build-context.ts @@ -41,6 +41,10 @@ export function getPluginState() { return pluginState } +export interface MappedPages { + [page: string]: string +} + // a global object to store context for the current build // this is used to pass data between different steps of the build without having // to pass it through function arguments. @@ -67,19 +71,9 @@ export const NextBuildContext: Partial<{ originalRedirects: Redirect[] loadedEnvFiles: LoadedEnvFiles previewProps: __ApiPreviewProps - mappedPages: - | { - [page: string]: string - } - | undefined - mappedAppPages: - | { - [page: string]: string - } - | undefined - mappedRootPaths: { - [page: string]: string - } + mappedPages: MappedPages | undefined + mappedAppPages: MappedPages | undefined + mappedRootPaths: MappedPages hasInstrumentationHook: boolean // misc fields diff --git a/packages/next/src/build/entries.ts b/packages/next/src/build/entries.ts index c62b0f041da5d..0cd26cd204a55 100644 --- a/packages/next/src/build/entries.ts +++ b/packages/next/src/build/entries.ts @@ -62,6 +62,7 @@ import { RouteKind } from '../server/future/route-kind' import { encodeToBase64 } from './webpack/loaders/utils' import { normalizeCatchAllRoutes } from './normalize-catchall-routes' import type { PageExtensions } from './page-extensions-type' +import type { MappedPages } from './build-context' export function sortByPageExts(pageExtensions: PageExtensions) { return (a: string, b: string) => { @@ -213,6 +214,10 @@ export function getPageFilePath({ return require.resolve(absolutePagePath) } +/** + * Creates a mapping of route to page file path for a given list of page paths. + * For example ['/middleware.ts'] is turned into { '/middleware': `${ROOT_DIR_ALIAS}/middleware.ts` } + */ export function createPagesMapping({ isDev, pageExtensions, @@ -225,7 +230,7 @@ export function createPagesMapping({ pagePaths: string[] pagesType: 'pages' | 'root' | 'app' pagesDir: string | undefined -}): { [page: string]: string } { +}): MappedPages { const isAppRoute = pagesType === 'app' const previousPages: { [key: string]: string } = {} const pages = pagePaths.reduce<{ [key: string]: string }>( @@ -312,13 +317,13 @@ export interface CreateEntrypointsParams { config: NextConfigComplete envFiles: LoadedEnvFiles isDev?: boolean - pages: { [page: string]: string } + pages: MappedPages pagesDir?: string previewMode: __ApiPreviewProps rootDir: string - rootPaths?: Record + rootPaths?: MappedPages appDir?: string - appPaths?: Record + appPaths?: MappedPages pageExtensions: PageExtensions hasInstrumentationHook?: boolean } @@ -332,7 +337,7 @@ export function getEdgeServerEntry(opts: { isDev: boolean isServerComponent: boolean page: string - pages: { [page: string]: string } + pages: MappedPages middleware?: Partial pagesType: 'app' | 'pages' | 'root' appDirLoader?: string @@ -561,7 +566,7 @@ export async function createEntrypoints( const getEntryHandler = ( - mappings: Record, + mappings: MappedPages, pagesType: 'app' | 'pages' | 'root' ): ((page: string) => void) => async (page) => { diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index de1afeb5bc4b4..ffeeb6e98b476 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -145,7 +145,7 @@ import { NEXT_DID_POSTPONE_HEADER, } from '../client/components/app-router-headers' import { webpackBuild } from './webpack-build' -import { NextBuildContext } from './build-context' +import { NextBuildContext, type MappedPages } from './build-context' import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep' import { isAppRouteRoute } from '../lib/is-app-route-route' import { createClientRouterFilter } from '../lib/create-client-router-filter' @@ -607,7 +607,7 @@ export default async function build( ) NextBuildContext.mappedPages = mappedPages - let mappedAppPages: { [page: string]: string } | undefined + let mappedAppPages: MappedPages | undefined let denormalizedAppPages: string[] | undefined if (appDir) { @@ -672,7 +672,8 @@ export default async function build( NextBuildContext.mappedAppPages = mappedAppPages } - let mappedRootPaths: { [page: string]: string } = {} + let mappedRootPaths: MappedPages = {} + if (rootPaths.length > 0) { mappedRootPaths = createPagesMapping({ isDev: false, @@ -682,6 +683,7 @@ export default async function build( pagesDir: pagesDir, }) } + NextBuildContext.mappedRootPaths = mappedRootPaths const pagesPageKeys = Object.keys(mappedPages) diff --git a/packages/next/src/build/webpack/loaders/next-route-loader/index.ts b/packages/next/src/build/webpack/loaders/next-route-loader/index.ts index 6423a4a2b310a..7ddb25aa5adda 100644 --- a/packages/next/src/build/webpack/loaders/next-route-loader/index.ts +++ b/packages/next/src/build/webpack/loaders/next-route-loader/index.ts @@ -11,6 +11,7 @@ import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-pa import { decodeFromBase64, encodeToBase64 } from '../utils' import { isInstrumentationHookFile } from '../../../worker' import { loadEntrypoint } from '../../../load-entrypoint' +import type { MappedPages } from '../../../build-context' type RouteLoaderOptionsPagesAPIInput = { kind: RouteKind.PAGES_API @@ -23,7 +24,7 @@ type RouteLoaderOptionsPagesAPIInput = { type RouteLoaderOptionsPagesInput = { kind: RouteKind.PAGES page: string - pages: { [page: string]: string } + pages: MappedPages preferredRegion: string | string[] | undefined absolutePagePath: string middlewareConfig: MiddlewareConfig