Skip to content

Commit 958d0ac

Browse files
committed
refactor: enforce typings on component modules
1 parent feca3ce commit 958d0ac

29 files changed

+114
-119
lines changed

packages/next/src/build/templates/app-page.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { LoaderTree } from '../../server/lib/app-dir-module'
22

3-
// @ts-ignore this need to be imported from next/dist to be external
4-
import * as module from 'next/dist/server/future/route-modules/app-page/module.compiled'
3+
import { AppPageRouteModule } from '../../server/future/route-modules/app-page/module.compiled'
54
import { RouteKind } from '../../server/future/route-kind'
65

7-
const AppPageRouteModule =
8-
module.AppPageRouteModule as unknown as typeof import('../../server/future/route-modules/app-page/module').AppPageRouteModule
9-
106
// These are injected by the loader afterwards.
7+
8+
/**
9+
* The tree created in next-app-loader that holds component segments and modules
10+
* and I've updated it.
11+
*/
1112
declare const tree: LoaderTree
1213
declare const pages: any
1314

@@ -18,7 +19,6 @@ declare const pages: any
1819

1920
export { tree, pages }
2021

21-
// @ts-expect-error - replaced by webpack/turbopack loader
2222
export { default as GlobalError } from 'VAR_MODULE_GLOBAL_ERROR'
2323

2424
// These are injected by the loader afterwards.

packages/next/src/build/templates/app-route.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import '../../server/node-polyfill-headers'
22

3-
// @ts-ignore this need to be imported from next/dist to be external
4-
import * as module from 'next/dist/server/future/route-modules/app-route/module.compiled'
5-
6-
import type { AppRouteRouteModuleOptions } from '../../server/future/route-modules/app-route/module'
3+
import {
4+
AppRouteRouteModule,
5+
type AppRouteRouteModuleOptions,
6+
} from '../../server/future/route-modules/app-route/module.compiled'
77
import { RouteKind } from '../../server/future/route-kind'
88

9-
// @ts-expect-error - replaced by webpack/turbopack loader
109
import * as userland from 'VAR_USERLAND'
1110

12-
const AppRouteRouteModule =
13-
module.AppRouteRouteModule as unknown as typeof import('../../server/future/route-modules/app-route/module').AppRouteRouteModule
14-
1511
// These are injected by the loader afterwards. This is injected as a variable
1612
// instead of a replacement because this could also be `undefined` instead of
1713
// an empty string.

packages/next/src/build/templates/edge-app-route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { EdgeRouteModuleWrapper } from '../../server/web/edge-route-module-wrapper'
22

33
// Import the userland code.
4-
// @ts-expect-error - replaced by webpack/turbopack loader
54
import * as module from 'VAR_USERLAND'
65

76
export const ComponentMod = module

packages/next/src/build/templates/middleware.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import '../../server/web/globals'
21
import type { AdapterOptions } from '../../server/web/adapter'
2+
3+
import '../../server/web/globals'
4+
35
import { adapter } from '../../server/web/adapter'
46

57
// Import the userland code.
6-
// @ts-expect-error - replaced by webpack/turbopack loader
78
import * as _mod from 'VAR_USERLAND'
89

910
const mod = { ..._mod }

packages/next/src/build/templates/pages-api.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
// @ts-ignore this need to be imported from next/dist to be external
2-
import * as module from 'next/dist/server/future/route-modules/pages-api/module.compiled'
3-
1+
import { PagesAPIRouteModule } from '../../server/future/route-modules/pages-api/module.compiled'
42
import { RouteKind } from '../../server/future/route-kind'
5-
import { hoist } from './helpers'
63

7-
const PagesAPIRouteModule =
8-
module.PagesAPIRouteModule as unknown as typeof import('../../server/future/route-modules/pages-api/module').PagesAPIRouteModule
4+
import { hoist } from './helpers'
95

106
// Import the userland code.
11-
// @ts-expect-error - replaced by webpack/turbopack loader
127
import * as userland from 'VAR_USERLAND'
138

149
// Re-export the handler (should be the default export).

packages/next/src/build/templates/pages-edge-api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import '../../server/web/globals'
21
import type { AdapterOptions } from '../../server/web/adapter'
2+
3+
import '../../server/web/globals'
4+
35
import { adapter } from '../../server/web/adapter'
46
import { IncrementalCache } from '../../server/lib/incremental-cache'
57

