forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight-client-entry-plugin.ts
908 lines (803 loc) · 28.7 KB
/
flight-client-entry-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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
import type {
CssImports,
ClientComponentImports,
NextFlightClientEntryLoaderOptions,
} from '../loaders/next-flight-client-entry-loader'
import type { ClientCSSReferenceManifest } from './flight-manifest-plugin'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { stringify } from 'querystring'
import path from 'path'
import { sources } from 'next/dist/compiled/webpack/webpack'
import {
getInvalidator,
getEntries,
EntryTypes,
getEntryKey,
} from '../../../server/dev/on-demand-entry-handler'
import { WEBPACK_LAYERS } from '../../../lib/constants'
import {
APP_CLIENT_INTERNALS,
COMPILER_NAMES,
EDGE_RUNTIME_WEBPACK,
SERVER_REFERENCE_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST,
} from '../../../shared/lib/constants'
import {
generateActionId,
getActions,
isClientComponentEntryModule,
isCSSMod,
} from '../loaders/utils'
import { traverseModules, forEachEntryModule } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { getProxiedPluginState } from '../../build-context'
interface Options {
dev: boolean
appDir: string
isEdgeServer: boolean
useServerActions: boolean
}
const PLUGIN_NAME = 'ClientEntryPlugin'
export type ActionManifest = {
[key in 'node' | 'edge']: {
[actionId: string]: {
workers: {
[name: string]: string | number
}
// Record which layer the action is in (sc_server or sc_action), in the specific entry.
layer: {
[name: string]: string
}
}
}
}
const pluginState = getProxiedPluginState({
// A map to track "action" -> "list of bundles".
serverActions: {} as ActionManifest['node'],
edgeServerActions: {} as ActionManifest['edge'],
actionModServerId: {} as Record<
string,
{
server?: string | number
client?: string | number
}
>,
actionModEdgeServerId: {} as Record<
string,
{
server?: string | number
client?: string | number
}
>,
// Manifest of CSS entry files for server/edge server.
serverCSSManifest: {} as ClientCSSReferenceManifest,
edgeServerCSSManifest: {} as ClientCSSReferenceManifest,
// Mapping of resource path to module id for server/edge server.
serverModuleIds: {} as Record<string, string | number>,
edgeServerModuleIds: {} as Record<string, string | number>,
// Collect modules from server/edge compiler in client layer,
// and detect if it's been used, and mark it as `async: true` for react.
// So that react could unwrap the async module from promise and render module itself.
ASYNC_CLIENT_MODULES: [] as string[],
injectedClientEntries: {} as Record<string, string>,
})
export class ClientReferenceEntryPlugin {
dev: boolean
appDir: string
isEdgeServer: boolean
useServerActions: boolean
assetPrefix: string
constructor(options: Options) {
this.dev = options.dev
this.appDir = options.appDir
this.isEdgeServer = options.isEdgeServer
this.useServerActions = options.useServerActions
this.assetPrefix = !this.dev && !this.isEdgeServer ? '../' : ''
}
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.finishMake.tapPromise(PLUGIN_NAME, (compilation) =>
this.createClientEntries(compiler, compilation)
)
compiler.hooks.afterCompile.tap(PLUGIN_NAME, (compilation) => {
const recordModule = (modId: string, mod: any) => {
const modResource = mod.resourceResolveData?.path || mod.resource
if (mod.layer !== WEBPACK_LAYERS.client) {
return
}
// Check mod resource to exclude the empty resource module like virtual module created by next-flight-client-entry-loader
if (typeof modId !== 'undefined' && modResource) {
// 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 = path.relative(compiler.context, modResource)
if (!ssrNamedModuleId.startsWith('.')) {
// TODO use getModuleId instead
ssrNamedModuleId = `./${normalizePathSep(ssrNamedModuleId)}`
}
if (this.isEdgeServer) {
pluginState.edgeServerModuleIds[
ssrNamedModuleId.replace(/\/next\/dist\/esm\//, '/next/dist/')
] = modId
} else {
pluginState.serverModuleIds[ssrNamedModuleId] = modId
}
}
}
traverseModules(compilation, (mod, _chunk, _chunkGroup, modId) => {
// The module must has request, and resource so it's not a new entry created with loader.
// Using the client layer module, which doesn't have `rsc` tag in buildInfo.
if (mod.request && mod.resource && !mod.buildInfo.rsc) {
if (compilation.moduleGraph.isAsync(mod)) {
pluginState.ASYNC_CLIENT_MODULES.push(mod.resource)
}
}
recordModule(String(modId), mod)
})
})
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
},
(assets) => this.createActionAssets(compilation, assets)
)
})
}
async createClientEntries(compiler: webpack.Compiler, compilation: any) {
const addClientEntryAndSSRModulesList: Array<
ReturnType<typeof this.injectClientEntryAndSSRModules>
> = []
const addActionEntryList: Array<ReturnType<typeof this.injectActionEntry>> =
[]
const actionMapsPerEntry: Record<string, Map<string, string[]>> = {}
// For each SC server compilation entry, we need to create its corresponding
// client component entry.
forEachEntryModule(compilation, ({ name, entryModule }) => {
const internalClientComponentEntryImports = new Set<
ClientComponentImports[0]
>()
const actionEntryImports = new Map<string, string[]>()
for (const connection of compilation.moduleGraph.getOutgoingConnections(
entryModule
)) {
// Entry can be any user defined entry files such as layout, page, error, loading, etc.
const entryDependency = connection.dependency
const entryRequest = connection.dependency.request
const { clientImports, actionImports } =
this.collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency: entryDependency,
})
actionImports.forEach(([dep, names]) =>
actionEntryImports.set(dep, names)
)
const isAbsoluteRequest = path.isAbsolute(entryRequest)
// Next.js internals are put into a separate entry.
if (!isAbsoluteRequest) {
clientImports.forEach((value) =>
internalClientComponentEntryImports.add(value)
)
continue
}
const relativeRequest = isAbsoluteRequest
? path.relative(compilation.options.context, entryRequest)
: entryRequest
// Replace file suffix as `.js` will be added.
const bundlePath = normalizePathSep(
relativeRequest.replace(/\.[^.\\/]+$/, '').replace(/^src[\\/]/, '')
)
addClientEntryAndSSRModulesList.push(
this.injectClientEntryAndSSRModules({
compiler,
compilation,
entryName: name,
clientImports,
bundlePath,
absolutePagePath: entryRequest,
})
)
}
// Create internal app
addClientEntryAndSSRModulesList.push(
this.injectClientEntryAndSSRModules({
compiler,
compilation,
entryName: name,
clientImports: [...internalClientComponentEntryImports],
bundlePath: APP_CLIENT_INTERNALS,
})
)
if (actionEntryImports.size > 0) {
if (!this.useServerActions) {
compilation.errors.push(
new Error(
'Server Actions require `experimental.serverActions` option to be enabled in your Next.js config: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions'
)
)
} else {
if (!actionMapsPerEntry[name]) {
actionMapsPerEntry[name] = new Map()
}
actionMapsPerEntry[name] = new Map([
...actionMapsPerEntry[name],
...actionEntryImports,
])
}
}
})
for (const [name, actionEntryImports] of Object.entries(
actionMapsPerEntry
)) {
addActionEntryList.push(
this.injectActionEntry({
compiler,
compilation,
actions: actionEntryImports,
entryName: name,
bundlePath: name,
})
)
}
// To collect all CSS imports and action imports for a specific entry
// including the ones that are in the client graph, we need to store a
// map for client boundary dependencies.
function collectClientEntryDependencyMap(name: string) {
const clientEntryDependencyMap: Record<string, any> = {}
const entry = compilation.entries.get(name)
entry.includeDependencies.forEach((dep: any) => {
if (
dep.request &&
dep.request.startsWith('next-flight-client-entry-loader?')
) {
const mod: webpack.NormalModule =
compilation.moduleGraph.getResolvedModule(dep)
compilation.moduleGraph
.getOutgoingConnections(mod)
.forEach((connection: any) => {
if (connection.dependency) {
clientEntryDependencyMap[connection.dependency.request] =
connection.dependency
}
})
}
})
return clientEntryDependencyMap
}
// We need to create extra action entries that are created in the
// client layer.
compilation.hooks.finishModules.tapPromise(PLUGIN_NAME, () => {
const addedClientActionEntryList: Promise<any>[] = []
const actionMapsPerClientEntry: Record<string, Map<string, string[]>> = {}
forEachEntryModule(compilation, ({ name, entryModule }) => {
const actionEntryImports = new Map<string, string[]>()
const clientEntryDependencyMap = collectClientEntryDependencyMap(name)
const tracked = new Set<string>()
for (const connection of compilation.moduleGraph.getOutgoingConnections(
entryModule
)) {
const entryDependency = connection.dependency
const entryRequest = connection.dependency.request
// It is possible that the same entry is added multiple times in the
// connection graph. We can just skip these to speed up the process.
if (tracked.has(entryRequest)) continue
tracked.add(entryRequest)
const { clientActionImports } =
this.collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency: entryDependency,
clientEntryDependencyMap,
})
clientActionImports.forEach(([dep, names]) =>
actionEntryImports.set(dep, names)
)
}
if (actionEntryImports.size > 0) {
if (!this.useServerActions) {
compilation.errors.push(
new Error(
'Server Actions require `experimental.serverActions` option to be enabled in your Next.js config: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions'
)
)
} else {
if (!actionMapsPerClientEntry[name]) {
actionMapsPerClientEntry[name] = new Map()
}
actionMapsPerClientEntry[name] = new Map([
...actionMapsPerClientEntry[name],
...actionEntryImports,
])
}
}
})
for (const [name, actionEntryImports] of Object.entries(
actionMapsPerClientEntry
)) {
addedClientActionEntryList.push(
this.injectActionEntry({
compiler,
compilation,
actions: actionEntryImports,
entryName: name,
bundlePath: name,
fromClient: true,
})
)
}
return Promise.all(addedClientActionEntryList)
})
// After optimizing all the modules, we collect the CSS that are still used
// by the certain chunk.
compilation.hooks.afterOptimizeModules.tap(PLUGIN_NAME, () => {
const cssImportsForChunk: Record<string, Set<string>> = {}
const cssManifest = this.isEdgeServer
? pluginState.edgeServerCSSManifest
: pluginState.serverCSSManifest
function collectModule(entryName: string, mod: any) {
const resource = mod.resource
const modId = resource
if (modId) {
if (isCSSMod(mod)) {
cssImportsForChunk[entryName].add(modId)
}
}
}
compilation.chunkGroups.forEach((chunkGroup: any) => {
chunkGroup.chunks.forEach((chunk: webpack.Chunk) => {
// Here we only track page chunks.
if (!chunk.name) return
if (!chunk.name.endsWith('/page')) return
const entryName = path.join(this.appDir, '..', chunk.name)
if (!cssImportsForChunk[entryName]) {
cssImportsForChunk[entryName] = new Set()
}
const chunkModules = compilation.chunkGraph.getChunkModulesIterable(
chunk
) as Iterable<webpack.NormalModule>
for (const mod of chunkModules) {
collectModule(entryName, mod)
const anyModule = mod as any
if (anyModule.modules) {
anyModule.modules.forEach((concatenatedMod: any) => {
collectModule(entryName, concatenatedMod)
})
}
}
const entryCSSInfo: Record<string, string[]> =
cssManifest.cssModules || {}
entryCSSInfo[entryName] = [...cssImportsForChunk[entryName]]
cssManifest.cssModules = entryCSSInfo
})
})
forEachEntryModule(compilation, ({ name, entryModule }) => {
const clientEntryDependencyMap = collectClientEntryDependencyMap(name)
const tracked = new Set<string>()
for (const connection of compilation.moduleGraph.getOutgoingConnections(
entryModule
)) {
const entryDependency = connection.dependency
const entryRequest = connection.dependency.request
// It is possible that the same entry is added multiple times in the
// connection graph. We can just skip these to speed up the process.
if (tracked.has(entryRequest)) continue
tracked.add(entryRequest)
const { cssImports } = this.collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency: entryDependency,
clientEntryDependencyMap,
})
if (!cssManifest.cssImports) cssManifest.cssImports = {}
Object.assign(cssManifest.cssImports, cssImports)
}
})
})
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
},
(assets: webpack.Compilation['assets']) => {
const data: ClientCSSReferenceManifest = {
cssImports: {
...pluginState.serverCSSManifest.cssImports,
...pluginState.edgeServerCSSManifest.cssImports,
},
cssModules: {
...pluginState.serverCSSManifest.cssModules,
...pluginState.edgeServerCSSManifest.cssModules,
},
}
const manifest = JSON.stringify(data, null, this.dev ? 2 : undefined)
assets[`${this.assetPrefix}${FLIGHT_SERVER_CSS_MANIFEST}.json`] =
new sources.RawSource(
manifest
) as unknown as webpack.sources.RawSource
assets[`${this.assetPrefix}${FLIGHT_SERVER_CSS_MANIFEST}.js`] =
new sources.RawSource(
'self.__RSC_CSS_MANIFEST=' + manifest
) as unknown as webpack.sources.RawSource
}
)
// Invalidate in development to trigger recompilation
const invalidator = getInvalidator(compiler.outputPath)
// Check if any of the entry injections need an invalidation
if (
invalidator &&
addClientEntryAndSSRModulesList.some(
([shouldInvalidate]) => shouldInvalidate === true
)
) {
invalidator.invalidate([COMPILER_NAMES.client])
}
// Client compiler is invalidated before awaiting the compilation of the SSR client component entries
// so that the client compiler is running in parallel to the server compiler.
await Promise.all(
addClientEntryAndSSRModulesList.map(
(addClientEntryAndSSRModules) => addClientEntryAndSSRModules[1]
)
)
// Wait for action entries to be added.
await Promise.all(addActionEntryList)
}
collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency,
clientEntryDependencyMap,
}: {
entryRequest: string
compilation: any
dependency: any /* Dependency */
clientEntryDependencyMap?: Record<string, any>
}): {
clientImports: ClientComponentImports
cssImports: CssImports
actionImports: [string, string[]][]
clientActionImports: [string, string[]][]
} {
/**
* Keep track of checked modules to avoid infinite loops with recursive imports.
*/
const visitedBySegment: { [segment: string]: Set<string> } = {}
const clientComponentImports: ClientComponentImports = []
const actionImports: [string, string[]][] = []
const clientActionImports: [string, string[]][] = []
const CSSImports = new Set<string>()
const filterClientComponents = (
dependencyToFilter: any,
inClientComponentBoundary: boolean
): void => {
const mod: webpack.NormalModule =
compilation.moduleGraph.getResolvedModule(dependencyToFilter)
if (!mod) return
const isCSS = isCSSMod(mod)
// We have to always use the resolved request here to make sure the
// server and client are using the same module path (required by RSC), as
// the server compiler and client compiler have different resolve configs.
const modRequest: string | undefined =
mod.resourceResolveData?.path + mod.resourceResolveData?.query
// Ensure module is not walked again if it's already been visited
if (!visitedBySegment[entryRequest]) {
visitedBySegment[entryRequest] = new Set()
}
const storeKey =
(inClientComponentBoundary ? '0' : '1') + ':' + modRequest
if (!modRequest || visitedBySegment[entryRequest].has(storeKey)) {
return
}
visitedBySegment[entryRequest].add(storeKey)
const isClientComponent = isClientComponentEntryModule(mod)
const actions = getActions(mod)
if (actions) {
if (isClientComponent) {
clientActionImports.push([modRequest, actions])
} else {
actionImports.push([modRequest, actions])
}
}
if (isCSS) {
const sideEffectFree =
mod.factoryMeta && (mod.factoryMeta as any).sideEffectFree
if (sideEffectFree) {
const unused = !compilation.moduleGraph
.getExportsInfo(mod)
.isModuleUsed(
this.isEdgeServer ? EDGE_RUNTIME_WEBPACK : 'webpack-runtime'
)
if (unused) {
return
}
}
CSSImports.add(modRequest)
}
// Check if request is for css file.
if ((!inClientComponentBoundary && isClientComponent) || isCSS) {
clientComponentImports.push(modRequest)
// Here we are entering a client boundary, and we need to collect dependencies
// in the client graph too.
if (isClientComponent && clientEntryDependencyMap) {
if (clientEntryDependencyMap[modRequest]) {
filterClientComponents(clientEntryDependencyMap[modRequest], true)
}
}
return
}
compilation.moduleGraph
.getOutgoingConnections(mod)
.forEach((connection: any) => {
filterClientComponents(
connection.dependency,
inClientComponentBoundary || isClientComponent
)
})
}
// Don't traverse the module graph for the action loader.
if (!/next-flight-action-entry-loader/.test(entryRequest)) {
// Traverse the module graph to find all client components.
filterClientComponents(dependency, false)
}
return {
clientImports: clientComponentImports,
cssImports: CSSImports.size
? {
[entryRequest]: Array.from(CSSImports),
}
: {},
actionImports,
clientActionImports,
}
}
injectClientEntryAndSSRModules({
compiler,
compilation,
entryName,
clientImports,
bundlePath,
absolutePagePath,
}: {
compiler: webpack.Compiler
compilation: webpack.Compilation
entryName: string
clientImports: ClientComponentImports
bundlePath: string
absolutePagePath?: string
}): [shouldInvalidate: boolean, addEntryPromise: Promise<void>] {
let shouldInvalidate = false
const loaderOptions: NextFlightClientEntryLoaderOptions = {
modules: clientImports,
server: false,
}
// For the client entry, we always use the CJS build of Next.js. If the
// server is using the ESM build (when using the Edge runtime), we need to
// replace them.
const clientLoader = `next-flight-client-entry-loader?${stringify({
modules: this.isEdgeServer
? clientImports.map((importPath) =>
importPath.replace(
/[\\/]next[\\/]dist[\\/]esm[\\/]/,
'/next/dist/'.replace(/\//g, path.sep)
)
)
: clientImports,
server: false,
})}!`
const clientSSRLoader = `next-flight-client-entry-loader?${stringify({
...loaderOptions,
server: true,
})}!`
// Add for the client compilation
// Inject the entry to the client compiler.
if (this.dev) {
const entries = getEntries(compiler.outputPath)
const pageKey = getEntryKey(COMPILER_NAMES.client, 'app', bundlePath)
if (!entries[pageKey]) {
entries[pageKey] = {
type: EntryTypes.CHILD_ENTRY,
parentEntries: new Set([entryName]),
absoluteEntryFilePath: absolutePagePath,
bundlePath,
request: clientLoader,
dispose: false,
lastActiveTime: Date.now(),
}
shouldInvalidate = true
} else {
const entryData = entries[pageKey]
// New version of the client loader
if (entryData.request !== clientLoader) {
entryData.request = clientLoader
shouldInvalidate = true
}
if (entryData.type === EntryTypes.CHILD_ENTRY) {
entryData.parentEntries.add(entryName)
}
entryData.dispose = false
entryData.lastActiveTime = Date.now()
}
} else {
pluginState.injectedClientEntries[bundlePath] = clientLoader
}
// Inject the entry to the server compiler (__sc_client__).
const clientComponentEntryDep = webpack.EntryPlugin.createDependency(
clientSSRLoader,
{
name: bundlePath,
}
)
return [
shouldInvalidate,
// Add the dependency to the server compiler.
// This promise is awaited later using `Promise.all` in order to parallelize adding the entries.
// It ensures we can parallelize the SSR and Client compiler entries.
this.addEntry(
compilation,
// Reuse compilation context.
compiler.context,
clientComponentEntryDep,
{
// By using the same entry name
name: entryName,
// Layer should be client for the SSR modules
// This ensures the client components are bundled on client layer
layer: WEBPACK_LAYERS.client,
}
),
]
}
injectActionEntry({
compiler,
compilation,
actions,
entryName,
bundlePath,
fromClient,
}: {
compiler: webpack.Compiler
compilation: webpack.Compilation
actions: Map<string, string[]>
entryName: string
bundlePath: string
fromClient?: boolean
}) {
const actionsArray = Array.from(actions.entries())
const actionLoader = `next-flight-action-entry-loader?${stringify({
actions: JSON.stringify(actionsArray),
__client_imported__: fromClient,
})}!`
const currentCompilerServerActions = this.isEdgeServer
? pluginState.edgeServerActions
: pluginState.serverActions
for (const [p, names] of actionsArray) {
for (const name of names) {
const id = generateActionId(p, name)
if (typeof currentCompilerServerActions[id] === 'undefined') {
currentCompilerServerActions[id] = {
workers: {},
layer: {},
}
}
currentCompilerServerActions[id].workers[bundlePath] = ''
currentCompilerServerActions[id].layer[bundlePath] = fromClient
? WEBPACK_LAYERS.action
: WEBPACK_LAYERS.server
}
}
// Inject the entry to the server compiler
const actionEntryDep = webpack.EntryPlugin.createDependency(actionLoader, {
name: bundlePath,
})
return this.addEntry(
compilation,
// Reuse compilation context.
compiler.context,
actionEntryDep,
{
name: entryName,
layer: fromClient ? WEBPACK_LAYERS.action : WEBPACK_LAYERS.server,
}
)
}
addEntry(
compilation: any,
context: string,
dependency: webpack.Dependency,
options: webpack.EntryOptions
): Promise<any> /* Promise<module> */ {
return new Promise((resolve, reject) => {
const entry = compilation.entries.get(options.name)
entry.includeDependencies.push(dependency)
compilation.hooks.addEntry.call(entry, options)
compilation.addModuleTree(
{
context,
dependency,
contextInfo: { issuerLayer: options.layer },
},
(err: Error | undefined, module: any) => {
if (err) {
compilation.hooks.failedEntry.call(dependency, options, err)
return reject(err)
}
compilation.hooks.succeedEntry.call(dependency, options, module)
return resolve(module)
}
)
})
}
createActionAssets(
compilation: webpack.Compilation,
assets: webpack.Compilation['assets']
) {
traverseModules(compilation, (mod, _chunk, chunkGroup, modId) => {
// Go through all action entries and record the module ID for each entry.
if (
chunkGroup.name &&
mod.request &&
/next-flight-action-entry-loader/.test(mod.request)
) {
const fromClient = /&__client_imported__=true/.test(mod.request)
const mapping = this.isEdgeServer
? pluginState.actionModEdgeServerId
: pluginState.actionModServerId
if (!mapping[chunkGroup.name]) {
mapping[chunkGroup.name] = {}
}
mapping[chunkGroup.name][fromClient ? 'client' : 'server'] = modId
}
})
const serverActions: ActionManifest['node'] = {}
for (let id in pluginState.serverActions) {
const action = pluginState.serverActions[id]
for (let name in action.workers) {
const modId =
pluginState.actionModServerId[name][
action.layer[name] === WEBPACK_LAYERS.action ? 'client' : 'server'
]
action.workers[name] = modId!
}
serverActions[id] = action
}
const edgeServerActions: ActionManifest['edge'] = {}
for (let id in pluginState.edgeServerActions) {
const action = pluginState.edgeServerActions[id]
for (let name in action.workers) {
const modId =
pluginState.actionModEdgeServerId[name][
action.layer[name] === WEBPACK_LAYERS.action ? 'client' : 'server'
]
action.workers[name] = modId!
}
edgeServerActions[id] = action
}
const json = JSON.stringify(
{
node: serverActions,
edge: edgeServerActions,
},
null,
this.dev ? 2 : undefined
)
assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.js`] =
new sources.RawSource(
'self.__RSC_SERVER_MANIFEST=' + json
) as unknown as webpack.sources.RawSource
assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.json`] =
new sources.RawSource(json) as unknown as webpack.sources.RawSource
}
}