webpack 4.8.2 → 4.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +95 -52
  2. package/bin/webpack.js +128 -43
  3. package/lib/AmdMainTemplatePlugin.js +10 -0
  4. package/lib/AsyncDependencyToInitialChunkError.js +12 -2
  5. package/lib/BannerPlugin.js +115 -101
  6. package/lib/CaseSensitiveModulesWarning.js +20 -2
  7. package/lib/Chunk.js +1 -0
  8. package/lib/ChunkGroup.js +465 -465
  9. package/lib/ChunkRenderError.js +8 -0
  10. package/lib/ChunkTemplate.js +71 -71
  11. package/lib/Compilation.js +1 -1
  12. package/lib/Compiler.js +2 -1
  13. package/lib/ContextModule.js +8 -8
  14. package/lib/DllPlugin.js +3 -1
  15. package/lib/DllReferencePlugin.js +2 -1
  16. package/lib/Entrypoint.js +54 -54
  17. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +115 -115
  18. package/lib/ExportPropertyMainTemplatePlugin.js +13 -0
  19. package/lib/Generator.js +52 -52
  20. package/lib/HotModuleReplacement.runtime.js +633 -633
  21. package/lib/JsonParser.js +2 -1
  22. package/lib/LibManifestPlugin.js +9 -0
  23. package/lib/LibraryTemplatePlugin.js +66 -33
  24. package/lib/MainTemplate.js +468 -468
  25. package/lib/Module.js +3 -3
  26. package/lib/ModuleDependencyError.js +12 -2
  27. package/lib/NormalModuleFactory.js +5 -3
  28. package/lib/Parser.js +27 -23
  29. package/lib/ProgressPlugin.js +1 -1
  30. package/lib/RecordIdsPlugin.js +3 -1
  31. package/lib/RuntimeTemplate.js +1 -1
  32. package/lib/SetVarMainTemplatePlugin.js +12 -0
  33. package/lib/SourceMapDevToolPlugin.js +11 -13
  34. package/lib/Template.js +289 -290
  35. package/lib/UmdMainTemplatePlugin.js +67 -32
  36. package/lib/WebpackError.js +8 -2
  37. package/lib/compareLocations.js +20 -0
  38. package/lib/debug/ProfilingPlugin.js +416 -416
  39. package/lib/dependencies/ContextDependencyHelpers.js +142 -142
  40. package/lib/dependencies/WebpackMissingModule.js +2 -2
  41. package/lib/optimize/RemoveEmptyChunksPlugin.js +42 -40
  42. package/lib/optimize/RuntimeChunkPlugin.js +9 -5
  43. package/lib/optimize/SplitChunksPlugin.js +195 -124
  44. package/lib/util/Queue.js +46 -46
  45. package/lib/util/SetHelpers.js +48 -48
  46. package/lib/util/SortableSet.js +106 -106
  47. package/lib/util/StackedSetMap.js +128 -128
  48. package/lib/util/cachedMerge.js +13 -0
  49. package/lib/util/identifier.js +5 -0
  50. package/lib/util/objectToMap.js +16 -16
  51. package/lib/wasm/WebAssemblyGenerator.js +280 -280
  52. package/lib/wasm/WebAssemblyParser.js +79 -79
  53. package/lib/web/JsonpMainTemplatePlugin.js +2 -2
  54. package/package.json +21 -17
  55. package/schemas/WebpackOptions.json +12 -1
  56. package/schemas/plugins/BannerPlugin.json +96 -85
  57. package/schemas/plugins/DllPlugin.json +4 -0