68
// Import the userland code.
7-
// @ts-expect-error - replaced by webpack/turbopack loader
89
import handler from 'VAR_USERLAND'
910

1011
const page = 'VAR_DEFINITION_PAGE'

packages/next/src/build/templates/pages.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
// @ts-ignore this need to be imported from next/dist to be external
2-
import * as module from 'next/dist/server/future/route-modules/pages/module.compiled'
1+
import { PagesRouteModule } from '../../server/future/route-modules/pages/module.compiled'
32
import { RouteKind } from '../../server/future/route-kind'
43
import { hoist } from './helpers'
54

65
// Import the app and document modules.
7-
// @ts-expect-error - replaced by webpack/turbopack loader
86
import Document from 'VAR_MODULE_DOCUMENT'
9-
// @ts-expect-error - replaced by webpack/turbopack loader
107
import App from 'VAR_MODULE_APP'
118

129
// Import the userland code.
13-
// @ts-expect-error - replaced by webpack/turbopack loader
1410
import * as userland from 'VAR_USERLAND'
1511

16-
const PagesRouteModule =
17-
module.PagesRouteModule as unknown as typeof import('../../server/future/route-modules/pages/module').PagesRouteModule
18-
1912
// Re-export the component (should be the default export).
2013
export default hoist(userland, 'default')
2114

packages/next/src/build/utils.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
EdgeFunctionDefinition,
1515
MiddlewareManifest,
1616
} from './webpack/plugins/middleware-plugin'
17-
import type { StaticGenerationAsyncStorage } from '../client/components/static-generation-async-storage.external'
1817
import type { WebpackLayerName } from '../lib/constants'
1918

2019
import '../server/require-hook'
@@ -68,6 +67,7 @@ import { nodeFs } from '../server/lib/node-fs-methods'
6867
import * as ciEnvironment from '../telemetry/ci-info'
6968
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
7069
import { denormalizeAppPagePath } from '../shared/lib/page-path/denormalize-app-path'
70+
import { AppPageModule } from '../server/future/route-modules/app-page/module'
7171

