-
Notifications
You must be signed in to change notification settings - Fork 26k
/
next-app-loader.ts
439 lines (378 loc) · 13.2 KB
/
next-app-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import type webpack from 'webpack'
import type { ValueOf } from '../../../shared/lib/constants'
import type { ModuleReference, CollectedMetadata } from './metadata/types'
import path from 'path'
import chalk from 'next/dist/compiled/chalk'
import { NODE_RESOLVE_OPTIONS } from '../../webpack-config'
import { getModuleBuildInfo } from './get-module-build-info'
import { verifyRootLayout } from '../../../lib/verifyRootLayout'
import * as Log from '../../../build/output/log'
import { APP_DIR_ALIAS } from '../../../lib/constants'
import {
buildMetadata,
discoverStaticMetadataFiles,
METADATA_RESOURCE_QUERY,
} from './metadata/discover'
import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'
export type AppLoaderOptions = {
name: string
pagePath: string
appDir: string
appPaths: string[] | null
pageExtensions: string[]
basePath: string
assetPrefix: string
rootDir?: string
tsconfigPath?: string
isDev?: boolean
}
type AppLoader = webpack.LoaderDefinitionFunction<AppLoaderOptions>
const isNotResolvedError = (err: any) => err.message.includes("Can't resolve")
const FILE_TYPES = {
layout: 'layout',
template: 'template',
error: 'error',
loading: 'loading',
head: 'head',
'not-found': 'not-found',
} as const
const GLOBAL_ERROR_FILE_TYPE = 'global-error'
const PAGE_SEGMENT = 'page$'
type PathResolver = (
pathname: string,
resolveDir?: boolean
) => Promise<string | undefined>
export type ComponentsType = {
readonly [componentKey in ValueOf<typeof FILE_TYPES>]?: ModuleReference
} & {
readonly page?: ModuleReference
} & {
readonly metadata?: CollectedMetadata
}
async function createAppRouteCode({
name,
pagePath,
resolver,
}: {
name: string
pagePath: string
resolver: PathResolver
}): Promise<string> {
const routePath = pagePath.replace(/[\\/]/, '/')
// This, when used with the resolver will give us the pathname to the built
// route handler file.
let resolvedPagePath = (await resolver(routePath))!
if (isMetadataRoute(name)) {
resolvedPagePath = `next-metadata-route-loader!${
resolvedPagePath + METADATA_RESOURCE_QUERY
}`
}
// TODO: verify if other methods need to be injected
// TODO: validate that the handler exports at least one of the supported methods
return `
import 'next/dist/server/node-polyfill-headers'
export * as handlers from ${JSON.stringify(resolvedPagePath)}
export const resolvedPagePath = ${JSON.stringify(resolvedPagePath)}
export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage'
export * as serverHooks from 'next/dist/client/components/hooks-server-context'
export { staticGenerationBailout } from 'next/dist/client/components/static-generation-bailout'
export * as headerHooks from 'next/dist/client/components/headers'
export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage'
`
}
async function createTreeCodeFromPath(
pagePath: string,
{
resolver,
resolvePath,
resolveParallelSegments,
loaderContext,
loaderOptions,
}: {
resolver: (
pathname: string,
resolveDir?: boolean
) => Promise<string | undefined>
resolvePath: (pathname: string) => Promise<string>
resolveParallelSegments: (
pathname: string
) => [key: string, segment: string | string[]][]
loaderContext: webpack.LoaderContext<AppLoaderOptions>
loaderOptions: AppLoaderOptions
}
) {
const splittedPath = pagePath.split(/[\\/]/)
const appDirPrefix = splittedPath[0]
const pages: string[] = []
let rootLayout: string | undefined
let globalError: string | undefined
async function createSubtreePropsFromSegmentPath(
segments: string[]
): Promise<{
treeCode: string
}> {
const segmentPath = segments.join('/')
// Existing tree are the children of the current segment
const props: Record<string, string> = {}
const isRootLayer = segments.length === 0
// We need to resolve all parallel routes in this level.
const parallelSegments: [key: string, segment: string | string[]][] = []
if (isRootLayer) {
parallelSegments.push(['children', ''])
} else {
parallelSegments.push(...resolveParallelSegments(segmentPath))
}
let metadata: Awaited<ReturnType<typeof discoverStaticMetadataFiles>> = null
try {
const routerDirPath = `${appDirPrefix}${segmentPath}`
const resolvedRouteDir = await resolver(routerDirPath, true)
if (resolvedRouteDir) {
metadata = await discoverStaticMetadataFiles(resolvedRouteDir, {
resolvePath,
isRootLayer,
loaderContext,
loaderOptions,
})
}
} catch (err: any) {
if (isNotResolvedError(err)) {
throw err
}
}
for (const [parallelKey, parallelSegment] of parallelSegments) {
if (parallelSegment === PAGE_SEGMENT) {
const matchedPagePath = `${appDirPrefix}${segmentPath}${
parallelKey === 'children' ? '' : `/@${parallelKey}`
}/page`
const resolvedPagePath = await resolver(matchedPagePath)
if (resolvedPagePath) pages.push(resolvedPagePath)
// Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it.
props[parallelKey] = `['', {}, {
page: [() => import(/* webpackMode: "eager" */ ${JSON.stringify(
resolvedPagePath
)}), ${JSON.stringify(resolvedPagePath)}],
${buildMetadata(metadata)}
}]`
continue
}
const parallelSegmentPath = segmentPath + '/' + parallelSegment
const { treeCode: subtreeCode } = await createSubtreePropsFromSegmentPath(
[
...segments,
...(parallelKey === 'children' ? [] : [`@${parallelKey}`]),
Array.isArray(parallelSegment) ? parallelSegment[0] : parallelSegment,
]
)
// `page` is not included here as it's added above.
const filePaths = await Promise.all(
Object.values(FILE_TYPES).map(async (file) => {
return [
file,
await resolver(
`${appDirPrefix}${
// TODO-APP: parallelSegmentPath sometimes ends in `/` but sometimes it doesn't. This should be consistent.
parallelSegmentPath.endsWith('/')
? parallelSegmentPath
: parallelSegmentPath + '/'
}${file}`
),
] as const
})
)
if (!rootLayout) {
const layoutPath = filePaths.find(
([type, filePath]) => type === 'layout' && !!filePath
)?.[1]
rootLayout = layoutPath
if (layoutPath) {
globalError = await resolver(
`${path.dirname(layoutPath)}/${GLOBAL_ERROR_FILE_TYPE}`
)
}
}
const definedFilePaths = filePaths.filter(
([, filePath]) => filePath !== undefined
)
props[parallelKey] = `[
'${
Array.isArray(parallelSegment) ? parallelSegment[0] : parallelSegment
}',
${subtreeCode},
{
${definedFilePaths
.map(([file, filePath]) => {
return `'${file}': [() => import(/* webpackMode: "eager" */ ${JSON.stringify(
filePath
)}), ${JSON.stringify(filePath)}],`
})
.join('\n')}
${definedFilePaths.length ? buildMetadata(metadata) : ''}
}
]`
}
return {
treeCode: `{
${Object.entries(props)
.map(([key, value]) => `${key}: ${value}`)
.join(',\n')}
}`,
}
}
const { treeCode } = await createSubtreePropsFromSegmentPath([])
return {
treeCode: `const tree = ${treeCode}.children;`,
pages: `const pages = ${JSON.stringify(pages)};`,
rootLayout,
globalError,
}
}
function createAbsolutePath(appDir: string, pathToTurnAbsolute: string) {
return (
pathToTurnAbsolute
// Replace all POSIX path separators with the current OS path separator
.replace(/\//g, path.sep)
.replace(/^private-next-app-dir/, appDir)
)
}
const nextAppLoader: AppLoader = async function nextAppLoader() {
const loaderOptions = this.getOptions() || {}
const {
name,
appDir,
appPaths,
pagePath,
pageExtensions,
rootDir,
tsconfigPath,
isDev,
} = loaderOptions
const buildInfo = getModuleBuildInfo((this as any)._module)
buildInfo.route = {
page: name.replace(/^app/, ''),
absolutePagePath: createAbsolutePath(appDir, pagePath),
}
const extensions = pageExtensions.map((extension) => `.${extension}`)
const resolveOptions: any = {
...NODE_RESOLVE_OPTIONS,
extensions,
}
const resolve = this.getResolve(resolveOptions)
const normalizedAppPaths =
typeof appPaths === 'string' ? [appPaths] : appPaths || []
const resolveParallelSegments = (
pathname: string
): [string, string | string[]][] => {
const matched: Record<string, string | string[]> = {}
for (const appPath of normalizedAppPaths) {
if (appPath.startsWith(pathname + '/')) {
const rest = appPath.slice(pathname.length + 1).split('/')
// It is the actual page, mark it specially.
if (rest.length === 1 && rest[0] === 'page') {
matched.children = PAGE_SEGMENT
continue
}
const isParallelRoute = rest[0].startsWith('@')
if (isParallelRoute && rest.length === 2 && rest[1] === 'page') {
matched[rest[0].slice(1)] = PAGE_SEGMENT
continue
}
if (isParallelRoute) {
matched[rest[0].slice(1)] = rest.slice(1)
continue
}
matched.children = rest[0]
}
}
return Object.entries(matched)
}
const resolver: PathResolver = async (pathname, resolveDir) => {
if (resolveDir) {
return createAbsolutePath(appDir, pathname)
}
try {
const resolved = await resolve(this.rootContext, pathname)
this.addDependency(resolved)
return resolved
} catch (err: any) {
const absolutePath = createAbsolutePath(appDir, pathname)
for (const ext of extensions) {
const absolutePathWithExtension = `${absolutePath}${ext}`
this.addMissingDependency(absolutePathWithExtension)
}
if (isNotResolvedError(err)) {
return undefined
}
throw err
}
}
if (isAppRouteRoute(name)) {
return createAppRouteCode({ name, pagePath, resolver })
}
const {
treeCode,
pages: pageListCode,
rootLayout,
globalError,
} = await createTreeCodeFromPath(pagePath, {
resolver,
resolvePath: (pathname: string) => resolve(this.rootContext, pathname),
resolveParallelSegments,
loaderContext: this,
loaderOptions: loaderOptions,
})
if (!rootLayout) {
if (!isDev) {
// If we're building and missing a root layout, exit the build
Log.error(
`${chalk.bold(
pagePath.replace(`${APP_DIR_ALIAS}/`, '')
)} doesn't have a root layout. To fix this error, make sure every page has a root layout.`
)
process.exit(1)
} else {
// In dev we'll try to create a root layout
const [createdRootLayout, rootLayoutPath] = await verifyRootLayout({
appDir: appDir,
dir: rootDir!,
tsconfigPath: tsconfigPath!,
pagePath,
pageExtensions,
})
if (!createdRootLayout) {
let message = `${chalk.bold(
pagePath.replace(`${APP_DIR_ALIAS}/`, '')
)} doesn't have a root layout. `
if (rootLayoutPath) {
message += `We tried to create ${chalk.bold(
path.relative(this._compiler?.context ?? '', rootLayoutPath)
)} for you but something went wrong.`
} else {
message +=
'To fix this error, make sure every page has a root layout.'
}
throw new Error(message)
}
}
}
const result = `
export ${treeCode}
export ${pageListCode}
export { default as AppRouter } from 'next/dist/client/components/app-router'
export { default as LayoutRouter } from 'next/dist/client/components/layout-router'
export { default as RenderFromTemplateContext } from 'next/dist/client/components/render-from-template-context'
export { default as GlobalError } from ${JSON.stringify(
globalError || 'next/dist/client/components/error-boundary'
)}
export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage'
export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage'
export { staticGenerationBailout } from 'next/dist/client/components/static-generation-bailout'
export { default as StaticGenerationSearchParamsBailoutProvider } from 'next/dist/client/components/static-generation-searchparams-bailout-provider'
export { createSearchParamsBailoutProxy } from 'next/dist/client/components/searchparams-bailout-proxy'
export * as serverHooks from 'next/dist/client/components/hooks-server-context'
export { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/server.edge'
export const __next_app_webpack_require__ = __webpack_require__
`
return result
}
export default nextAppLoader