66
77const identifierUtils = require ( "./util/identifier" ) ;
88
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+
933class RecordIdsPlugin {
34+ /**
35+ * @param {Object } options Options object
36+ * @param {boolean= } options.portableIds true, when ids need to be portable
37+ */
1038 constructor ( options ) {
1139 this . options = options || { } ;
1240 }
1341
42+ /**
43+ * @param {Compiler } compiler the Compiler
44+ * @returns {void }
45+ */
1446 apply ( compiler ) {
1547 const portableIds = this . options . portableIds ;
1648 compiler . hooks . compilation . tap ( "RecordIdsPlugin" , compilation => {
1749 compilation . hooks . recordModules . tap (
1850 "RecordIdsPlugin" ,
51+ /**
52+ * @param {Module[] } modules the modules array
53+ * @param {Records } records the records object
54+ * @returns {void }
55+ */
1956 ( modules , records ) => {
2057 if ( ! records . modules ) records . modules = { } ;
2158 if ( ! records . modules . byIdentifier ) records . modules . byIdentifier = { } ;
@@ -36,9 +73,15 @@ class RecordIdsPlugin {
3673 ) ;
3774 compilation . hooks . reviveModules . tap (
3875 "RecordIdsPlugin" ,
76+ /**
77+ * @param {Module[] } modules the modules array
78+ * @param {Records } records the records object
79+ * @returns {void }
80+ */
3981 ( modules , records ) => {
4082 if ( ! records . modules ) return ;
4183 if ( records . modules . byIdentifier ) {
84+ /** @type {Set<number> } */
4285 const usedIds = new Set ( ) ;
4386 for ( const module of modules ) {
4487 if ( module . id !== null ) continue ;
@@ -62,6 +105,10 @@ class RecordIdsPlugin {
62105 }
63106 ) ;
64107
108+ /**
109+ * @param {Module } module the module
110+ * @returns {string } the (portable) identifier
111+ */
65112 const getModuleIdentifier = module => {
66113 if ( portableIds ) {
67114 return identifierUtils . makePathsRelative (
@@ -73,7 +120,12 @@ class RecordIdsPlugin {
73120 return module . identifier ( ) ;
74121 } ;
75122
123+ /**
124+ * @param {Chunk } chunk the chunk
125+ * @returns {string[] } sources of the chunk
126+ */
76127 const getChunkSources = chunk => {
128+ /** @type {string[] } */
77129 const sources = [ ] ;
78130 for ( const chunkGroup of chunk . groupsIterable ) {
79131 const index = chunkGroup . chunks . indexOf ( chunk ) ;
@@ -108,10 +160,16 @@ class RecordIdsPlugin {
108160
109161 compilation . hooks . recordChunks . tap (
110162 "RecordIdsPlugin" ,
163+ /**
164+ * @param {Chunk[] } chunks the chunks array
165+ * @param {Records } records the records object
166+ * @returns {void }
167+ */
111168 ( chunks , records ) => {
112169 if ( ! records . chunks ) records . chunks = { } ;
113170 if ( ! records . chunks . byName ) records . chunks . byName = { } ;
114171 if ( ! records . chunks . bySource ) records . chunks . bySource = { } ;
172+ /** @type {Set<number> } */
115173 const usedIds = new Set ( ) ;
116174 for ( const chunk of chunks ) {
117175 if ( typeof chunk . id !== "number" ) continue ;
@@ -128,8 +186,14 @@ class RecordIdsPlugin {
128186 ) ;
129187 compilation . hooks . reviveChunks . tap (
130188 "RecordIdsPlugin" ,
189+ /**
190+ * @param {Chunk[] } chunks the chunks array
191+ * @param {Records } records the records object
192+ * @returns {void }
193+ */
131194 ( chunks , records ) => {
132195 if ( ! records . chunks ) return ;
196+ /** @type {Set<number> } */
133197 const usedIds = new Set ( ) ;
134198 if ( records . chunks . byName ) {
135199 for ( const chunk of chunks ) {
@@ -148,8 +212,8 @@ class RecordIdsPlugin {
148212 for ( const source of sources ) {
149213 const id = records . chunks . bySource [ source ] ;
150214 if ( id === undefined ) continue ;
151- if ( usedIds [ id ] ) continue ;
152- usedIds [ id ] = true ;
215+ if ( usedIds . has ( id ) ) continue ;
216+ usedIds . add ( id ) ;
153217 chunk . id = id ;
154218 break ;
155219 }
0 commit comments