6
6
7
7
const identifierUtils = require ( "./util/identifier" ) ;
8
8
9
+ /** @typedef {import("./Compiler") } Compiler */
10
+ /** @typedef {import("./Chunk") } Chunk */
11
+ /** @typedef {import("./Module") } Module */
12
+
13
+ /**
14
+ * @typedef {Object } RecordsChunks
15
+ * @property {Record<string, number>= } byName
16
+ * @property {Record<string, number>= } bySource
17
+ * @property {number[]= } usedIds
18
+ */
19
+
20
+ /**
21
+ * @typedef {Object } RecordsModules
22
+ * @property {Record<string, number>= } byIdentifier
23
+ * @property {Record<string, number>= } bySource
24
+ * @property {Record<number, number>= } usedIds
25
+ */
26
+
27
+ /**
28
+ * @typedef {Object } Records
29
+ * @property {RecordsChunks= } chunks
30
+ * @property {RecordsModules= } modules
31
+ */
32
+
9
33
class RecordIdsPlugin {
34
+ /**
35
+ * @param {Object } options Options object
36
+ * @param {boolean= } options.portableIds true, when ids need to be portable
37
+ */
10
38
constructor ( options ) {
11
39
this . options = options || { } ;
12
40
}
13
41
42
+ /**
43
+ * @param {Compiler } compiler the Compiler
44
+ * @returns {void }
45
+ */
14
46
apply ( compiler ) {
15
47
const portableIds = this . options . portableIds ;
16
48
compiler . hooks . compilation . tap ( "RecordIdsPlugin" , compilation => {
17
49
compilation . hooks . recordModules . tap (
18
50
"RecordIdsPlugin" ,
51
+ /**
52
+ * @param {Module[] } modules the modules array
53
+ * @param {Records } records the records object
54
+ * @returns {void }
55
+ */
19
56
( modules , records ) => {
20
57
if ( ! records . modules ) records . modules = { } ;
21
58
if ( ! records . modules . byIdentifier ) records . modules . byIdentifier = { } ;
@@ -36,9 +73,15 @@ class RecordIdsPlugin {
36
73
) ;
37
74
compilation . hooks . reviveModules . tap (
38
75
"RecordIdsPlugin" ,
76
+ /**
77
+ * @param {Module[] } modules the modules array
78
+ * @param {Records } records the records object
79
+ * @returns {void }
80
+ */
39
81
( modules , records ) => {
40
82
if ( ! records . modules ) return ;
41
83
if ( records . modules . byIdentifier ) {
84
+ /** @type {Set<number> } */
42
85
const usedIds = new Set ( ) ;
43
86
for ( const module of modules ) {
44
87
if ( module . id !== null ) continue ;
@@ -62,6 +105,10 @@ class RecordIdsPlugin {
62
105
}
63
106
) ;
64
107
108
+ /**
109
+ * @param {Module } module the module
110
+ * @returns {string } the (portable) identifier
111
+ */
65
112
const getModuleIdentifier = module => {
66
113
if ( portableIds ) {
67
114
return identifierUtils . makePathsRelative (
@@ -73,7 +120,12 @@ class RecordIdsPlugin {
73
120
return module . identifier ( ) ;
74
121
} ;
75
122
123
+ /**
124
+ * @param {Chunk } chunk the chunk
125
+ * @returns {string[] } sources of the chunk
126
+ */
76
127
const getChunkSources = chunk => {
128
+ /** @type {string[] } */
77
129
const sources = [ ] ;
78
130
for ( const chunkGroup of chunk . groupsIterable ) {
79
131
const index = chunkGroup . chunks . indexOf ( chunk ) ;
@@ -108,10 +160,16 @@ class RecordIdsPlugin {
108
160
109
161
compilation . hooks . recordChunks . tap (
110
162
"RecordIdsPlugin" ,
163
+ /**
164
+ * @param {Chunk[] } chunks the chunks array
165
+ * @param {Records } records the records object
166
+ * @returns {void }
167
+ */
111
168
( chunks , records ) => {
112
169
if ( ! records . chunks ) records . chunks = { } ;
113
170
if ( ! records . chunks . byName ) records . chunks . byName = { } ;
114
171
if ( ! records . chunks . bySource ) records . chunks . bySource = { } ;
172
+ /** @type {Set<number> } */
115
173
const usedIds = new Set ( ) ;
116
174
for ( const chunk of chunks ) {
117
175
if ( typeof chunk . id !== "number" ) continue ;
@@ -128,8 +186,14 @@ class RecordIdsPlugin {
128
186
) ;
129
187
compilation . hooks . reviveChunks . tap (
130
188
"RecordIdsPlugin" ,
189
+ /**
190
+ * @param {Chunk[] } chunks the chunks array
191
+ * @param {Records } records the records object
192
+ * @returns {void }
193
+ */
131
194
( chunks , records ) => {
132
195
if ( ! records . chunks ) return ;
196
+ /** @type {Set<number> } */
133
197
const usedIds = new Set ( ) ;
134
198
if ( records . chunks . byName ) {
135
199
for ( const chunk of chunks ) {
@@ -148,8 +212,8 @@ class RecordIdsPlugin {
148
212
for ( const source of sources ) {
149
213
const id = records . chunks . bySource [ source ] ;
150
214
if ( id === undefined ) continue ;
151
- if ( usedIds [ id ] ) continue ;
152
- usedIds [ id ] = true ;
215
+ if ( usedIds . has ( id ) ) continue ;
216
+ usedIds . add ( id ) ;
153
217
chunk . id = id ;
154
218
break ;
155
219
}
0 commit comments