webpack 4.1.0 → 4.4.0

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 (118) hide show
  1. package/README.md +719 -721
  2. package/bin/webpack.js +69 -10
  3. package/lib/APIPlugin.js +84 -84
  4. package/lib/AmdMainTemplatePlugin.js +75 -77
  5. package/lib/AsyncDependencyToInitialChunkError.js +21 -23
  6. package/lib/BannerPlugin.js +101 -101
  7. package/lib/Chunk.js +477 -469
  8. package/lib/ChunkTemplate.js +51 -53
  9. package/lib/Compilation.js +1858 -1851
  10. package/lib/Compiler.js +493 -478
  11. package/lib/ConcurrentCompilationError.js +19 -0
  12. package/lib/ContextModule.js +696 -685
  13. package/lib/ContextModuleFactory.js +245 -243
  14. package/lib/DefinePlugin.js +197 -197
  15. package/lib/DelegatedModule.js +101 -101
  16. package/lib/DependenciesBlockVariable.js +51 -52
  17. package/lib/Dependency.js +53 -52
  18. package/lib/DllModule.js +54 -54
  19. package/lib/DllModuleFactory.js +29 -29
  20. package/lib/EnvironmentPlugin.js +65 -67
  21. package/lib/EvalDevToolModuleTemplatePlugin.js +60 -60
  22. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +105 -105
  23. package/lib/ExportPropertyMainTemplatePlugin.js +40 -40
  24. package/lib/ExternalModule.js +159 -159
  25. package/lib/FunctionModuleTemplatePlugin.js +98 -98
  26. package/lib/HotModuleReplacement.runtime.js +631 -631
  27. package/lib/HotModuleReplacementPlugin.js +407 -406
  28. package/lib/HotUpdateChunkTemplate.js +78 -80
  29. package/lib/JavascriptGenerator.js +228 -229
  30. package/lib/JavascriptModulesPlugin.js +184 -158
  31. package/lib/JsonGenerator.js +42 -42
  32. package/lib/MainTemplate.js +406 -402
  33. package/lib/Module.js +343 -340
  34. package/lib/ModuleBuildError.js +42 -42
  35. package/lib/ModuleError.js +28 -28
  36. package/lib/ModuleFilenameHelpers.js +166 -166
  37. package/lib/ModuleTemplate.js +77 -79
  38. package/lib/ModuleWarning.js +30 -30
  39. package/lib/MultiCompiler.js +271 -259
  40. package/lib/MultiModule.js +78 -75
  41. package/lib/MultiModuleFactory.js +23 -23
  42. package/lib/MultiWatching.js +38 -37
  43. package/lib/NoModeWarning.js +23 -21
  44. package/lib/NormalModule.js +478 -470
  45. package/lib/NormalModuleFactory.js +483 -481
  46. package/lib/OptionsDefaulter.js +80 -86
  47. package/lib/Parser.js +2074 -2071
  48. package/lib/ProgressPlugin.js +231 -231
  49. package/lib/RawModule.js +54 -55
  50. package/lib/RecordIdsPlugin.js +160 -160
  51. package/lib/RemovedPluginError.js +13 -13
  52. package/lib/ResolverFactory.js +64 -67
  53. package/lib/RuntimeTemplate.js +267 -297
  54. package/lib/SetVarMainTemplatePlugin.js +57 -57
  55. package/lib/SourceMapDevToolPlugin.js +302 -308
  56. package/lib/Stats.js +1234 -1212
  57. package/lib/Template.js +205 -205
  58. package/lib/TemplatedPathPlugin.js +170 -143
  59. package/lib/UmdMainTemplatePlugin.js +264 -269
  60. package/lib/Watching.js +193 -193
  61. package/lib/WebAssemblyParser.js +50 -54
  62. package/lib/WebpackOptionsApply.js +401 -401
  63. package/lib/WebpackOptionsDefaulter.js +337 -317
  64. package/lib/WebpackOptionsValidationError.js +316 -319
  65. package/lib/debug/ProfilingPlugin.js +409 -405
  66. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +328 -311
  67. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  68. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +270 -241
  69. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  70. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -606
  71. package/lib/dependencies/HarmonyExportSpecifierDependency.js +53 -53
  72. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +214 -214
  73. package/lib/dependencies/HarmonyImportSpecifierDependency.js +154 -156
  74. package/lib/dependencies/ImportDependenciesBlock.js +17 -17
  75. package/lib/dependencies/ImportDependency.js +34 -34
  76. package/lib/dependencies/ImportEagerDependency.js +32 -32
  77. package/lib/dependencies/ImportParserPlugin.js +175 -179
  78. package/lib/dependencies/ImportWeakDependency.js +34 -34
  79. package/lib/dependencies/JsonExportsDependency.js +25 -25
  80. package/lib/dependencies/ModuleDependency.js +20 -20
  81. package/lib/dependencies/NullDependency.js +20 -20
  82. package/lib/dependencies/RequireContextDependency.js +22 -22
  83. package/lib/dependencies/RequireIncludeDependency.js +40 -40
  84. package/lib/dependencies/WebpackMissingModule.js +20 -22
  85. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  86. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  87. package/lib/node/NodeMainTemplatePlugin.js +320 -273
  88. package/lib/node/ReadFileCompileWasmMainTemplatePlugin.js +113 -115
  89. package/lib/optimize/AggressiveSplittingPlugin.js +281 -281
  90. package/lib/optimize/ConcatenatedModule.js +1364 -1366
  91. package/lib/optimize/RemoveParentModulesPlugin.js +114 -114
  92. package/lib/optimize/SplitChunksPlugin.js +519 -491
  93. package/lib/performance/SizeLimitsPlugin.js +105 -105
  94. package/lib/util/TrackingSet.js +35 -35
  95. package/lib/util/objectToMap.js +10 -10
  96. package/lib/wasm/WasmModuleTemplatePlugin.js +106 -106
  97. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  98. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  99. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  100. package/lib/web/JsonpMainTemplatePlugin.js +425 -403
  101. package/lib/webpack.js +182 -179
  102. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  103. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  104. package/lib/webworker/WebWorkerMainTemplatePlugin.js +177 -154
  105. package/package.json +9 -8
  106. package/schemas/WebpackOptions.json +1973 -1951
  107. package/schemas/ajv.absolutePath.js +55 -29
  108. package/schemas/plugins/BannerPlugin.json +85 -85
  109. package/schemas/plugins/DllPlugin.json +28 -28
  110. package/schemas/plugins/DllReferencePlugin.json +99 -99
  111. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  112. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  113. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  114. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  115. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  116. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  117. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  118. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