7272
const { AppRouteRouteModule } =
7373
require('../server/future/route-modules/app-route/module.compiled') as typeof import('../server/future/route-modules/app-route/module')
@@ -1469,25 +1469,12 @@ export async function isPageStatic({
14691469
| undefined
14701470

14711471
if (pageType === 'app') {
1472-
isClientComponent = isClientReference(componentsResult.ComponentMod)
1473-
const tree = componentsResult.ComponentMod.tree
1474-
1475-
const staticGenerationAsyncStorage: StaticGenerationAsyncStorage =
1476-
componentsResult.ComponentMod.staticGenerationAsyncStorage
1477-
if (!staticGenerationAsyncStorage) {
1478-
throw new Error(
1479-
'Invariant: staticGenerationAsyncStorage should be defined on the module'
1480-
)
1481-
}
1472+
const ComponentMod: AppPageModule = componentsResult.ComponentMod
14821473

1483-
const serverHooks = componentsResult.ComponentMod.serverHooks
1484-
if (!serverHooks) {
1485-
throw new Error(
1486-
'Invariant: serverHooks should be defined on the module'
1487-
)
1488-
}
1474+
isClientComponent = isClientReference(componentsResult.ComponentMod)
14891475

1490-
const { routeModule } = componentsResult
1476+
const { tree, staticGenerationAsyncStorage, serverHooks, routeModule } =
1477+
ComponentMod
14911478

14921479
const generateParams: GenerateParams =
14931480
routeModule && AppRouteRouteModule.is(routeModule)

packages/next/src/server/app-render/app-render.tsx

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import type {
1010
RenderOpts,
1111
Segment,
1212
} from './types'
13-
import type { StaticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage.external'
14-
import type { StaticGenerationBailout } from '../../client/components/static-generation-bailout'
15-
import type { RequestAsyncStorage } from '../../client/components/request-async-storage.external'
1613

1714
import React from 'react'
1815
import { createServerComponentRenderer } from './create-server-components-renderer'
@@ -237,6 +234,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
237234
})
238235

239236
patchFetch(ComponentMod)
237+
240238
/**
241239
* Rules of Static & Dynamic HTML:
242240
*
@@ -252,12 +250,25 @@ export const renderToHTMLOrFlight: AppPageRender = (
252250
*/
253251
const generateStaticHTML = supportsDynamicHTML !== true
254252

255-
const staticGenerationAsyncStorage: StaticGenerationAsyncStorage =
256-
ComponentMod.staticGenerationAsyncStorage
257-
const requestAsyncStorage: RequestAsyncStorage =
258-
ComponentMod.requestAsyncStorage
259-
const staticGenerationBailout: StaticGenerationBailout =
260-
ComponentMod.staticGenerationBailout
253+
// Pull out the hooks/references from the component.
254+
const {
255+
staticGenerationAsyncStorage,
256+
requestAsyncStorage,
257+
staticGenerationBailout,
258+
LayoutRouter,
259+
RenderFromTemplateContext,
260+
createSearchParamsBailoutProxy,
261+
StaticGenerationSearchParamsBailoutProvider,
262+
serverHooks: { DynamicServerError },
263+
NotFoundBoundary,
264+
renderToReadableStream,
265+
AppRouter,
266+
GlobalError,
267+
tree: loaderTree,
268+
preloadFont,
269+
preconnect,
270+
preloadStyle,
271+
} = ComponentMod
261272

262273
// we wrap the render in an AsyncLocalStorage context
263274
const wrappedRender = async () => {
@@ -294,11 +305,6 @@ export const renderToHTMLOrFlight: AppPageRender = (
294305
)
295306
: undefined
296307

297-
/**
298-
* The tree created in next-app-loader that holds component segments and modules
299-
*/
300-
const loaderTree: LoaderTree = ComponentMod.tree
301-
302308
/**
303309
* The metadata items array created in next-app-loader with all relevant information
304310
* that we need to resolve the final metadata.
@@ -312,15 +318,6 @@ export const renderToHTMLOrFlight: AppPageRender = (
312318
requestId = require('next/dist/compiled/nanoid').nanoid()
313319
}
314320

315-
const LayoutRouter =
316-
ComponentMod.LayoutRouter as typeof import('../../client/components/layout-router').default
317-
const RenderFromTemplateContext =
318-
ComponentMod.RenderFromTemplateContext as typeof import('../../client/components/render-from-template-context').default
319-
const createSearchParamsBailoutProxy =
320-
ComponentMod.createSearchParamsBailoutProxy as typeof import('../../client/components/searchparams-bailout-proxy').createSearchParamsBailoutProxy
321-
const StaticGenerationSearchParamsBailoutProvider =
322-
ComponentMod.StaticGenerationSearchParamsBailoutProvider as typeof import('../../client/components/static-generation-searchparams-bailout-provider').default
323-
324321
const isStaticGeneration = staticGenerationStore.isStaticGeneration
325322
// During static generation we need to call the static generation bailout when reading searchParams
326323
const providedSearchParams = isStaticGeneration
@@ -511,16 +508,16 @@ export const renderToHTMLOrFlight: AppPageRender = (
511508
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(fontFilename)![1]
512509
const type = `font/${ext}`
513510
const href = `${assetPrefix}/_next/${fontFilename}`
514-
ComponentMod.preloadFont(href, type, renderOpts.crossOrigin)
511+
preloadFont(href, type, renderOpts.crossOrigin)
515512
}
516513
} else {
517514
try {
518515
let url = new URL(assetPrefix)
519-
ComponentMod.preconnect(url.origin, 'anonymous')
516+
preconnect(url.origin, 'anonymous')
520517
} catch (error) {
521518
// assetPrefix must not be a fully qualified domain name. We assume
522519
// we should preconnect to same origin instead
523-
ComponentMod.preconnect('/', 'anonymous')
520+
preconnect('/', 'anonymous')
524521
}
525522
}
526523
}
@@ -546,7 +543,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
546543
const precedence =
547544
process.env.NODE_ENV === 'development' ? 'next_' + href : 'next'
548545

549-
ComponentMod.preloadStyle(fullHref, renderOpts.crossOrigin)
546+
preloadStyle(fullHref, renderOpts.crossOrigin)
550547

551548
return (
552549
<link
@@ -732,9 +729,6 @@ export const renderToHTMLOrFlight: AppPageRender = (
732729
staticGenerationStore.isStaticGeneration &&
733730
defaultRevalidate === 0
734731
) {
735-
const { DynamicServerError } =
736-
ComponentMod.serverHooks as typeof import('../../client/components/hooks-server-context')
737-
738732
const dynamicUsageDescription = `revalidate: 0 configured ${segment}`
739733
staticGenerationStore.dynamicUsageDescription =
740734
dynamicUsageDescription
@@ -759,8 +753,6 @@ export const renderToHTMLOrFlight: AppPageRender = (
759753
const hasSlotKey = parallelKeys.length > 1
760754

761755
if (hasSlotKey && rootLayoutAtThisLevel) {
762-
const NotFoundBoundary =
763-
ComponentMod.NotFoundBoundary as typeof import('../../client/components/not-found-boundary').NotFoundBoundary
764756
Component = (componentProps: any) => {
765757
const NotFoundComponent = NotFound
766758
const RootLayoutComponent = LayoutOrPage
@@ -1287,7 +1279,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
12871279

12881280
// For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
12891281
// which contains the subset React.
1290-
const flightReadableStream = ComponentMod.renderToReadableStream(
1282+
const flightReadableStream = renderToReadableStream(
12911283
options
12921284
? [options.actionResult, buildIdFlightDataPair]
12931285
: buildIdFlightDataPair,
@@ -1305,16 +1297,6 @@ export const renderToHTMLOrFlight: AppPageRender = (
13051297
return generateFlight()
13061298
}
13071299

1308-
// Below this line is handling for rendering to HTML.
1309-
1310-
// AppRouter is provided by next-app-loader
1311-
const AppRouter =
1312-
ComponentMod.AppRouter as typeof import('../../client/components/app-router').default
1313-
1314-
const GlobalError =
1315-
/** GlobalError can be either the default error boundary or the overwritten app/global-error.js **/
1316-
ComponentMod.GlobalError as typeof import('../../client/components/error-boundary').GlobalError
1317-
13181300
// Get the nonce from the incoming request if it has one.
13191301
const csp = req.headers['content-security-policy']
13201302
let nonce: string | undefined

packages/next/src/server/app-render/create-server-components-renderer.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RenderOpts } from './types'
22
import type { FlightResponseRef } from './flight-response-ref'
3+
import type { AppPageModule } from '../future/route-modules/app-page/module'
34

45
import React, { use } from 'react'
56
import { createErrorHandler } from './create-error-handler'
@@ -11,13 +12,7 @@ import { useFlightResponse } from './use-flight-response'
1112
*/
1213
export function createServerComponentRenderer<Props>(
1314
ComponentToRender: (props: Props) => any,
14-
ComponentMod: {
15-
renderToReadableStream: any
16-
__next_app__?: {
17-
require: any
18-
loadChunk: any
19-
}
20-
},
15+
ComponentMod: AppPageModule,
2116
{
2217
inlinedDataTransformStream,
2318
clientReferenceManifest,

packages/next/src/server/app-render/entry-base.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const {
1+
export {
22
renderToReadableStream,
33
decodeReply,
44
decodeAction,
55
decodeFormState,
66
// eslint-disable-next-line import/no-extraneous-dependencies
7-
} = require('react-server-dom-webpack/server.edge')
7+
} from 'react-server-dom-webpack/server.edge'
88

99
import AppRouter from '../../client/components/app-router'
1010
import LayoutRouter from '../../client/components/layout-router'
@@ -36,10 +36,6 @@ export {
3636
staticGenerationBailout,
3737
createSearchParamsBailoutProxy,
3838
serverHooks,
39-
renderToReadableStream,
40-
decodeReply,
41-
decodeAction,
42-
decodeFormState,
4339
preloadStyle,
4440
preloadFont,
4541
preconnect,

packages/next/src/server/app-render/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { LoadComponentsReturnType } from '../load-components'
22
import type { ServerRuntime, SizeLimit } from '../../../types'
3-
import { NextConfigComplete } from '../../server/config-shared'
3+
import type { NextConfigComplete } from '../../server/config-shared'
44
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'
55
import type { NextFontManifest } from '../../build/webpack/plugins/next-font-manifest-plugin'
66
import type { ParsedUrlQuery } from 'querystring'
7+
import type { AppPageModule } from '../future/route-modules/app-page/module'
78

89
import s from 'next/dist/compiled/superstruct'
910

@@ -135,4 +136,5 @@ export interface RenderOptsPartial {
135136
isPrefetch?: boolean
136137
}
137138

138-
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
139+
export type RenderOpts = LoadComponentsReturnType<AppPageModule> &
140+
RenderOptsPartial
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './module'

0 commit comments

Comments
 (0)