-
Notifications
You must be signed in to change notification settings - Fork 26k
/
flight-manifest-plugin.ts
409 lines (356 loc) · 13.1 KB
/
flight-manifest-plugin.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import {
CLIENT_REFERENCE_MANIFEST,
SYSTEM_ENTRYPOINTS,
} from '../../../shared/lib/constants'
import { relative } from 'path'
import { isClientComponentEntryModule, isCSSMod } from '../loaders/utils'
import { getProxiedPluginState } from '../../build-context'
import { traverseModules } from '../utils'
import { nonNullable } from '../../../lib/non-nullable'
import { WEBPACK_LAYERS } from '../../../lib/constants'
import { getClientReferenceModuleKey } from '../../../lib/client-reference'
interface Options {
dev: boolean
appDir: string
}
/**
* Webpack module id
*/
// TODO-APP ensure `null` is included as it is used.
type ModuleId = string | number /*| null*/
export type ManifestChunks = Array<`${string}:${string}` | string>
const pluginState = getProxiedPluginState({
serverModuleIds: {} as Record<string, string | number>,
edgeServerModuleIds: {} as Record<string, string | number>,
ASYNC_CLIENT_MODULES: [] as string[],
})
export interface ManifestNode {
[moduleExport: string]: {
/**
* Webpack module id
*/
id: ModuleId
/**
* Export name
*/
name: string
/**
* Chunks for the module. JS and CSS.
*/
chunks: ManifestChunks
/**
* If chunk contains async module
*/
async?: boolean
}
}
export type ClientReferenceManifest = {
clientModules: ManifestNode
ssrModuleMapping: {
[moduleId: string]: ManifestNode
}
edgeSSRModuleMapping: {
[moduleId: string]: ManifestNode
}
}
export type ClientCSSReferenceManifest = {
cssImports: {
[modulePath: string]: string[]
}
cssModules?: {
[entry: string]: string[]
}
}
const PLUGIN_NAME = 'ClientReferenceManifestPlugin'
export class ClientReferenceManifestPlugin {
dev: Options['dev'] = false
appDir: Options['appDir']
ASYNC_CLIENT_MODULES: Set<string>
constructor(options: Options) {
this.dev = options.dev
this.appDir = options.appDir
this.ASYNC_CLIENT_MODULES = new Set(pluginState.ASYNC_CLIENT_MODULES)
}
apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(
webpack.dependencies.ModuleDependency,
normalModuleFactory
)
compilation.dependencyTemplates.set(
webpack.dependencies.ModuleDependency,
new webpack.dependencies.NullDependency.Template()
)
}
)
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
// Have to be in the optimize stage to run after updating the CSS
// asset hash via extract mini css plugin.
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
},
(assets) => this.createAsset(assets, compilation, compiler.context)
)
})
}
createAsset(
assets: webpack.Compilation['assets'],
compilation: webpack.Compilation,
context: string
) {
const manifest: ClientReferenceManifest = {
ssrModuleMapping: {},
edgeSSRModuleMapping: {},
clientModules: {},
}
const clientRequestsSet = new Set()
// Collect client requests
function collectClientRequest(mod: webpack.NormalModule) {
if (mod.resource === '' && mod.buildInfo.rsc) {
const { requests = [] } = mod.buildInfo.rsc
requests.forEach((r: string) => {
clientRequestsSet.add(r)
})
}
}
traverseModules(compilation, (mod) => collectClientRequest(mod))
compilation.chunkGroups.forEach((chunkGroup) => {
function getAppPathRequiredChunks() {
return chunkGroup.chunks
.map((requiredChunk: webpack.Chunk) => {
if (SYSTEM_ENTRYPOINTS.has(requiredChunk.name)) {
return null
}
// Get the actual chunk file names from the chunk file list.
// It's possible that the chunk is generated via `import()`, in
// that case the chunk file name will be '[name].[contenthash]'
// instead of '[name]-[chunkhash]'.
return [...requiredChunk.files].map((file) => {
// It's possible that a chunk also emits CSS files, that will
// be handled separatedly.
if (!file.endsWith('.js')) return null
if (file.endsWith('.hot-update.js')) return null
return requiredChunk.id + ':' + file
})
})
.flat()
.filter(nonNullable)
}
const requiredChunks = getAppPathRequiredChunks()
const recordModule = (
id: ModuleId,
mod: webpack.NormalModule,
chunkCSS: string[]
) => {
const isCSSModule = isCSSMod(mod)
// Skip all modules from the pages folder. CSS modules are a special case
// as they are generated by mini-css-extract-plugin and these modules
// don't have layer information attached.
if (!isCSSModule && mod.layer !== WEBPACK_LAYERS.appClient) {
return
}
const resource =
mod.type === 'css/mini-extract'
? // @ts-expect-error TODO: use `identifier()` instead.
mod._identifier.slice(mod._identifier.lastIndexOf('!') + 1)
: mod.resource
if (!resource) {
return
}
const moduleReferences = manifest.clientModules
const moduleIdMapping = manifest.ssrModuleMapping
const edgeModuleIdMapping = manifest.edgeSSRModuleMapping
// Note that this isn't that reliable as webpack is still possible to assign
// additional queries to make sure there's no conflict even using the `named`
// module ID strategy.
let ssrNamedModuleId = relative(
context,
mod.resourceResolveData?.path || resource
)
if (!ssrNamedModuleId.startsWith('.'))
ssrNamedModuleId = `./${ssrNamedModuleId.replace(/\\/g, '/')}`
if (isCSSModule) {
const exportName = getClientReferenceModuleKey(resource, '')
if (!moduleReferences[exportName]) {
moduleReferences[exportName] = {
id: id || '',
name: 'default',
chunks: chunkCSS,
}
} else {
// It is possible that there are multiple modules with the same resource,
// e.g. extracted by mini-css-extract-plugin. In that case we need to
// merge the chunks.
moduleReferences[exportName].chunks = [
...new Set([...moduleReferences[exportName].chunks, ...chunkCSS]),
]
}
return
}
// Only apply following logic to client module requests from client entry,
// or if the module is marked as client module.
if (
!clientRequestsSet.has(resource) &&
!isClientComponentEntryModule(mod)
) {
return
}
const exportsInfo = compilation.moduleGraph.getExportsInfo(mod)
const isAsyncModule = this.ASYNC_CLIENT_MODULES.has(mod.resource)
const cjsExports = [
...new Set(
[
...mod.dependencies.map((dep) => {
// Match CommonJsSelfReferenceDependency
if (dep.type === 'cjs self exports reference') {
// @ts-expect-error: TODO: Fix Dependency type
if (dep.base === 'module.exports') {
return 'default'
}
// `exports.foo = ...`, `exports.default = ...`
// @ts-expect-error: TODO: Fix Dependency type
if (dep.base === 'exports') {
// @ts-expect-error: TODO: Fix Dependency type
return dep.names.filter(
(name: any) => name !== '__esModule'
)
}
}
return null
}),
...(mod.buildInfo.rsc?.clientRefs || []),
]
.filter(Boolean)
.flat()
),
]
// The client compiler will always use the CJS Next.js build, so here we
// also add the mapping for the ESM build (Edge runtime) to consume.
const esmResource = /[\\/]next[\\/]dist[\\/]/.test(resource)
? resource.replace(
/[\\/]next[\\/]dist[\\/]/,
'/next/dist/esm/'.replace(/\//g, path.sep)
)
: null
function addClientReference(name: string) {
const exportName = getClientReferenceModuleKey(resource, name)
manifest.clientModules[exportName] = {
id,
name,
chunks: requiredChunks,
async: isAsyncModule,
}
if (esmResource) {
const edgeExportName = getClientReferenceModuleKey(
esmResource,
name
)
manifest.clientModules[edgeExportName] =
manifest.clientModules[exportName]
}
}
function addSSRIdMapping(name: string) {
const exportName = getClientReferenceModuleKey(resource, name)
if (
typeof pluginState.serverModuleIds[ssrNamedModuleId] !== 'undefined'
) {
moduleIdMapping[id] = moduleIdMapping[id] || {}
moduleIdMapping[id][name] = {
...manifest.clientModules[exportName],
// During SSR, we don't have external chunks to load on the server
// side with our architecture of Webpack / Turbopack. We can keep
// this field empty to save some bytes.
chunks: [],
id: pluginState.serverModuleIds[ssrNamedModuleId],
}
}
if (
typeof pluginState.edgeServerModuleIds[ssrNamedModuleId] !==
'undefined'
) {
edgeModuleIdMapping[id] = edgeModuleIdMapping[id] || {}
edgeModuleIdMapping[id][name] = {
...manifest.clientModules[exportName],
// During SSR, we don't have external chunks to load on the server
// side with our architecture of Webpack / Turbopack. We can keep
// this field empty to save some bytes.
chunks: [],
id: pluginState.edgeServerModuleIds[ssrNamedModuleId],
}
}
}
addClientReference('*')
addClientReference('')
addSSRIdMapping('*')
addSSRIdMapping('')
const moduleExportedKeys = [
...[...exportsInfo.exports]
.filter((exportInfo) => exportInfo.provided)
.map((exportInfo) => exportInfo.name),
...cjsExports,
].filter((name) => name !== null)
moduleExportedKeys.forEach((name) => {
const key = getClientReferenceModuleKey(resource, name)
// If the chunk is from `app/` chunkGroup, use it first.
// This make sure not to load the overlapped chunk from `pages/` chunkGroup
if (
!manifest.clientModules[key] ||
(chunkGroup.name && /^app[\\/]/.test(chunkGroup.name))
) {
addClientReference(name)
}
addSSRIdMapping(name)
})
manifest.clientModules = moduleReferences
manifest.ssrModuleMapping = moduleIdMapping
manifest.edgeSSRModuleMapping = edgeModuleIdMapping
}
chunkGroup.chunks.forEach((chunk: webpack.Chunk) => {
const chunkModules = compilation.chunkGraph.getChunkModulesIterable(
chunk
// TODO: Update type so that it doesn't have to be cast.
) as Iterable<webpack.NormalModule>
const chunkCSS = [...chunk.files].filter(
(f) => !f.startsWith('static/css/pages/') && f.endsWith('.css')
)
for (const mod of chunkModules) {
const modId: string = compilation.chunkGraph.getModuleId(mod) + ''
recordModule(modId, mod, chunkCSS)
// If this is a concatenation, register each child to the parent ID.
// TODO: remove any
const anyModule = mod as any
if (anyModule.modules) {
anyModule.modules.forEach((concatenatedMod: any) => {
recordModule(modId, concatenatedMod, chunkCSS)
})
}
}
})
})
const file = 'server/' + CLIENT_REFERENCE_MANIFEST
const json = JSON.stringify(manifest, null, this.dev ? 2 : undefined)
pluginState.ASYNC_CLIENT_MODULES = []
assets[file + '.js'] = new sources.RawSource(
'self.__RSC_MANIFEST=' + json
// Work around webpack 4 type of RawSource being used
// TODO: use webpack 5 type by default
) as unknown as webpack.sources.RawSource
assets[file + '.json'] = new sources.RawSource(
json
// Work around webpack 4 type of RawSource being used
// TODO: use webpack 5 type by default
) as unknown as webpack.sources.RawSource
}
}