package/lib/Chunk.js CHANGED
@@ -1,469 +1,477 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const util = require("util");
8
- const SortableSet = require("./util/SortableSet");
9
- const GraphHelpers = require("./GraphHelpers");
10
- let debugId = 1000;
11
- const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()";
12
- const ERR_CHUNK_INITIAL =
13
- "Chunk.initial was removed. Use canBeInitial/isOnlyInitial()";
14
-
15
- const sortById = (a, b) => {
16
- if (a.id < b.id) return -1;
17
- if (b.id < a.id) return 1;
18
- return 0;
19
- };
20
-
21
- const sortByIdentifier = (a, b) => {
22
- if (a.identifier() > b.identifier()) return 1;
23
- if (a.identifier() < b.identifier()) return -1;
24
- return 0;
25
- };
26
-
27
- const getModulesIdent = set => {
28
- set.sort();
29
- let str = "";
30
- for (const m of set) {
31
- str += m.identifier() + "#";
32
- }
33
- return str;
34
- };
35
-
36
- const getArray = set => Array.from(set);
37
-
38
- const getModulesSize = set => {
39
- let count = 0;
40
- for (const module of set) {
41
- count += module.size();
42
- }
43
- return count;
44
- };
45
-
46
- class Chunk {
47
- constructor(name) {
48
- this.id = null;
49
- this.ids = null;
50
- this.debugId = debugId++;
51
- this.name = name;
52
- this.entryModule = undefined;
53
- this._modules = new SortableSet(undefined, sortByIdentifier);
54
- this._groups = new SortableSet(undefined, sortById);
55
- this.files = [];
56
- this.rendered = false;
57
- this.hash = undefined;
58
- this.renderedHash = undefined;
59
- this.chunkReason = undefined;
60
- this.extraAsync = false;
61
- }
62
-
63
- get entry() {
64
- throw new Error(ERR_CHUNK_ENTRY);
65
- }
66
-
67
- set entry(data) {
68
- throw new Error(ERR_CHUNK_ENTRY);
69
- }
70
-
71
- get initial() {
72
- throw new Error(ERR_CHUNK_INITIAL);
73
- }
74
-
75
- set initial(data) {
76
- throw new Error(ERR_CHUNK_INITIAL);
77
- }
78
-
79
- hasRuntime() {
80
- for (const chunkGroup of this._groups) {
81
- // We only need to check the first one
82
- return chunkGroup.isInitial() && chunkGroup.getRuntimeChunk() === this;
83
- }
84
- return false;
85
- }
86
-
87
- canBeInitial() {
88
- for (const chunkGroup of this._groups) {
89
- if (chunkGroup.isInitial()) return true;
90
- }
91
- return false;
92
- }
93
-
94
- isOnlyInitial() {
95
- if (this._groups.size <= 0) return false;
96
- for (const chunkGroup of this._groups) {
97
- if (!chunkGroup.isInitial()) return false;
98
- }
99
- return true;
100
- }
101
-
102
- hasEntryModule() {
103
- return !!this.entryModule;
104
- }
105
-
106
- addModule(module) {
107
- if (!this._modules.has(module)) {
108
- this._modules.add(module);
109
- return true;
110
- }
111
- return false;
112
- }
113
-
114
- removeModule(module) {
115
- if (this._modules.delete(module)) {
116
- module.removeChunk(this);
117
- return true;
118
- }
119
- return false;
120
- }
121
-
122
- setModules(modules) {
123
- this._modules = new SortableSet(modules, sortByIdentifier);
124
- }
125
-
126
- getNumberOfModules() {
127
- return this._modules.size;
128
- }
129
-
130
- get modulesIterable() {
131
- return this._modules;
132
- }
133
-
134
- addGroup(chunkGroup) {
135
- if (this._groups.has(chunkGroup)) return false;
136
- this._groups.add(chunkGroup);
137
- return true;
138
- }
139
-
140
- removeGroup(chunkGroup) {
141
- if (!this._groups.has(chunkGroup)) return false;
142
- this._groups.delete(chunkGroup);
143
- return true;
144
- }
145
-
146
- isInGroup(chunkGroup) {
147
- return this._groups.has(chunkGroup);
148
- }
149
-
150
- getNumberOfGroups() {
151
- return this._groups.size;
152
- }
153
-
154
- get groupsIterable() {
155
- return this._groups;
156
- }
157
-
158
- compareTo(otherChunk) {
159
- this._modules.sort();
160
- otherChunk._modules.sort();
161
- if (this._modules.size > otherChunk._modules.size) return -1;
162
- if (this._modules.size < otherChunk._modules.size) return 1;
163
- const a = this._modules[Symbol.iterator]();
164
- const b = otherChunk._modules[Symbol.iterator]();
165
- // eslint-disable-next-line
166
- while (true) {
167
- const aItem = a.next();
168
- const bItem = b.next();
169
- if (aItem.done) return 0;
170
- const aModuleIdentifier = aItem.value.identifier();
171
- const bModuleIdentifier = bItem.value.identifier();
172
- if (aModuleIdentifier > bModuleIdentifier) return -1;
173
- if (aModuleIdentifier < bModuleIdentifier) return 1;
174
- }
175
- }
176
-
177
- containsModule(module) {
178
- return this._modules.has(module);
179
- }
180
-
181
- getModules() {
182
- return this._modules.getFromCache(getArray);
183
- }
184
-
185
- getModulesIdent() {
186
- return this._modules.getFromUnorderedCache(getModulesIdent);
187
- }
188
-
189
- remove(reason) {
190
- // cleanup modules
191
- // Array.from is used here to create a clone, because removeChunk modifies this._modules
192
- for (const module of Array.from(this._modules)) {
193
- module.removeChunk(this);
194
- }
195
- for (const chunkGroup of this._groups) {
196
- chunkGroup.removeChunk(this);
197
- }
198
- }
199
-
200
- moveModule(module, otherChunk) {
201
- GraphHelpers.disconnectChunkAndModule(this, module);
202
- GraphHelpers.connectChunkAndModule(otherChunk, module);
203
- module.rewriteChunkInReasons(this, [otherChunk]);
204
- }
205
-
206
- integrate(otherChunk, reason) {
207
- if (!this.canBeIntegrated(otherChunk)) {
208
- return false;
209
- }
210
-
211
- // Array.from is used here to create a clone, because moveModule modifies otherChunk._modules
212
- for (const module of Array.from(otherChunk._modules)) {
213
- otherChunk.moveModule(module, this);
214
- }
215
- otherChunk._modules.clear();
216
-
217
- for (const chunkGroup of otherChunk._groups) {
218
- chunkGroup.replaceChunk(otherChunk, this);
219
- this.addGroup(chunkGroup);
220
- }
221
- otherChunk._groups.clear();
222
-
223
- if (this.name && otherChunk.name) {
224
- if (this.name.length !== otherChunk.name.length)
225
- this.name =
226
- this.name.length < otherChunk.name.length
227
- ? this.name
228
- : otherChunk.name;
229
- else
230
- this.name = this.name < otherChunk.name ? this.name : otherChunk.name;
231
- }
232
-
233
- return true;
234
- }
235
-
236
- split(newChunk) {
237
- for (const chunkGroup of this._groups) {
238
- chunkGroup.insertChunk(newChunk, this);
239
- newChunk.addGroup(chunkGroup);
240
- }
241
- }
242
-
243
- isEmpty() {
244
- return this._modules.size === 0;
245
- }
246
-
247
- updateHash(hash) {
248
- hash.update(`${this.id} `);
249
- hash.update(this.ids ? this.ids.join(",") : "");
250
- hash.update(`${this.name || ""} `);
251
- for (const m of this._modules) {
252
- hash.update(m.hash);
253
- }
254
- }
255
-
256
- canBeIntegrated(otherChunk) {
257
- const isAvailable = (a, b) => {
258
- const queue = new Set(b.groupsIterable);
259
- for (const chunkGroup of queue) {
260
- if (a.isInGroup(chunkGroup)) continue;
261
- if (chunkGroup.isInitial()) return false;
262
- for (const parent of chunkGroup.parentsIterable) queue.add(parent);
263
- }
264
- return true;
265
- };
266
- if (this.hasRuntime() !== otherChunk.hasRuntime()) {
267
- if (this.hasRuntime()) {
268
- return isAvailable(this, otherChunk);
269
- } else if (otherChunk.hasRuntime()) {
270
- return isAvailable(otherChunk, this);
271
- } else {
272
- return false;
273
- }
274
- }
275
- if (this.hasEntryModule() || otherChunk.hasEntryModule()) return false;
276
- return true;
277
- }
278
-
279
- addMultiplierAndOverhead(size, options) {
280
- const overhead =
281
- typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
282
- const multiplicator = this.canBeInitial()
283
- ? options.entryChunkMultiplicator || 10
284
- : 1;
285
-
286
- return size * multiplicator + overhead;
287
- }
288
-
289
- modulesSize() {
290
- return this._modules.getFromUnorderedCache(getModulesSize);
291
- }
292
-
293
- size(options) {
294
- return this.addMultiplierAndOverhead(this.modulesSize(), options);
295
- }
296
-
297
- integratedSize(otherChunk, options) {
298
- // Chunk if it's possible to integrate this chunk
299
- if (!this.canBeIntegrated(otherChunk)) {
300
- return false;
301
- }
302
-
303
- let integratedModulesSize = this.modulesSize();
304
- // only count modules that do not exist in this chunk!
305
- for (const otherModule of otherChunk._modules) {
306
- if (!this._modules.has(otherModule)) {
307
- integratedModulesSize += otherModule.size();
308
- }
309
- }
310
-
311
- return this.addMultiplierAndOverhead(integratedModulesSize, options);
312
- }
313
-
314
- sortModules(sortByFn) {
315
- this._modules.sortWith(sortByFn || sortById);
316
- }
317
-
318
- sortItems(sortChunks) {
319
- this.sortModules();
320
- }
321
-
322
- getAllAsyncChunks() {
323
- const initialChunks = new Set();
324
- const queue = new Set(this.groupsIterable);
325
- const chunks = new Set();
326
-
327
- for (const chunkGroup of queue) {
328
- for (const chunk of chunkGroup.chunks) initialChunks.add(chunk);
329
- }
330
-
331
- for (const chunkGroup of queue) {
332
- for (const chunk of chunkGroup.chunks) {
333
- if (!initialChunks.has(chunk)) chunks.add(chunk);
334
- }
335
- for (const child of chunkGroup.childrenIterable) queue.add(child);
336
- }
337
-
338
- return chunks;
339
- }
340
-
341
- getChunkMaps(realHash) {
342
- const chunkHashMap = Object.create(null);
343
- const chunkNameMap = Object.create(null);
344
-
345
- for (const chunk of this.getAllAsyncChunks()) {
346
- chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
347
- if (chunk.name) chunkNameMap[chunk.id] = chunk.name;
348
- }
349
-
350
- return {
351
- hash: chunkHashMap,
352
- name: chunkNameMap
353
- };
354
- }
355
-
356
- getChunkModuleMaps(filterFn) {
357
- const chunkModuleIdMap = Object.create(null);
358
- const chunkModuleHashMap = Object.create(null);
359
-
360
- for (const chunk of this.getAllAsyncChunks()) {
361
- let array;
362
- for (const module of chunk.modulesIterable) {
363
- if (filterFn(module)) {
364
- if (array === undefined) {
365
- array = [];
366
- chunkModuleIdMap[chunk.id] = array;
367
- }
368
- array.push(module.id);
369
- chunkModuleHashMap[module.id] = module.renderedHash;
370
- }
371
- }
372
- if (array !== undefined) {
373
- array.sort();
374
- }
375
- }
376
-
377
- return {
378
- id: chunkModuleIdMap,
379
- hash: chunkModuleHashMap
380
- };
381
- }
382
-
383
- hasModuleInGraph(filterFn, filterChunkFn) {
384
- const queue = new Set(this.groupsIterable);
385
- const chunksProcessed = new Set();
386
-
387
- for (const chunkGroup of queue) {
388
- for (const chunk of chunkGroup.chunks) {
389
- if (!chunksProcessed.has(chunk)) {
390
- chunksProcessed.add(chunk);
391
- if (!filterChunkFn || filterChunkFn(chunk)) {
392
- for (const module of chunk.modulesIterable)
393
- if (filterFn(module)) return true;
394
- }
395
- }
396
- }
397
- for (const child of chunkGroup.childrenIterable) queue.add(child);
398
- }
399
- return false;
400
- }
401
-
402
- toString() {
403
- return `Chunk[${Array.from(this._modules).join()}]`;
404
- }
405
- }
406
-
407
- // TODO remove in webpack 5
408
- Object.defineProperty(Chunk.prototype, "forEachModule", {
409
- configurable: false,
410
- value: util.deprecate(function(fn) {
411
- this._modules.forEach(fn);
412
- }, "Chunk.forEachModule: Use for(const module of chunk.modulesIterable) instead")
413
- });
414
-
415
- // TODO remove in webpack 5
416
- Object.defineProperty(Chunk.prototype, "mapModules", {
417
- configurable: false,
418
- value: util.deprecate(function(fn) {
419
- return Array.from(this._modules, fn);
420
- }, "Chunk.mapModules: Use Array.from(chunk.modulesIterable, fn) instead")
421
- });
422
-
423
- // TODO remove in webpack 5
424
- Object.defineProperty(Chunk.prototype, "chunks", {
425
- configurable: false,
426
- get() {
427
- throw new Error("Chunk.chunks: Use ChunkGroup.getChildren() instead");
428
- },
429
- set() {
430
- throw new Error("Chunk.chunks: Use ChunkGroup.add/removeChild() instead");
431
- }
432
- });
433
-
434
- // TODO remove in webpack 5
435
- Object.defineProperty(Chunk.prototype, "parents", {
436
- configurable: false,
437
- get() {
438
- throw new Error("Chunk.parents: Use ChunkGroup.getParents() instead");
439
- },
440
- set() {
441
- throw new Error("Chunk.parents: Use ChunkGroup.add/removeParent() instead");
442
- }
443
- });
444
-
445
- // TODO remove in webpack 5
446
- Object.defineProperty(Chunk.prototype, "blocks", {
447
- configurable: false,
448
- get() {
449
- throw new Error("Chunk.blocks: Use ChunkGroup.getBlocks() instead");
450
- },
451
- set() {
452
- throw new Error("Chunk.blocks: Use ChunkGroup.add/removeBlock() instead");
453
- }
454
- });
455
-
456
- // TODO remove in webpack 5
457
- Object.defineProperty(Chunk.prototype, "entrypoints", {
458
- configurable: false,
459
- get() {
460
- throw new Error(
461
- "Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead"
462
- );
463
- },
464
- set() {
465
- throw new Error("Chunk.entrypoints: Use Chunks.addGroup instead");
466
- }
467
- });
468
-
469
- module.exports = Chunk;
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const util = require("util");
8
+ const SortableSet = require("./util/SortableSet");
9
+ const GraphHelpers = require("./GraphHelpers");
10
+ let debugId = 1000;
11
+ const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()";
12
+ const ERR_CHUNK_INITIAL =
13
+ "Chunk.initial was removed. Use canBeInitial/isOnlyInitial()";
14
+
15
+ const sortById = (a, b) => {
16
+ if (a.id < b.id) return -1;
17
+ if (b.id < a.id) return 1;
18
+ return 0;
19
+ };
20
+
21
+ const sortByIdentifier = (a, b) => {
22
+ if (a.identifier() > b.identifier()) return 1;
23
+ if (a.identifier() < b.identifier()) return -1;
24
+ return 0;
25
+ };
26
+
27
+ const getModulesIdent = set => {
28
+ set.sort();
29
+ let str = "";
30
+ for (const m of set) {
31
+ str += m.identifier() + "#";
32
+ }
33
+ return str;
34
+ };
35
+
36
+ const getArray = set => Array.from(set);
37
+
38
+ const getModulesSize = set => {
39
+ let count = 0;
40
+ for (const module of set) {
41
+ count += module.size();
42
+ }
43
+ return count;
44
+ };
45
+
46
+ class Chunk {
47
+ constructor(name) {
48
+ this.id = null;
49
+ this.ids = null;
50
+ this.debugId = debugId++;
51
+ this.name = name;
52
+ this.entryModule = undefined;
53
+ this._modules = new SortableSet(undefined, sortByIdentifier);
54
+ this._groups = new SortableSet(undefined, sortById);
55
+ this.files = [];
56
+ this.rendered = false;
57
+ this.hash = undefined;
58
+ this.contentHash = Object.create(null);
59
+ this.renderedHash = undefined;
60
+ this.chunkReason = undefined;
61
+ this.extraAsync = false;
62
+ }
63
+
64
+ get entry() {
65
+ throw new Error(ERR_CHUNK_ENTRY);
66
+ }
67
+
68
+ set entry(data) {
69
+ throw new Error(ERR_CHUNK_ENTRY);
70
+ }
71
+
72
+ get initial() {
73
+ throw new Error(ERR_CHUNK_INITIAL);
74
+ }
75
+
76
+ set initial(data) {
77
+ throw new Error(ERR_CHUNK_INITIAL);
78
+ }
79
+
80
+ hasRuntime() {
81
+ for (const chunkGroup of this._groups) {
82
+ // We only need to check the first one
83
+ return chunkGroup.isInitial() && chunkGroup.getRuntimeChunk() === this;
84
+ }
85
+ return false;
86
+ }
87
+
88
+ canBeInitial() {
89
+ for (const chunkGroup of this._groups) {
90
+ if (chunkGroup.isInitial()) return true;
91
+ }
92
+ return false;
93
+ }
94
+
95
+ isOnlyInitial() {
96
+ if (this._groups.size <= 0) return false;
97
+ for (const chunkGroup of this._groups) {
98
+ if (!chunkGroup.isInitial()) return false;
99
+ }
100
+ return true;
101
+ }
102
+
103
+ hasEntryModule() {
104
+ return !!this.entryModule;
105
+ }
106
+
107
+ addModule(module) {
108
+ if (!this._modules.has(module)) {
109
+ this._modules.add(module);
110
+ return true;
111
+ }
112
+ return false;
113
+ }
114
+
115
+ removeModule(module) {
116
+ if (this._modules.delete(module)) {
117
+ module.removeChunk(this);
118
+ return true;
119
+ }
120
+ return false;
121
+ }
122
+
123
+ setModules(modules) {
124
+ this._modules = new SortableSet(modules, sortByIdentifier);
125
+ }
126
+
127
+ getNumberOfModules() {
128
+ return this._modules.size;
129
+ }
130
+
131
+ get modulesIterable() {
132
+ return this._modules;
133
+ }
134
+
135
+ addGroup(chunkGroup) {
136
+ if (this._groups.has(chunkGroup)) return false;
137
+ this._groups.add(chunkGroup);
138
+ return true;
139
+ }
140
+
141
+ removeGroup(chunkGroup) {
142
+ if (!this._groups.has(chunkGroup)) return false;
143
+ this._groups.delete(chunkGroup);
144
+ return true;
145
+ }
146
+
147
+ isInGroup(chunkGroup) {
148
+ return this._groups.has(chunkGroup);
149
+ }
150
+
151
+ getNumberOfGroups() {
152
+ return this._groups.size;
153
+ }
154
+
155
+ get groupsIterable() {
156
+ return this._groups;
157
+ }
158
+
159
+ compareTo(otherChunk) {
160
+ this._modules.sort();
161
+ otherChunk._modules.sort();
162
+ if (this._modules.size > otherChunk._modules.size) return -1;
163
+ if (this._modules.size < otherChunk._modules.size) return 1;
164
+ const a = this._modules[Symbol.iterator]();
165
+ const b = otherChunk._modules[Symbol.iterator]();
166
+ // eslint-disable-next-line
167
+ while (true) {
168
+ const aItem = a.next();
169
+ const bItem = b.next();
170
+ if (aItem.done) return 0;
171
+ const aModuleIdentifier = aItem.value.identifier();
172
+ const bModuleIdentifier = bItem.value.identifier();
173
+ if (aModuleIdentifier > bModuleIdentifier) return -1;
174
+ if (aModuleIdentifier < bModuleIdentifier) return 1;
175
+ }
176
+ }
177
+
178
+ containsModule(module) {
179
+ return this._modules.has(module);
180
+ }
181
+
182
+ getModules() {
183
+ return this._modules.getFromCache(getArray);
184
+ }
185
+
186
+ getModulesIdent() {
187
+ return this._modules.getFromUnorderedCache(getModulesIdent);
188
+ }
189
+
190
+ remove(reason) {
191
+ // cleanup modules
192
+ // Array.from is used here to create a clone, because removeChunk modifies this._modules
193
+ for (const module of Array.from(this._modules)) {
194
+ module.removeChunk(this);
195
+ }
196
+ for (const chunkGroup of this._groups) {
197
+ chunkGroup.removeChunk(this);
198
+ }
199
+ }
200
+
201
+ moveModule(module, otherChunk) {
202
+ GraphHelpers.disconnectChunkAndModule(this, module);
203
+ GraphHelpers.connectChunkAndModule(otherChunk, module);
204
+ module.rewriteChunkInReasons(this, [otherChunk]);
205
+ }
206
+
207
+ integrate(otherChunk, reason) {
208
+ if (!this.canBeIntegrated(otherChunk)) {
209
+ return false;
210
+ }
211
+
212
+ // Array.from is used here to create a clone, because moveModule modifies otherChunk._modules
213
+ for (const module of Array.from(otherChunk._modules)) {
214
+ otherChunk.moveModule(module, this);
215
+ }
216
+ otherChunk._modules.clear();
217
+
218
+ for (const chunkGroup of otherChunk._groups) {
219
+ chunkGroup.replaceChunk(otherChunk, this);
220
+ this.addGroup(chunkGroup);
221
+ }
222
+ otherChunk._groups.clear();
223
+
224
+ if (this.name && otherChunk.name) {
225
+ if (this.name.length !== otherChunk.name.length)
226
+ this.name =
227
+ this.name.length < otherChunk.name.length
228
+ ? this.name
229
+ : otherChunk.name;
230
+ else
231
+ this.name = this.name < otherChunk.name ? this.name : otherChunk.name;
232
+ }
233
+
234
+ return true;
235
+ }
236
+
237
+ split(newChunk) {
238
+ for (const chunkGroup of this._groups) {
239
+ chunkGroup.insertChunk(newChunk, this);
240
+ newChunk.addGroup(chunkGroup);
241
+ }
242
+ }
243
+
244
+ isEmpty() {
245
+ return this._modules.size === 0;
246
+ }
247
+
248
+ updateHash(hash) {
249
+ hash.update(`${this.id} `);
250
+ hash.update(this.ids ? this.ids.join(",") : "");
251
+ hash.update(`${this.name || ""} `);
252
+ for (const m of this._modules) {
253
+ hash.update(m.hash);
254
+ }
255
+ }
256
+
257
+ canBeIntegrated(otherChunk) {
258
+ const isAvailable = (a, b) => {
259
+ const queue = new Set(b.groupsIterable);
260
+ for (const chunkGroup of queue) {
261
+ if (a.isInGroup(chunkGroup)) continue;
262
+ if (chunkGroup.isInitial()) return false;
263
+ for (const parent of chunkGroup.parentsIterable) queue.add(parent);
264
+ }
265
+ return true;
266
+ };
267
+ if (this.hasRuntime() !== otherChunk.hasRuntime()) {
268
+ if (this.hasRuntime()) {
269
+ return isAvailable(this, otherChunk);
270
+ } else if (otherChunk.hasRuntime()) {
271
+ return isAvailable(otherChunk, this);
272
+ } else {
273
+ return false;
274
+ }
275
+ }
276
+ if (this.hasEntryModule() || otherChunk.hasEntryModule()) return false;
277
+ return true;
278
+ }
279
+
280
+ addMultiplierAndOverhead(size, options) {
281
+ const overhead =
282
+ typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
283
+ const multiplicator = this.canBeInitial()
284
+ ? options.entryChunkMultiplicator || 10
285
+ : 1;
286
+
287
+ return size * multiplicator + overhead;
288
+ }
289
+
290
+ modulesSize() {
291
+ return this._modules.getFromUnorderedCache(getModulesSize);
292
+ }
293
+
294
+ size(options) {
295
+ return this.addMultiplierAndOverhead(this.modulesSize(), options);
296
+ }
297
+
298
+ integratedSize(otherChunk, options) {
299
+ // Chunk if it's possible to integrate this chunk
300
+ if (!this.canBeIntegrated(otherChunk)) {
301
+ return false;
302
+ }
303
+
304
+ let integratedModulesSize = this.modulesSize();
305
+ // only count modules that do not exist in this chunk!
306
+ for (const otherModule of otherChunk._modules) {
307
+ if (!this._modules.has(otherModule)) {
308
+ integratedModulesSize += otherModule.size();
309
+ }
310
+ }
311
+
312
+ return this.addMultiplierAndOverhead(integratedModulesSize, options);
313
+ }
314
+
315
+ sortModules(sortByFn) {
316
+ this._modules.sortWith(sortByFn || sortById);
317
+ }
318
+
319
+ sortItems(sortChunks) {
320
+ this.sortModules();
321
+ }
322
+
323
+ getAllAsyncChunks() {
324
+ const initialChunks = new Set();
325
+ const queue = new Set(this.groupsIterable);
326
+ const chunks = new Set();
327
+
328
+ for (const chunkGroup of queue) {
329
+ for (const chunk of chunkGroup.chunks) initialChunks.add(chunk);
330
+ }
331
+
332
+ for (const chunkGroup of queue) {
333
+ for (const chunk of chunkGroup.chunks) {
334
+ if (!initialChunks.has(chunk)) chunks.add(chunk);
335
+ }
336
+ for (const child of chunkGroup.childrenIterable) queue.add(child);
337
+ }
338
+
339
+ return chunks;
340
+ }
341
+
342
+ getChunkMaps(realHash) {
343
+ const chunkHashMap = Object.create(null);
344
+ const chunkContentHashMap = Object.create(null);
345
+ const chunkNameMap = Object.create(null);
346
+
347
+ for (const chunk of this.getAllAsyncChunks()) {
348
+ chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
349
+ for (const key of Object.keys(chunk.contentHash)) {
350
+ if (!chunkContentHashMap[key])
351
+ chunkContentHashMap[key] = Object.create(null);
352
+ chunkContentHashMap[key][chunk.id] = chunk.contentHash[key];
353
+ }
354
+ if (chunk.name) chunkNameMap[chunk.id] = chunk.name;
355
+ }
356
+
357
+ return {
358
+ hash: chunkHashMap,
359
+ contentHash: chunkContentHashMap,
360
+ name: chunkNameMap
361
+ };
362
+ }
363
+
364
+ getChunkModuleMaps(filterFn) {
365
+ const chunkModuleIdMap = Object.create(null);
366
+ const chunkModuleHashMap = Object.create(null);
367
+
368
+ for (const chunk of this.getAllAsyncChunks()) {
369
+ let array;
370
+ for (const module of chunk.modulesIterable) {
371
+ if (filterFn(module)) {
372
+ if (array === undefined) {
373
+ array = [];
374
+ chunkModuleIdMap[chunk.id] = array;
375
+ }
376
+ array.push(module.id);
377
+ chunkModuleHashMap[module.id] = module.renderedHash;
378
+ }
379
+ }
380
+ if (array !== undefined) {
381
+ array.sort();
382
+ }
383
+ }
384
+
385
+ return {
386
+ id: chunkModuleIdMap,
387
+ hash: chunkModuleHashMap
388
+ };
389
+ }
390
+
391
+ hasModuleInGraph(filterFn, filterChunkFn) {
392
+ const queue = new Set(this.groupsIterable);
393
+ const chunksProcessed = new Set();
394
+
395
+ for (const chunkGroup of queue) {
396
+ for (const chunk of chunkGroup.chunks) {
397
+ if (!chunksProcessed.has(chunk)) {
398
+ chunksProcessed.add(chunk);
399
+ if (!filterChunkFn || filterChunkFn(chunk)) {
400
+ for (const module of chunk.modulesIterable)
401
+ if (filterFn(module)) return true;
402
+ }
403
+ }
404
+ }
405
+ for (const child of chunkGroup.childrenIterable) queue.add(child);
406
+ }
407
+ return false;
408
+ }
409
+
410
+ toString() {
411
+ return `Chunk[${Array.from(this._modules).join()}]`;
412
+ }
413
+ }
414
+
415
+ // TODO remove in webpack 5
416
+ Object.defineProperty(Chunk.prototype, "forEachModule", {
417
+ configurable: false,
418
+ value: util.deprecate(function(fn) {
419
+ this._modules.forEach(fn);
420
+ }, "Chunk.forEachModule: Use for(const module of chunk.modulesIterable) instead")
421
+ });
422
+
423
+ // TODO remove in webpack 5
424
+ Object.defineProperty(Chunk.prototype, "mapModules", {
425
+ configurable: false,
426
+ value: util.deprecate(function(fn) {
427
+ return Array.from(this._modules, fn);
428
+ }, "Chunk.mapModules: Use Array.from(chunk.modulesIterable, fn) instead")
429
+ });
430
+
431
+ // TODO remove in webpack 5
432
+ Object.defineProperty(Chunk.prototype, "chunks", {
433
+ configurable: false,
434
+ get() {
435
+ throw new Error("Chunk.chunks: Use ChunkGroup.getChildren() instead");
436
+ },
437
+ set() {
438
+ throw new Error("Chunk.chunks: Use ChunkGroup.add/removeChild() instead");
439
+ }
440
+ });
441
+
442
+ // TODO remove in webpack 5
443
+ Object.defineProperty(Chunk.prototype, "parents", {
444
+ configurable: false,
445
+ get() {
446
+ throw new Error("Chunk.parents: Use ChunkGroup.getParents() instead");
447
+ },
448
+ set() {
449
+ throw new Error("Chunk.parents: Use ChunkGroup.add/removeParent() instead");
450
+ }
451
+ });
452
+
453
+ // TODO remove in webpack 5
454
+ Object.defineProperty(Chunk.prototype, "blocks", {
455
+ configurable: false,
456
+ get() {
457
+ throw new Error("Chunk.blocks: Use ChunkGroup.getBlocks() instead");
458
+ },
459
+ set() {
460
+ throw new Error("Chunk.blocks: Use ChunkGroup.add/removeBlock() instead");
461
+ }
462
+ });
463
+
464
+ // TODO remove in webpack 5
465
+ Object.defineProperty(Chunk.prototype, "entrypoints", {
466
+ configurable: false,
467
+ get() {
468
+ throw new Error(
469
+ "Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead"
470
+ );
471
+ },
472
+ set() {
473
+ throw new Error("Chunk.entrypoints: Use Chunks.addGroup instead");
474
+ }
475
+ });
476
+
477
+ module.exports = Chunk;