1
- /**
2
- * The plugin is developing, ignore it now
3
- */
4
1
import type Webpack from 'webpack' ;
5
2
import type { Compilation , ModuleGraph , NormalModule } from 'webpack' ;
6
3
import {
@@ -25,13 +22,12 @@ export interface ModuleExportsInfo {
25
22
readonly exportName : string ;
26
23
}
27
24
28
- const resourcePath2Entries = new Map <
29
- string ,
30
- {
31
- entryName : string ;
32
- entryPath : string ;
33
- } [ ]
34
- > ( ) ;
25
+ export interface EntryInfo {
26
+ readonly entryName : string ;
27
+ readonly entryPath : string ;
28
+ }
29
+
30
+ const resourcePath2Entries = new Map < string , EntryInfo [ ] > ( ) ;
35
31
export class RscServerPlugin {
36
32
private clientReferencesMap : ClientReferencesMap = new Map ( ) ;
37
33
private serverReferencesMap : ServerReferencesMap = new Map ( ) ;
@@ -41,29 +37,47 @@ export class RscServerPlugin {
41
37
private styles : Set < string > ;
42
38
constructor ( options : RscServerPluginOptions ) {
43
39
this . styles = new Set ( ) ;
44
-
45
40
this . serverManifestFilename =
46
41
options ?. serverManifestFilename || `react-server-manifest.json` ;
47
-
48
42
this . entryPath2Name = options ?. entryPath2Name || new Map ( ) ;
49
43
}
50
44
45
+ private isValidModule ( module : NormalModule ) : boolean {
46
+ return Boolean ( module ?. resource ) ;
47
+ }
48
+
49
+ private hasValidEntries (
50
+ entries : EntryInfo [ ] | undefined ,
51
+ ) : entries is EntryInfo [ ] {
52
+ return Boolean ( entries && entries . length > 0 ) ;
53
+ }
54
+
55
+ private getEntryNameFromIssuer ( issuer : NormalModule ) : string | undefined {
56
+ return issuer . resource
57
+ ? this . entryPath2Name . get ( issuer . resource )
58
+ : undefined ;
59
+ }
60
+
61
+ private createEntryFromIssuer (
62
+ issuer : NormalModule ,
63
+ entryName : string ,
64
+ ) : EntryInfo {
65
+ return { entryName, entryPath : issuer . resource } ;
66
+ }
67
+
51
68
private findModuleEntries (
52
69
module : NormalModule ,
53
70
compilation : Compilation ,
54
- resourcePath2Entries : Map <
55
- string ,
56
- Array < { entryName : string ; entryPath : string } >
57
- > ,
71
+ resourcePath2Entries : Map < string , EntryInfo [ ] > ,
58
72
visited = new Set < string > ( ) ,
59
- ) : Array < { entryName : string ; entryPath : string } > {
60
- if ( ! module ?. resource || visited . has ( module . resource ) ) {
73
+ ) : EntryInfo [ ] {
74
+ if ( ! this . isValidModule ( module ) || visited . has ( module . resource ) ) {
61
75
return [ ] ;
62
76
}
63
77
visited . add ( module . resource ) ;
64
78
65
79
const currentEntries = resourcePath2Entries . get ( module . resource ) ;
66
- if ( currentEntries && currentEntries ?. length > 0 ) {
80
+ if ( this . hasValidEntries ( currentEntries ) ) {
67
81
return currentEntries ;
68
82
}
69
83
@@ -78,15 +92,13 @@ export class RscServerPlugin {
78
92
resourcePath2Entries ,
79
93
visited ,
80
94
) ;
81
- if ( issuerEntries . length > 0 ) {
95
+ if ( this . hasValidEntries ( issuerEntries ) ) {
82
96
return issuerEntries ;
83
97
}
84
98
85
- if ( issuer . resource ) {
86
- const entryName = this . entryPath2Name . get ( issuer . resource ) ;
87
- if ( entryName ) {
88
- return [ { entryName, entryPath : issuer . resource } ] ;
89
- }
99
+ const entryName = this . getEntryNameFromIssuer ( issuer ) ;
100
+ if ( entryName ) {
101
+ return [ this . createEntryFromIssuer ( issuer , entryName ) ] ;
90
102
}
91
103
92
104
return [ ] ;
@@ -96,38 +108,9 @@ export class RscServerPlugin {
96
108
const {
97
109
EntryPlugin,
98
110
WebpackError,
99
- // dependencies: { NullDependency },
100
- // util: {
101
- // runtime: { getEntryRuntime },
102
- // },
103
111
sources : { RawSource } ,
104
- // RuntimeGlobals,
105
112
} = compiler . webpack ;
106
113
107
- // class ServerReferenceDependency extends NullDependency {
108
- // override get type(): string {
109
- // return `server-reference`;
110
- // }
111
- // }
112
-
113
- // ServerReferenceDependency.Template = class ServerReferenceDependencyTemplate extends (
114
- // NullDependency.Template
115
- // ) {
116
- // override apply(
117
- // _dependency: ServerReferenceDependency,
118
- // _source: Webpack.sources.ReplaceSource,
119
- // { runtimeRequirements }: { runtimeRequirements: Set<string> },
120
- // ) {
121
- // runtimeRequirements.add(RuntimeGlobals.moduleId);
122
- // }
123
- // };
124
-
125
- // function hasServerReferenceDependency(module: Webpack.Module): boolean {
126
- // return module.dependencies.some(
127
- // dependency => dependency instanceof ServerReferenceDependency,
128
- // );
129
- // }
130
-
131
114
const includeModule = async (
132
115
compilation : Webpack . Compilation ,
133
116
resource : string ,
0 commit comments