Skip to content

Commit 4d00edc

Browse files
authored
Merge pull request webpack#7178 from webpack/feature/types-chunkgroup
Feature/types chunkgroup
2 parents ce184a5 + 54f5a5a commit 4d00edc

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

lib/ChunkGroup.js

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@
77
const SortableSet = require("./util/SortableSet");
88
const compareLocations = require("./compareLocations");
99

10+
/** @typedef {import("./Chunk")} Chunk */
11+
/** @typedef {import("./Module")} Module */
12+
/** @typedef {import("./ModuleReason")} ModuleReason */
13+
14+
/** @typedef {{id: number}} HasId */
15+
/** @typedef {{module: Module, loc: any, request: string}} OriginRecord */
16+
/** @typedef {string|{name: string}} ChunkGroupOptions */
17+
1018
let debugId = 5000;
1119

20+
/**
21+
* @template {T}
22+
* @param {Set<T>} set set to convert to array.
23+
* @returns {T[]} the array format of existing set
24+
*/
1225
const getArray = set => Array.from(set);
1326

27+
/**
28+
* A convenience method used to sort chunks based on their id's
29+
* @param {HasId} a first sorting comparitor
30+
* @param {HasId} b second sorting comparitor
31+
* @returns {1|0|-1} a sorting index to determine order
32+
*/
1433
const sortById = (a, b) => {
1534
if (a.id < b.id) return -1;
1635
if (b.id < a.id) return 1;
1736
return 0;
1837
};
1938

39+
/**
40+
* @param {OriginRecord} a the first comparitor in sort
41+
* @param {OriginRecord} b the second comparitor in sort
42+
* @returns {1|-1|0} returns sorting order as index
43+
*/
2044
const sortOrigin = (a, b) => {
2145
const aIdent = a.module ? a.module.identifier() : "";
2246
const bIdent = b.module ? b.module.identifier() : "";
@@ -26,21 +50,33 @@ const sortOrigin = (a, b) => {
2650
};
2751

2852
class ChunkGroup {
53+
/**
54+
* Creates an instance of ChunkGroup.
55+
* @param {ChunkGroupOptions=} options chunk group options passed to chunkGroup
56+
*/
2957
constructor(options) {
3058
if (typeof options === "string") {
3159
options = { name: options };
3260
} else if (!options) {
3361
options = { name: undefined };
3462
}
63+
/** @type {number} */
3564
this.groupDebugId = debugId++;
3665
this.options = options;
3766
this._children = new SortableSet(undefined, sortById);
3867
this._parents = new SortableSet(undefined, sortById);
3968
this._blocks = new SortableSet();
69+
/** @type {Chunk[]} */
4070
this.chunks = [];
71+
/** @type {OriginRecord[]} */
4172
this.origins = [];
4273
}
4374

75+
/**
76+
* when a new chunk is added to a chunkGroup, addingOptions will occur.
77+
* @param {ChunkGroupOptions} options the chunkGroup options passed to addOptions
78+
* @returns {void}
79+
*/
4480
addOptions(options) {
4581
for (const key of Object.keys(options)) {
4682
if (this.options[key] === undefined) {
@@ -57,23 +93,46 @@ class ChunkGroup {
5793
}
5894
}
5995

96+
/**
97+
* returns the name of current ChunkGroup
98+
* @returns {string|undefined} returns the ChunkGroup name
99+
*/
60100
get name() {
61101
return this.options.name;
62102
}
63103

104+
/**
105+
* sets a new name for current ChunkGroup
106+
* @param {string} value the new name for ChunkGroup
107+
* @returns {void}
108+
*/
64109
set name(value) {
65110
this.options.name = value;
66111
}
67112

68-
/* istanbul ignore next */
113+
/**
114+
* get a uniqueId for ChunkGroup, made up of its member Chunk debugId's
115+
* @readonly
116+
* @returns {string} a unique concatenation of chunk debugId's
117+
*/
69118
get debugId() {
70119
return Array.from(this.chunks, x => x.debugId).join("+");
71120
}
72121

122+
/**
123+
* get a unique id for ChunkGroup, made up of its member Chunk id's
124+
* @readonly
125+
* @returns {string} a unique concatenation of chunk ids
126+
*/
73127
get id() {
74128
return Array.from(this.chunks, x => x.id).join("+");
75129
}
76130

131+
/**
132+
* Performs an unshift of a specific chunk
133+
* @param {Chunk} chunk chunk being unshifted
134+
* @returns {boolean} returns true if attempted chunk shift is accepted
135+
*/
77136
unshiftChunk(chunk) {
78137
const oldIdx = this.chunks.indexOf(chunk);
79138
if (oldIdx > 0) {
@@ -86,6 +145,12 @@ class ChunkGroup {
86145
return false;
87146
}
88147

148+
/**
149+
* inserts a chunk before another existing chunk in group
150+
* @param {Chunk} chunk Chunk being inserted
151+
* @param {Chunk} before Placeholder/target chunk marking new chunk insertion point
152+
* @returns {boolean} return true if insertion was successful
153+
*/
89154
insertChunk(chunk, before) {
90155
const oldIdx = this.chunks.indexOf(chunk);
91156
const idx = this.chunks.indexOf(before);
@@ -102,6 +167,11 @@ class ChunkGroup {
102167
return false;
103168
}
104169

170+
/**
171+
* add a chunk into ChunkGroup. Is pushed on or prepended
172+
* @param {Chunk} chunk chunk being pushed into ChunkGroupS
173+
* @returns {boolean} returns true if chunk addition was ssuccesful.
174+
*/
105175
pushChunk(chunk) {
106176
const oldIdx = this.chunks.indexOf(chunk);
107177
if (oldIdx >= 0) {
@@ -111,6 +181,12 @@ class ChunkGroup {
111181
return true;
112182
}
113183

184+
/**
185+
* @description
186+
* @param {Chunk} oldChunk chunk to be replaced
187+
* @param {Chunk} newChunk New chunkt that will be replaced
188+
* @returns {boolean} rerturns true for
189+
*/
114190
replaceChunk(oldChunk, newChunk) {
115191
const oldIdx = this.chunks.indexOf(oldChunk);
116192
if (oldIdx < 0) return false;
@@ -210,7 +286,7 @@ class ChunkGroup {
210286
}
211287

212288
/**
213-
* @return {Array} - an array containing the blocks
289+
* @returns {Array} - an array containing the blocks
214290
*/
215291
getBlocks() {
216292
return this._blocks.getFromCache(getArray);
@@ -263,6 +339,10 @@ class ChunkGroup {
263339
return Array.from(files);
264340
}
265341

342+
/**
343+
* @param {ModuleReason} reason reason for removing ChunkGroup
344+
* @returns {void}
345+
*/
266346
remove(reason) {
267347
// cleanup parents
268348
for (const parentChunkGroup of this._parents) {
@@ -310,6 +390,13 @@ class ChunkGroup {
310390
this._children.sort();
311391
}
312392

393+
/**
394+
* Sorting predicate which allows current ChunkGroup to be compared against another.
395+
* Sorting values are based off of number of chunks in ChunkGroup.
396+
*
397+
* @param {ChunkGroup} otherGroup the chunkGroup to compare this against
398+
* @returns {-1|0|1} sort position for comparison
399+
*/
313400
compareTo(otherGroup) {
314401
if (this.chunks.length > otherGroup.chunks.length) return -1;
315402
if (this.chunks.length < otherGroup.chunks.length) return 1;

0 commit comments

Comments
 (0)