package/lib/ChunkGroup.js CHANGED
@@ -1,465 +1,465 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const SortableSet = require("./util/SortableSet");
8
- const compareLocations = require("./compareLocations");
9
-
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: TODO, request: string}} OriginRecord */
16
- /** @typedef {string|{name: string}} ChunkGroupOptions */
17
-
18
- let debugId = 5000;
19
-
20
- /**
21
- * @template {T}
22
- * @param {Set<T>} set set to convert to array.
23
- * @returns {T[]} the array format of existing set
24
- */
25
- const getArray = set => Array.from(set);
26
-
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
- */
33
- const sortById = (a, b) => {
34
- if (a.id < b.id) return -1;
35
- if (b.id < a.id) return 1;
36
- return 0;
37
- };
38
-
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
- */
44
- const sortOrigin = (a, b) => {
45
- const aIdent = a.module ? a.module.identifier() : "";
46
- const bIdent = b.module ? b.module.identifier() : "";
47
- if (aIdent < bIdent) return -1;
48
- if (aIdent > bIdent) return 1;
49
- return compareLocations(a.loc, b.loc);
50
- };
51
-
52
- class ChunkGroup {
53
- /**
54
- * Creates an instance of ChunkGroup.
55
- * @param {ChunkGroupOptions=} options chunk group options passed to chunkGroup
56
- */
57
- constructor(options) {
58
- if (typeof options === "string") {
59
- options = { name: options };
60
- } else if (!options) {
61
- options = { name: undefined };
62
- }
63
- /** @type {number} */
64
- this.groupDebugId = debugId++;
65
- this.options = options;
66
- this._children = new SortableSet(undefined, sortById);
67
- this._parents = new SortableSet(undefined, sortById);
68
- this._blocks = new SortableSet();
69
- /** @type {Chunk[]} */
70
- this.chunks = [];
71
- /** @type {OriginRecord[]} */
72
- this.origins = [];
73
- }
74
-
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
- */
80
- addOptions(options) {
81
- for (const key of Object.keys(options)) {
82
- if (this.options[key] === undefined) {
83
- this.options[key] = options[key];
84
- } else if (this.options[key] !== options[key]) {
85
- if (key.endsWith("Order")) {
86
- this.options[key] = Math.max(this.options[key], options[key]);
87
- } else {
88
- throw new Error(
89
- `ChunkGroup.addOptions: No option merge strategy for ${key}`
90
- );
91
- }
92
- }
93
- }
94
- }
95
-
96
- /**
97
- * returns the name of current ChunkGroup
98
- * @returns {string|undefined} returns the ChunkGroup name
99
- */
100
- get name() {
101
- return this.options.name;
102
- }
103
-
104
- /**
105
- * sets a new name for current ChunkGroup
106
- * @param {string} value the new name for ChunkGroup
107
- * @returns {void}
108
- */
109
- set name(value) {
110
- this.options.name = value;
111
- }
112
-
113
- /**
114
- * get a uniqueId for ChunkGroup, made up of its member Chunk debugId's
115
- * @returns {string} a unique concatenation of chunk debugId's
116
- */
117
- get debugId() {
118
- return Array.from(this.chunks, x => x.debugId).join("+");
119
- }
120
-
121
- /**
122
- * get a unique id for ChunkGroup, made up of its member Chunk id's
123
- * @returns {string} a unique concatenation of chunk ids
124
- */
125
- get id() {
126
- return Array.from(this.chunks, x => x.id).join("+");
127
- }
128
-
129
- /**
130
- * Performs an unshift of a specific chunk
131
- * @param {Chunk} chunk chunk being unshifted
132
- * @returns {boolean} returns true if attempted chunk shift is accepted
133
- */
134
- unshiftChunk(chunk) {
135
- const oldIdx = this.chunks.indexOf(chunk);
136
- if (oldIdx > 0) {
137
- this.chunks.splice(oldIdx, 1);
138
- this.chunks.unshift(chunk);
139
- } else if (oldIdx < 0) {
140
- this.chunks.unshift(chunk);
141
- return true;
142
- }
143
- return false;
144
- }
145
-
146
- /**
147
- * inserts a chunk before another existing chunk in group
148
- * @param {Chunk} chunk Chunk being inserted
149
- * @param {Chunk} before Placeholder/target chunk marking new chunk insertion point
150
- * @returns {boolean} return true if insertion was successful
151
- */
152
- insertChunk(chunk, before) {
153
- const oldIdx = this.chunks.indexOf(chunk);
154
- const idx = this.chunks.indexOf(before);
155
- if (idx < 0) {
156
- throw new Error("before chunk not found");
157
- }
158
- if (oldIdx >= 0 && oldIdx > idx) {
159
- this.chunks.splice(oldIdx, 1);
160
- this.chunks.splice(idx, 0, chunk);
161
- } else if (oldIdx < 0) {
162
- this.chunks.splice(idx, 0, chunk);
163
- return true;
164
- }
165
- return false;
166
- }
167
-
168
- /**
169
- * add a chunk into ChunkGroup. Is pushed on or prepended
170
- * @param {Chunk} chunk chunk being pushed into ChunkGroupS
171
- * @returns {boolean} returns true if chunk addition was ssuccesful.
172
- */
173
- pushChunk(chunk) {
174
- const oldIdx = this.chunks.indexOf(chunk);
175
- if (oldIdx >= 0) {
176
- return false;
177
- }
178
- this.chunks.push(chunk);
179
- return true;
180
- }
181
-
182
- /**
183
- * @param {Chunk} oldChunk chunk to be replaced
184
- * @param {Chunk} newChunk New chunkt that will be replaced
185
- * @returns {boolean} rerturns true for
186
- */
187
- replaceChunk(oldChunk, newChunk) {
188
- const oldIdx = this.chunks.indexOf(oldChunk);
189
- if (oldIdx < 0) return false;
190
- const newIdx = this.chunks.indexOf(newChunk);
191
- if (newIdx < 0) {
192
- this.chunks[oldIdx] = newChunk;
193
- return true;
194
- }
195
- if (newIdx < oldIdx) {
196
- this.chunks.splice(oldIdx, 1);
197
- return true;
198
- } else if (newIdx !== oldIdx) {
199
- this.chunks[oldIdx] = newChunk;
200
- this.chunks.splice(newIdx, 1);
201
- return true;
202
- }
203
- }
204
-
205
- removeChunk(chunk) {
206
- const idx = this.chunks.indexOf(chunk);
207
- if (idx >= 0) {
208
- this.chunks.splice(idx, 1);
209
- return true;
210
- }
211
- return false;
212
- }
213
-
214
- isInitial() {
215
- return false;
216
- }
217
-
218
- addChild(chunk) {
219
- if (this._children.has(chunk)) {
220
- return false;
221
- }
222
- this._children.add(chunk);
223
- return true;
224
- }
225
-
226
- getChildren() {
227
- return this._children.getFromCache(getArray);
228
- }
229
-
230
- getNumberOfChildren() {
231
- return this._children.size;
232
- }
233
-
234
- get childrenIterable() {
235
- return this._children;
236
- }
237
-
238
- removeChild(chunk) {
239
- if (!this._children.has(chunk)) {
240
- return false;
241
- }
242
-
243
- this._children.delete(chunk);
244
- chunk.removeParent(this);
245
- return true;
246
- }
247
-
248
- addParent(parentChunk) {
249
- if (!this._parents.has(parentChunk)) {
250
- this._parents.add(parentChunk);
251
- return true;
252
- }
253
- return false;
254
- }
255
-
256
- getParents() {
257
- return this._parents.getFromCache(getArray);
258
- }
259
-
260
- setParents(newParents) {
261
- this._parents.clear();
262
- for (const p of newParents) this._parents.add(p);
263
- }
264
-
265
- getNumberOfParents() {
266
- return this._parents.size;
267
- }
268
-
269
- hasParent(parent) {
270
- return this._parents.has(parent);
271
- }
272
-
273
- get parentsIterable() {
274
- return this._parents;
275
- }
276
-
277
- removeParent(chunk) {
278
- if (this._parents.delete(chunk)) {
279
- chunk.removeChunk(this);
280
- return true;
281
- }
282
- return false;
283
- }
284
-
285
- /**
286
- * @returns {Array} - an array containing the blocks
287
- */
288
- getBlocks() {
289
- return this._blocks.getFromCache(getArray);
290
- }
291
-
292
- getNumberOfBlocks() {
293
- return this._blocks.size;
294
- }
295
-
296
- hasBlock(block) {
297
- return this._blocks.has(block);
298
- }
299
-
300
- get blocksIterable() {
301
- return this._blocks;
302
- }
303
-
304
- addBlock(block) {
305
- if (!this._blocks.has(block)) {
306
- this._blocks.add(block);
307
- return true;
308
- }
309
- return false;
310
- }
311
-
312
- addOrigin(module, loc, request) {
313
- this.origins.push({
314
- module,
315
- loc,
316
- request
317
- });
318
- }
319
-
320
- containsModule(module) {
321
- for (const chunk of this.chunks) {
322
- if (chunk.containsModule(module)) return true;
323
- }
324
- return false;
325
- }
326
-
327
- getFiles() {
328
- const files = new Set();
329
-
330
- for (const chunk of this.chunks) {
331
- for (const file of chunk.files) {
332
- files.add(file);
333
- }
334
- }
335
-
336
- return Array.from(files);
337
- }
338
-
339
- /**
340
- * @param {ModuleReason} reason reason for removing ChunkGroup
341
- * @returns {void}
342
- */
343
- remove(reason) {
344
- // cleanup parents
345
- for (const parentChunkGroup of this._parents) {
346
- // remove this chunk from its parents
347
- parentChunkGroup._children.delete(this);
348
-
349
- // cleanup "sub chunks"
350
- for (const chunkGroup of this._children) {
351
- /**
352
- * remove this chunk as "intermediary" and connect
353
- * it "sub chunks" and parents directly
354
- */
355
- // add parent to each "sub chunk"
356
- chunkGroup.addParent(parentChunkGroup);
357
- // add "sub chunk" to parent
358
- parentChunkGroup.addChild(chunkGroup);
359
- }
360
- }
361
-
362
- /**
363
- * we need to iterate again over the children
364
- * to remove this from the childs parents.
365
- * This can not be done in the above loop
366
- * as it is not guaranteed that `this._parents` contains anything.
367
- */
368
- for (const chunkGroup of this._children) {
369
- // remove this as parent of every "sub chunk"
370
- chunkGroup._parents.delete(this);
371
- }
372
-
373
- // cleanup blocks
374
- for (const block of this._blocks) {
375
- block.chunkGroup = null;
376
- }
377
-
378
- // remove chunks
379
- for (const chunk of this.chunks) {
380
- chunk.removeGroup(this);
381
- }
382
- }
383
-
384
- sortItems() {
385
- this.origins.sort(sortOrigin);
386
- this._parents.sort();
387
- this._children.sort();
388
- }
389
-
390
- /**
391
- * Sorting predicate which allows current ChunkGroup to be compared against another.
392
- * Sorting values are based off of number of chunks in ChunkGroup.
393
- *
394
- * @param {ChunkGroup} otherGroup the chunkGroup to compare this against
395
- * @returns {-1|0|1} sort position for comparison
396
- */
397
- compareTo(otherGroup) {
398
- if (this.chunks.length > otherGroup.chunks.length) return -1;
399
- if (this.chunks.length < otherGroup.chunks.length) return 1;
400
- const a = this.chunks[Symbol.iterator]();
401
- const b = otherGroup.chunks[Symbol.iterator]();
402
- // eslint-disable-next-line
403
- while (true) {
404
- const aItem = a.next();
405
- const bItem = b.next();
406
- if (aItem.done) return 0;
407
- const cmp = aItem.value.compareTo(bItem.value);
408
- if (cmp !== 0) return cmp;
409
- }
410
- }
411
-
412
- getChildrenByOrders() {
413
- const lists = new Map();
414
- for (const childGroup of this._children) {
415
- // TODO webpack 5 remove this check for options
416
- if (typeof childGroup.options === "object") {
417
- for (const key of Object.keys(childGroup.options)) {
418
- if (key.endsWith("Order")) {
419
- const name = key.substr(0, key.length - "Order".length);
420
- let list = lists.get(name);
421
- if (list === undefined) lists.set(name, (list = []));
422
- list.push({
423
- order: childGroup.options[key],
424
- group: childGroup
425
- });
426
- }
427
- }
428
- }
429
- }
430
- const result = Object.create(null);
431
- for (const [name, list] of lists) {
432
- list.sort((a, b) => {
433
- const cmp = b.order - a.order;
434
- if (cmp !== 0) return cmp;
435
- // TOOD webpack 5 remove this check of compareTo
436
- if (a.group.compareTo) return a.group.compareTo(b.group);
437
- return 0;
438
- });
439
- result[name] = list.map(i => i.group);
440
- }
441
- return result;
442
- }
443
-
444
- checkConstraints() {
445
- const chunk = this;
446
- for (const child of chunk._children) {
447
- if (!child._parents.has(chunk))
448
- throw new Error(
449
- `checkConstraints: child missing parent ${chunk.debugId} -> ${
450
- child.debugId
451
- }`
452
- );
453
- }
454
- for (const parentChunk of chunk._parents) {
455
- if (!parentChunk._children.has(chunk))
456
- throw new Error(
457
- `checkConstraints: parent missing child ${parentChunk.debugId} <- ${
458
- chunk.debugId
459
- }`
460
- );
461
- }
462
- }
463
- }
464
-
465
- module.exports = ChunkGroup;
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const SortableSet = require("./util/SortableSet");
8
+ const compareLocations = require("./compareLocations");
9
+
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: TODO, request: string}} OriginRecord */
16
+ /** @typedef {string|{name: string}} ChunkGroupOptions */
17
+
18
+ let debugId = 5000;
19
+
20
+ /**
21
+ * @template {T}
22
+ * @param {Set<T>} set set to convert to array.
23
+ * @returns {T[]} the array format of existing set
24
+ */
25
+ const getArray = set => Array.from(set);
26
+
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
+ */
33
+ const sortById = (a, b) => {
34
+ if (a.id < b.id) return -1;
35
+ if (b.id < a.id) return 1;
36
+ return 0;
37
+ };
38
+
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
+ */
44
+ const sortOrigin = (a, b) => {
45
+ const aIdent = a.module ? a.module.identifier() : "";
46
+ const bIdent = b.module ? b.module.identifier() : "";
47
+ if (aIdent < bIdent) return -1;
48
+ if (aIdent > bIdent) return 1;
49
+ return compareLocations(a.loc, b.loc);
50
+ };
51
+
52
+ class ChunkGroup {
53
+ /**
54
+ * Creates an instance of ChunkGroup.
55
+ * @param {ChunkGroupOptions=} options chunk group options passed to chunkGroup
56
+ */
57
+ constructor(options) {
58
+ if (typeof options === "string") {
59
+ options = { name: options };
60
+ } else if (!options) {
61
+ options = { name: undefined };
62
+ }
63
+ /** @type {number} */
64
+ this.groupDebugId = debugId++;
65
+ this.options = options;
66
+ this._children = new SortableSet(undefined, sortById);
67
+ this._parents = new SortableSet(undefined, sortById);
68
+ this._blocks = new SortableSet();
69
+ /** @type {Chunk[]} */
70
+ this.chunks = [];
71
+ /** @type {OriginRecord[]} */
72
+ this.origins = [];
73
+ }
74
+
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
+ */
80
+ addOptions(options) {
81
+ for (const key of Object.keys(options)) {
82
+ if (this.options[key] === undefined) {
83
+ this.options[key] = options[key];
84
+ } else if (this.options[key] !== options[key]) {
85
+ if (key.endsWith("Order")) {
86
+ this.options[key] = Math.max(this.options[key], options[key]);
87
+ } else {
88
+ throw new Error(
89
+ `ChunkGroup.addOptions: No option merge strategy for ${key}`
90
+ );
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ /**
97
+ * returns the name of current ChunkGroup
98
+ * @returns {string|undefined} returns the ChunkGroup name
99
+ */
100
+ get name() {
101
+ return this.options.name;
102
+ }
103
+
104
+ /**
105
+ * sets a new name for current ChunkGroup
106
+ * @param {string} value the new name for ChunkGroup
107
+ * @returns {void}
108
+ */
109
+ set name(value) {
110
+ this.options.name = value;
111
+ }
112
+
113
+ /**
114
+ * get a uniqueId for ChunkGroup, made up of its member Chunk debugId's
115
+ * @returns {string} a unique concatenation of chunk debugId's
116
+ */
117
+ get debugId() {
118
+ return Array.from(this.chunks, x => x.debugId).join("+");
119
+ }
120
+
121
+ /**
122
+ * get a unique id for ChunkGroup, made up of its member Chunk id's
123
+ * @returns {string} a unique concatenation of chunk ids
124
+ */
125
+ get id() {
126
+ return Array.from(this.chunks, x => x.id).join("+");
127
+ }
128
+
129
+ /**
130
+ * Performs an unshift of a specific chunk
131
+ * @param {Chunk} chunk chunk being unshifted
132
+ * @returns {boolean} returns true if attempted chunk shift is accepted
133
+ */
134
+ unshiftChunk(chunk) {
135
+ const oldIdx = this.chunks.indexOf(chunk);
136
+ if (oldIdx > 0) {
137
+ this.chunks.splice(oldIdx, 1);
138
+ this.chunks.unshift(chunk);
139
+ } else if (oldIdx < 0) {
140
+ this.chunks.unshift(chunk);
141
+ return true;
142
+ }
143
+ return false;
144
+ }
145
+
146
+ /**
147
+ * inserts a chunk before another existing chunk in group
148
+ * @param {Chunk} chunk Chunk being inserted
149
+ * @param {Chunk} before Placeholder/target chunk marking new chunk insertion point
150
+ * @returns {boolean} return true if insertion was successful
151
+ */
152
+ insertChunk(chunk, before) {
153
+ const oldIdx = this.chunks.indexOf(chunk);
154
+ const idx = this.chunks.indexOf(before);
155
+ if (idx < 0) {
156
+ throw new Error("before chunk not found");
157
+ }
158
+ if (oldIdx >= 0 && oldIdx > idx) {
159
+ this.chunks.splice(oldIdx, 1);
160
+ this.chunks.splice(idx, 0, chunk);
161
+ } else if (oldIdx < 0) {
162
+ this.chunks.splice(idx, 0, chunk);
163
+ return true;
164
+ }
165
+ return false;
166
+ }
167
+
168
+ /**
169
+ * add a chunk into ChunkGroup. Is pushed on or prepended
170
+ * @param {Chunk} chunk chunk being pushed into ChunkGroupS
171
+ * @returns {boolean} returns true if chunk addition was ssuccesful.
172
+ */
173
+ pushChunk(chunk) {
174
+ const oldIdx = this.chunks.indexOf(chunk);
175
+ if (oldIdx >= 0) {
176
+ return false;
177
+ }
178
+ this.chunks.push(chunk);
179
+ return true;
180
+ }
181
+
182
+ /**
183
+ * @param {Chunk} oldChunk chunk to be replaced
184
+ * @param {Chunk} newChunk New chunkt that will be replaced
185
+ * @returns {boolean} rerturns true for
186
+ */
187
+ replaceChunk(oldChunk, newChunk) {
188
+ const oldIdx = this.chunks.indexOf(oldChunk);
189
+ if (oldIdx < 0) return false;
190
+ const newIdx = this.chunks.indexOf(newChunk);
191
+ if (newIdx < 0) {
192
+ this.chunks[oldIdx] = newChunk;
193
+ return true;
194
+ }
195
+ if (newIdx < oldIdx) {
196
+ this.chunks.splice(oldIdx, 1);
197
+ return true;
198
+ } else if (newIdx !== oldIdx) {
199
+ this.chunks[oldIdx] = newChunk;
200
+ this.chunks.splice(newIdx, 1);
201
+ return true;
202
+ }
203
+ }
204
+
205
+ removeChunk(chunk) {
206
+ const idx = this.chunks.indexOf(chunk);
207
+ if (idx >= 0) {
208
+ this.chunks.splice(idx, 1);
209
+ return true;
210
+ }
211
+ return false;
212
+ }
213
+
214
+ isInitial() {
215
+ return false;
216
+ }
217
+
218
+ addChild(chunk) {
219
+ if (this._children.has(chunk)) {
220
+ return false;
221
+ }
222
+ this._children.add(chunk);
223
+ return true;
224
+ }
225
+
226
+ getChildren() {
227
+ return this._children.getFromCache(getArray);
228
+ }
229
+
230
+ getNumberOfChildren() {
231
+ return this._children.size;
232
+ }
233
+
234
+ get childrenIterable() {
235
+ return this._children;
236
+ }
237
+
238
+ removeChild(chunk) {
239
+ if (!this._children.has(chunk)) {
240
+ return false;
241
+ }
242
+
243
+ this._children.delete(chunk);
244
+ chunk.removeParent(this);
245
+ return true;
246
+ }
247
+
248
+ addParent(parentChunk) {
249
+ if (!this._parents.has(parentChunk)) {
250
+ this._parents.add(parentChunk);
251
+ return true;
252
+ }
253
+ return false;
254
+ }
255
+
256
+ getParents() {
257
+ return this._parents.getFromCache(getArray);
258
+ }
259
+
260
+ setParents(newParents) {
261
+ this._parents.clear();
262
+ for (const p of newParents) this._parents.add(p);
263
+ }
264
+
265
+ getNumberOfParents() {
266
+ return this._parents.size;
267
+ }
268
+
269
+ hasParent(parent) {
270
+ return this._parents.has(parent);
271
+ }
272
+
273
+ get parentsIterable() {
274
+ return this._parents;
275
+ }
276
+
277
+ removeParent(chunk) {
278
+ if (this._parents.delete(chunk)) {
279
+ chunk.removeChunk(this);
280
+ return true;
281
+ }
282
+ return false;
283
+ }
284
+
285
+ /**
286
+ * @returns {Array} - an array containing the blocks
287
+ */
288
+ getBlocks() {
289
+ return this._blocks.getFromCache(getArray);
290
+ }
291
+
292
+ getNumberOfBlocks() {
293
+ return this._blocks.size;
294
+ }
295
+
296
+ hasBlock(block) {
297
+ return this._blocks.has(block);
298
+ }
299
+
300
+ get blocksIterable() {
301
+ return this._blocks;
302
+ }
303
+
304
+ addBlock(block) {
305
+ if (!this._blocks.has(block)) {
306
+ this._blocks.add(block);
307
+ return true;
308
+ }
309
+ return false;
310
+ }
311
+
312
+ addOrigin(module, loc, request) {
313
+ this.origins.push({
314
+ module,
315
+ loc,
316
+ request
317
+ });
318
+ }
319
+
320
+ containsModule(module) {
321
+ for (const chunk of this.chunks) {
322
+ if (chunk.containsModule(module)) return true;
323
+ }
324
+ return false;
325
+ }
326
+
327
+ getFiles() {
328
+ const files = new Set();
329
+
330
+ for (const chunk of this.chunks) {
331
+ for (const file of chunk.files) {
332
+ files.add(file);
333
+ }
334
+ }
335
+
336
+ return Array.from(files);
337
+ }
338
+
339
+ /**
340
+ * @param {ModuleReason} reason reason for removing ChunkGroup
341
+ * @returns {void}
342
+ */
343
+ remove(reason) {
344
+ // cleanup parents
345
+ for (const parentChunkGroup of this._parents) {
346
+ // remove this chunk from its parents
347
+ parentChunkGroup._children.delete(this);
348
+
349
+ // cleanup "sub chunks"
350
+ for (const chunkGroup of this._children) {
351
+ /**
352
+ * remove this chunk as "intermediary" and connect
353
+ * it "sub chunks" and parents directly
354
+ */
355
+ // add parent to each "sub chunk"
356
+ chunkGroup.addParent(parentChunkGroup);
357
+ // add "sub chunk" to parent
358
+ parentChunkGroup.addChild(chunkGroup);
359
+ }
360
+ }
361
+
362
+ /**
363
+ * we need to iterate again over the children
364
+ * to remove this from the childs parents.
365
+ * This can not be done in the above loop
366
+ * as it is not guaranteed that `this._parents` contains anything.
367
+ */
368
+ for (const chunkGroup of this._children) {
369
+ // remove this as parent of every "sub chunk"
370
+ chunkGroup._parents.delete(this);
371
+ }
372
+
373
+ // cleanup blocks
374
+ for (const block of this._blocks) {
375
+ block.chunkGroup = null;
376
+ }
377
+
378
+ // remove chunks
379
+ for (const chunk of this.chunks) {
380
+ chunk.removeGroup(this);
381
+ }
382
+ }
383
+
384
+ sortItems() {
385
+ this.origins.sort(sortOrigin);
386
+ this._parents.sort();
387
+ this._children.sort();
388
+ }
389
+
390
+ /**
391
+ * Sorting predicate which allows current ChunkGroup to be compared against another.
392
+ * Sorting values are based off of number of chunks in ChunkGroup.
393
+ *
394
+ * @param {ChunkGroup} otherGroup the chunkGroup to compare this against
395
+ * @returns {-1|0|1} sort position for comparison
396
+ */
397
+ compareTo(otherGroup) {
398
+ if (this.chunks.length > otherGroup.chunks.length) return -1;
399
+ if (this.chunks.length < otherGroup.chunks.length) return 1;
400
+ const a = this.chunks[Symbol.iterator]();
401
+ const b = otherGroup.chunks[Symbol.iterator]();
402
+ // eslint-disable-next-line
403
+ while (true) {
404
+ const aItem = a.next();
405
+ const bItem = b.next();
406
+ if (aItem.done) return 0;
407
+ const cmp = aItem.value.compareTo(bItem.value);
408
+ if (cmp !== 0) return cmp;
409
+ }
410
+ }
411
+
412
+ getChildrenByOrders() {
413
+ const lists = new Map();
414
+ for (const childGroup of this._children) {
415
+ // TODO webpack 5 remove this check for options
416
+ if (typeof childGroup.options === "object") {
417
+ for (const key of Object.keys(childGroup.options)) {
418
+ if (key.endsWith("Order")) {
419
+ const name = key.substr(0, key.length - "Order".length);
420
+ let list = lists.get(name);
421
+ if (list === undefined) lists.set(name, (list = []));
422
+ list.push({
423
+ order: childGroup.options[key],
424
+ group: childGroup
425
+ });
426
+ }
427
+ }
428
+ }
429
+ }
430
+ const result = Object.create(null);
431
+ for (const [name, list] of lists) {
432
+ list.sort((a, b) => {
433
+ const cmp = b.order - a.order;
434
+ if (cmp !== 0) return cmp;
435
+ // TOOD webpack 5 remove this check of compareTo
436
+ if (a.group.compareTo) return a.group.compareTo(b.group);
437
+ return 0;
438
+ });
439
+ result[name] = list.map(i => i.group);
440
+ }
441
+ return result;
442
+ }
443
+
444
+ checkConstraints() {
445
+ const chunk = this;
446
+ for (const child of chunk._children) {
447
+ if (!child._parents.has(chunk))
448
+ throw new Error(
449
+ `checkConstraints: child missing parent ${chunk.debugId} -> ${
450
+ child.debugId
451
+ }`
452
+ );
453
+ }
454
+ for (const parentChunk of chunk._parents) {
455
+ if (!parentChunk._children.has(chunk))
456
+ throw new Error(
457
+ `checkConstraints: parent missing child ${parentChunk.debugId} <- ${
458
+ chunk.debugId
459
+ }`
460
+ );
461
+ }
462
+ }
463
+ }
464
+
465
+ module.exports = ChunkGroup;