Skip to content

Commit

Permalink
Add MappedPages type
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jan 2, 2024
1 parent a2c14f1 commit de51ef1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
20 changes: 7 additions & 13 deletions packages/next/src/build/build-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand All @@ -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 }>(
Expand Down Expand Up @@ -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<string, string>
rootPaths?: MappedPages
appDir?: string
appPaths?: Record<string, string>
appPaths?: MappedPages
pageExtensions: PageExtensions
hasInstrumentationHook?: boolean
}
Expand All @@ -332,7 +337,7 @@ export function getEdgeServerEntry(opts: {
isDev: boolean
isServerComponent: boolean
page: string
pages: { [page: string]: string }
pages: MappedPages
middleware?: Partial<MiddlewareConfig>
pagesType: 'app' | 'pages' | 'root'
appDirLoader?: string
Expand Down Expand Up @@ -561,7 +566,7 @@ export async function createEntrypoints(

const getEntryHandler =
(
mappings: Record<string, string>,
mappings: MappedPages,
pagesType: 'app' | 'pages' | 'root'
): ((page: string) => void) =>
async (page) => {
Expand Down
8 changes: 5 additions & 3 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -682,6 +683,7 @@ export default async function build(
pagesDir: pagesDir,
})
}

NextBuildContext.mappedRootPaths = mappedRootPaths

const pagesPageKeys = Object.keys(mappedPages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit de51ef1

Please sign in to comment.