22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Tobias Koppers @sokra
44*/
5+
56"use strict" ;
67
78const SortableSet = require ( "./util/SortableSet" ) ;
89const compareLocations = require ( "./compareLocations" ) ;
910
11+ /** @typedef {import("./Chunk") } Chunk */
12+ /** @typedef {import("./Module") } Module */
13+ /** @typedef {import("./ModuleReason") } ModuleReason */
14+
15+ /** @typedef {{id: number} } HasId */
16+ /** @typedef {{module: Module, loc: any, request: string} } OriginRecord */
17+ /** @typedef {string|{name: string} } ChunkGroupOptions */
18+
1019let debugId = 5000 ;
1120
21+ /**
22+ * @template {T}
23+ * @param {Set<T> } set set to convert to array.
24+ * @returns {T[] } the array format of existing set
25+ */
1226const getArray = set => Array . from ( set ) ;
1327
28+ /**
29+ * A convenience method used to sort chunks based on their id's
30+ * @param {HasId } a first sorting comparitor
31+ * @param {HasId } b second sorting comparitor
32+ * @returns {1|0|-1 } a sorting index to determine order
33+ */
1434const sortById = ( a , b ) => {
1535 if ( a . id < b . id ) return - 1 ;
1636 if ( b . id < a . id ) return 1 ;
1737 return 0 ;
1838} ;
1939
40+ /**
41+ * @param {OriginRecord } a the first comparitor in sort
42+ * @param {OriginRecord } b the second comparitor in sort
43+ * @returns {1|-1|0 } returns sorting order as index
44+ */
2045const sortOrigin = ( a , b ) => {
2146 const aIdent = a . module ? a . module . identifier ( ) : "" ;
2247 const bIdent = b . module ? b . module . identifier ( ) : "" ;
@@ -26,21 +51,33 @@ const sortOrigin = (a, b) => {
2651} ;
2752
2853class ChunkGroup {
54+ /**
55+ * Creates an instance of ChunkGroup.
56+ * @param {ChunkGroupOptions= } options chunk group options passed to chunkGroup
57+ */
2958 constructor ( options ) {
3059 if ( typeof options === "string" ) {
3160 options = { name : options } ;
3261 } else if ( ! options ) {
3362 options = { name : undefined } ;
3463 }
64+ /** @type {number } */
3565 this . groupDebugId = debugId ++ ;
3666 this . options = options ;
3767 this . _children = new SortableSet ( undefined , sortById ) ;
3868 this . _parents = new SortableSet ( undefined , sortById ) ;
3969 this . _blocks = new SortableSet ( ) ;
70+ /** @type {Chunk[] } */
4071 this . chunks = [ ] ;
72+ /** @type {OriginRecord[] } */
4173 this . origins = [ ] ;
4274 }
4375
76+ /**
77+ * when a new chunk is added to a chunkGroup, addingOptions will occur.
78+ * @param {ChunkGroupOptions } options the chunkGroup options passed to addOptions
79+ * @returns {void }
80+ */
4481 addOptions ( options ) {
4582 for ( const key of Object . keys ( options ) ) {
4683 if ( this . options [ key ] === undefined ) {
@@ -57,23 +94,46 @@ class ChunkGroup {
5794 }
5895 }
5996
97+ /**
98+ * returns the name of current ChunkGroup
99+ * @returns {string } returns the ChunkGroup name
100+ */
60101 get name ( ) {
61102 return this . options . name ;
62103 }
63104
105+ /**
106+ * sets a new name for current ChunkGroup
107+ * @param {string } value the new name for ChunkGroup
108+ * @returns {void }
109+ */
64110 set name ( value ) {
65111 this . options . name = value ;
66112 }
67113
68- /* istanbul ignore next */
114+ /**
115+ * get a uniqueId for ChunkGroup, made up of its member Chunk debugId's
116+ * @readonly
117+ * @returns {string } a unique concatenation of chunk debugId's
118+ */
69119 get debugId ( ) {
70120 return Array . from ( this . chunks , x => x . debugId ) . join ( "+" ) ;
71121 }
72122
123+ /**
124+ * get a unique id for ChunkGroup, made up of its member Chunk id's
125+ * @readonly
126+ * @returns {string } a unique concatenation of chunk ids
127+ */
73128 get id ( ) {
74129 return Array . from ( this . chunks , x => x . id ) . join ( "+" ) ;
75130 }
76131
132+ /**
133+ * Performs an unshift of a specific chunk
134+ * @param {Chunk } chunk chunk being unshifted
135+ * @returns {boolean } returns true if attempted chunk shift is accepted
136+ */
77137 unshiftChunk ( chunk ) {
78138 const oldIdx = this . chunks . indexOf ( chunk ) ;
79139 if ( oldIdx > 0 ) {
@@ -86,6 +146,12 @@ class ChunkGroup {
86146 return false ;
87147 }
88148
149+ /**
150+ * inserts a chunk before another existing chunk in group
151+ * @param {Chunk } chunk Chunk being inserted
152+ * @param {Chunk } before Placeholder/target chunk marking new chunk insertion point
153+ * @returns {boolean } return true if insertion was successful
154+ */
89155 insertChunk ( chunk , before ) {
90156 const oldIdx = this . chunks . indexOf ( chunk ) ;
91157 const idx = this . chunks . indexOf ( before ) ;
@@ -102,6 +168,11 @@ class ChunkGroup {
102168 return false ;
103169 }
104170
171+ /**
172+ * add a chunk into ChunkGroup. Is pushed on or prepended
173+ * @param {Chunk } chunk chunk being pushed into ChunkGroupS
174+ * @returns {boolean } returns true if chunk addition was ssuccesful.
175+ */
105176 pushChunk ( chunk ) {
106177 const oldIdx = this . chunks . indexOf ( chunk ) ;
107178 if ( oldIdx >= 0 ) {
@@ -111,6 +182,12 @@ class ChunkGroup {
111182 return true ;
112183 }
113184
185+ /**
186+ * @description
187+ * @param {Chunk } oldChunk chunk to be replaced
188+ * @param {Chunk } newChunk New chunkt that will be replaced
189+ * @returns {boolean } rerturns true for
190+ */
114191 replaceChunk ( oldChunk , newChunk ) {
115192 const oldIdx = this . chunks . indexOf ( oldChunk ) ;
116193 if ( oldIdx < 0 ) return false ;
@@ -210,7 +287,7 @@ class ChunkGroup {
210287 }
211288
212289 /**
213- * @return {Array } - an array containing the blocks
290+ * @returns {Array } - an array containing the blocks
214291 */
215292 getBlocks ( ) {
216293 return this . _blocks . getFromCache ( getArray ) ;
@@ -263,6 +340,10 @@ class ChunkGroup {
263340 return Array . from ( files ) ;
264341 }
265342
343+ /**
344+ * @param {ModuleReason } reason reason for removing ChunkGroup
345+ * @returns {void }
346+ */
266347 remove ( reason ) {
267348 // cleanup parents
268349 for ( const parentChunkGroup of this . _parents ) {
@@ -310,6 +391,13 @@ class ChunkGroup {
310391 this . _children . sort ( ) ;
311392 }
312393
394+ /**
395+ * Sorting predicate which allows current ChunkGroup to be compared against another.
396+ * Sorting values are based off of number of chunks in ChunkGroup.
397+ *
398+ * @param {ChunkGroup } otherGroup the chunkGroup to compare this against
399+ * @returns {-1|0|1 } sort position for comparison
400+ */
313401 compareTo ( otherGroup ) {
314402 if ( this . chunks . length > otherGroup . chunks . length ) return - 1 ;
315403 if ( this . chunks . length < otherGroup . chunks . length ) return 1 ;
0 commit comments