webpack 4.36.0 → 4.39.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.
- package/declarations/WebpackOptions.d.ts +35 -10
- package/lib/Chunk.js +5 -1
- package/lib/ChunkGroup.js +1 -1
- package/lib/Compilation.js +83 -477
- package/lib/Compiler.js +42 -2
- package/lib/MultiCompiler.js +8 -1
- package/lib/NormalModule.js +15 -4
- package/lib/ProgressPlugin.js +21 -63
- package/lib/ResolverFactory.js +9 -13
- package/lib/SourceMapDevToolPlugin.js +25 -17
- package/lib/Stats.js +227 -3
- package/lib/SystemMainTemplatePlugin.js +5 -1
- package/lib/TemplatedPathPlugin.js +8 -1
- package/lib/WebpackOptionsDefaulter.js +12 -1
- package/lib/buildChunkGraph.js +689 -0
- package/lib/logging/Logger.js +131 -0
- package/lib/logging/createConsoleLogger.js +209 -0
- package/lib/logging/runtime.js +36 -0
- package/lib/logging/truncateArgs.js +74 -0
- package/lib/node/NodeEnvironmentPlugin.js +16 -0
- package/lib/node/nodeConsole.js +133 -0
- package/lib/web/JsonpMainTemplatePlugin.js +1 -1
- package/lib/webpack.js +3 -1
- package/package.json +18 -20
- package/schemas/WebpackOptions.json +53 -1
@@ -0,0 +1,689 @@
|
|
1
|
+
/*
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
+
Author Tobias Koppers @sokra
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError");
|
9
|
+
const GraphHelpers = require("./GraphHelpers");
|
10
|
+
|
11
|
+
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
12
|
+
/** @typedef {import("./Chunk")} Chunk */
|
13
|
+
/** @typedef {import("./ChunkGroup")} ChunkGroup */
|
14
|
+
/** @typedef {import("./Compilation")} Compilation */
|
15
|
+
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
|
16
|
+
/** @typedef {import("./Dependency")} Dependency */
|
17
|
+
/** @typedef {import("./Entrypoint")} Entrypoint */
|
18
|
+
/** @typedef {import("./Module")} Module */
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @typedef {Object} QueueItem
|
22
|
+
* @property {number} action
|
23
|
+
* @property {DependenciesBlock} block
|
24
|
+
* @property {Module} module
|
25
|
+
* @property {Chunk} chunk
|
26
|
+
* @property {ChunkGroup} chunkGroup
|
27
|
+
*/
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @typedef {Object} ChunkGroupInfo
|
31
|
+
* @property {Set<Module>} minAvailableModules current minimal set of modules available at this point
|
32
|
+
* @property {boolean} minAvailableModulesOwned true, if minAvailableModules is owned and can be modified
|
33
|
+
* @property {Set<Module>[]} availableModulesToBeMerged enqueued updates to the minimal set of available modules
|
34
|
+
* @property {QueueItem[]} skippedItems queue items that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules is shrinking)
|
35
|
+
* @property {Set<Module>} resultingAvailableModules set of modules available including modules from this chunk group
|
36
|
+
*/
|
37
|
+
|
38
|
+
/**
|
39
|
+
* @typedef {Object} ChunkGroupDep
|
40
|
+
* @property {AsyncDependenciesBlock} block referencing block
|
41
|
+
* @property {ChunkGroup} chunkGroup referenced chunk group
|
42
|
+
*/
|
43
|
+
|
44
|
+
/**
|
45
|
+
* @template T
|
46
|
+
* @param {Set<T>} a first set
|
47
|
+
* @param {Set<T>} b second set
|
48
|
+
* @returns {number} cmp
|
49
|
+
*/
|
50
|
+
const bySetSize = (a, b) => {
|
51
|
+
return b.size - a.size;
|
52
|
+
};
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Extracts simplified info from the modules and their dependencies
|
56
|
+
* @param {Compilation} compilation the compilation
|
57
|
+
* @returns {Map<DependenciesBlock, { modules: Iterable<Module>, blocks: AsyncDependenciesBlock[]}>} the mapping block to modules and inner blocks
|
58
|
+
*/
|
59
|
+
const extraceBlockInfoMap = compilation => {
|
60
|
+
/** @type {Map<DependenciesBlock, { modules: Iterable<Module>, blocks: AsyncDependenciesBlock[]}>} */
|
61
|
+
const blockInfoMap = new Map();
|
62
|
+
|
63
|
+
/**
|
64
|
+
* @param {Dependency} d dependency to iterate over
|
65
|
+
* @returns {void}
|
66
|
+
*/
|
67
|
+
const iteratorDependency = d => {
|
68
|
+
// We skip Dependencies without Reference
|
69
|
+
const ref = compilation.getDependencyReference(currentModule, d);
|
70
|
+
if (!ref) {
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
// We skip Dependencies without Module pointer
|
74
|
+
const refModule = ref.module;
|
75
|
+
if (!refModule) {
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
// We skip weak Dependencies
|
79
|
+
if (ref.weak) {
|
80
|
+
return;
|
81
|
+
}
|
82
|
+
|
83
|
+
blockInfoModules.add(refModule);
|
84
|
+
};
|
85
|
+
|
86
|
+
/**
|
87
|
+
* @param {AsyncDependenciesBlock} b blocks to prepare
|
88
|
+
* @returns {void}
|
89
|
+
*/
|
90
|
+
const iteratorBlockPrepare = b => {
|
91
|
+
blockInfoBlocks.push(b);
|
92
|
+
blockQueue.push(b);
|
93
|
+
};
|
94
|
+
|
95
|
+
/** @type {Module} */
|
96
|
+
let currentModule;
|
97
|
+
/** @type {DependenciesBlock} */
|
98
|
+
let block;
|
99
|
+
/** @type {DependenciesBlock[]} */
|
100
|
+
let blockQueue;
|
101
|
+
/** @type {Set<Module>} */
|
102
|
+
let blockInfoModules;
|
103
|
+
/** @type {AsyncDependenciesBlock[]} */
|
104
|
+
let blockInfoBlocks;
|
105
|
+
|
106
|
+
for (const module of compilation.modules) {
|
107
|
+
blockQueue = [module];
|
108
|
+
currentModule = module;
|
109
|
+
while (blockQueue.length > 0) {
|
110
|
+
block = blockQueue.pop();
|
111
|
+
blockInfoModules = new Set();
|
112
|
+
blockInfoBlocks = [];
|
113
|
+
|
114
|
+
if (block.variables) {
|
115
|
+
for (const variable of block.variables) {
|
116
|
+
for (const dep of variable.dependencies) iteratorDependency(dep);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
if (block.dependencies) {
|
121
|
+
for (const dep of block.dependencies) iteratorDependency(dep);
|
122
|
+
}
|
123
|
+
|
124
|
+
if (block.blocks) {
|
125
|
+
for (const b of block.blocks) iteratorBlockPrepare(b);
|
126
|
+
}
|
127
|
+
|
128
|
+
const blockInfo = {
|
129
|
+
modules: blockInfoModules,
|
130
|
+
blocks: blockInfoBlocks
|
131
|
+
};
|
132
|
+
blockInfoMap.set(block, blockInfo);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
return blockInfoMap;
|
137
|
+
};
|
138
|
+
|
139
|
+
/**
|
140
|
+
*
|
141
|
+
* @param {Compilation} compilation the compilation
|
142
|
+
* @param {Entrypoint[]} inputChunkGroups input groups
|
143
|
+
* @param {Map<ChunkGroup, ChunkGroupInfo>} chunkGroupInfoMap mapping from chunk group to available modules
|
144
|
+
* @param {Map<ChunkGroup, ChunkGroupDep[]>} chunkDependencies dependencies for chunk groups
|
145
|
+
* @param {Set<DependenciesBlock>} blocksWithNestedBlocks flag for blocks that have nested blocks
|
146
|
+
* @param {Set<ChunkGroup>} allCreatedChunkGroups filled with all chunk groups that are created here
|
147
|
+
*/
|
148
|
+
const visitModules = (
|
149
|
+
compilation,
|
150
|
+
inputChunkGroups,
|
151
|
+
chunkGroupInfoMap,
|
152
|
+
chunkDependencies,
|
153
|
+
blocksWithNestedBlocks,
|
154
|
+
allCreatedChunkGroups
|
155
|
+
) => {
|
156
|
+
const logger = compilation.getLogger("webpack.buildChunkGraph.visitModules");
|
157
|
+
const { namedChunkGroups } = compilation;
|
158
|
+
|
159
|
+
logger.time("prepare");
|
160
|
+
const blockInfoMap = extraceBlockInfoMap(compilation);
|
161
|
+
|
162
|
+
/** @type {Map<ChunkGroup, { index: number, index2: number }>} */
|
163
|
+
const chunkGroupCounters = new Map();
|
164
|
+
for (const chunkGroup of inputChunkGroups) {
|
165
|
+
chunkGroupCounters.set(chunkGroup, {
|
166
|
+
index: 0,
|
167
|
+
index2: 0
|
168
|
+
});
|
169
|
+
}
|
170
|
+
|
171
|
+
let nextFreeModuleIndex = 0;
|
172
|
+
let nextFreeModuleIndex2 = 0;
|
173
|
+
|
174
|
+
/** @type {Map<DependenciesBlock, ChunkGroup>} */
|
175
|
+
const blockChunkGroups = new Map();
|
176
|
+
|
177
|
+
const ADD_AND_ENTER_MODULE = 0;
|
178
|
+
const ENTER_MODULE = 1;
|
179
|
+
const PROCESS_BLOCK = 2;
|
180
|
+
const LEAVE_MODULE = 3;
|
181
|
+
|
182
|
+
/**
|
183
|
+
* @param {QueueItem[]} queue the queue array (will be mutated)
|
184
|
+
* @param {ChunkGroup} chunkGroup chunk group
|
185
|
+
* @returns {QueueItem[]} the queue array again
|
186
|
+
*/
|
187
|
+
const reduceChunkGroupToQueueItem = (queue, chunkGroup) => {
|
188
|
+
for (const chunk of chunkGroup.chunks) {
|
189
|
+
const module = chunk.entryModule;
|
190
|
+
queue.push({
|
191
|
+
action: ENTER_MODULE,
|
192
|
+
block: module,
|
193
|
+
module,
|
194
|
+
chunk,
|
195
|
+
chunkGroup
|
196
|
+
});
|
197
|
+
}
|
198
|
+
chunkGroupInfoMap.set(chunkGroup, {
|
199
|
+
minAvailableModules: new Set(),
|
200
|
+
minAvailableModulesOwned: true,
|
201
|
+
availableModulesToBeMerged: [],
|
202
|
+
skippedItems: [],
|
203
|
+
resultingAvailableModules: undefined
|
204
|
+
});
|
205
|
+
return queue;
|
206
|
+
};
|
207
|
+
|
208
|
+
// Start with the provided modules/chunks
|
209
|
+
/** @type {QueueItem[]} */
|
210
|
+
let queue = inputChunkGroups
|
211
|
+
.reduce(reduceChunkGroupToQueueItem, [])
|
212
|
+
.reverse();
|
213
|
+
/** @type {Map<ChunkGroup, Set<ChunkGroup>>} */
|
214
|
+
const queueConnect = new Map();
|
215
|
+
/** @type {Set<ChunkGroupInfo>} */
|
216
|
+
const outdatedChunkGroupInfo = new Set();
|
217
|
+
/** @type {QueueItem[]} */
|
218
|
+
let queueDelayed = [];
|
219
|
+
|
220
|
+
logger.timeEnd("prepare");
|
221
|
+
|
222
|
+
/** @type {Module} */
|
223
|
+
let module;
|
224
|
+
/** @type {Chunk} */
|
225
|
+
let chunk;
|
226
|
+
/** @type {ChunkGroup} */
|
227
|
+
let chunkGroup;
|
228
|
+
/** @type {DependenciesBlock} */
|
229
|
+
let block;
|
230
|
+
/** @type {Set<Module>} */
|
231
|
+
let minAvailableModules;
|
232
|
+
/** @type {QueueItem[]} */
|
233
|
+
let skippedItems;
|
234
|
+
|
235
|
+
// For each async Block in graph
|
236
|
+
/**
|
237
|
+
* @param {AsyncDependenciesBlock} b iterating over each Async DepBlock
|
238
|
+
* @returns {void}
|
239
|
+
*/
|
240
|
+
const iteratorBlock = b => {
|
241
|
+
// 1. We create a chunk for this Block
|
242
|
+
// but only once (blockChunkGroups map)
|
243
|
+
let c = blockChunkGroups.get(b);
|
244
|
+
if (c === undefined) {
|
245
|
+
c = namedChunkGroups.get(b.chunkName);
|
246
|
+
if (c && c.isInitial()) {
|
247
|
+
compilation.errors.push(
|
248
|
+
new AsyncDependencyToInitialChunkError(b.chunkName, module, b.loc)
|
249
|
+
);
|
250
|
+
c = chunkGroup;
|
251
|
+
} else {
|
252
|
+
c = compilation.addChunkInGroup(
|
253
|
+
b.groupOptions || b.chunkName,
|
254
|
+
module,
|
255
|
+
b.loc,
|
256
|
+
b.request
|
257
|
+
);
|
258
|
+
chunkGroupCounters.set(c, { index: 0, index2: 0 });
|
259
|
+
blockChunkGroups.set(b, c);
|
260
|
+
allCreatedChunkGroups.add(c);
|
261
|
+
}
|
262
|
+
} else {
|
263
|
+
// TODO webpack 5 remove addOptions check
|
264
|
+
if (c.addOptions) c.addOptions(b.groupOptions);
|
265
|
+
c.addOrigin(module, b.loc, b.request);
|
266
|
+
}
|
267
|
+
|
268
|
+
// 2. We store the Block+Chunk mapping as dependency for the chunk
|
269
|
+
let deps = chunkDependencies.get(chunkGroup);
|
270
|
+
if (!deps) chunkDependencies.set(chunkGroup, (deps = []));
|
271
|
+
deps.push({
|
272
|
+
block: b,
|
273
|
+
chunkGroup: c
|
274
|
+
});
|
275
|
+
|
276
|
+
// 3. We create/update the chunk group info
|
277
|
+
let connectList = queueConnect.get(chunkGroup);
|
278
|
+
if (connectList === undefined) {
|
279
|
+
connectList = new Set();
|
280
|
+
queueConnect.set(chunkGroup, connectList);
|
281
|
+
}
|
282
|
+
connectList.add(c);
|
283
|
+
|
284
|
+
// 4. We enqueue the DependenciesBlock for traversal
|
285
|
+
queueDelayed.push({
|
286
|
+
action: PROCESS_BLOCK,
|
287
|
+
block: b,
|
288
|
+
module: module,
|
289
|
+
chunk: c.chunks[0],
|
290
|
+
chunkGroup: c
|
291
|
+
});
|
292
|
+
};
|
293
|
+
|
294
|
+
// Iterative traversal of the Module graph
|
295
|
+
// Recursive would be simpler to write but could result in Stack Overflows
|
296
|
+
while (queue.length) {
|
297
|
+
logger.time("visiting");
|
298
|
+
while (queue.length) {
|
299
|
+
const queueItem = queue.pop();
|
300
|
+
module = queueItem.module;
|
301
|
+
block = queueItem.block;
|
302
|
+
chunk = queueItem.chunk;
|
303
|
+
if (chunkGroup !== queueItem.chunkGroup) {
|
304
|
+
chunkGroup = queueItem.chunkGroup;
|
305
|
+
const chunkGroupInfo = chunkGroupInfoMap.get(chunkGroup);
|
306
|
+
minAvailableModules = chunkGroupInfo.minAvailableModules;
|
307
|
+
skippedItems = chunkGroupInfo.skippedItems;
|
308
|
+
}
|
309
|
+
|
310
|
+
switch (queueItem.action) {
|
311
|
+
case ADD_AND_ENTER_MODULE: {
|
312
|
+
if (minAvailableModules.has(module)) {
|
313
|
+
// already in parent chunks
|
314
|
+
// skip it for now, but enqueue for rechecking when minAvailableModules shrinks
|
315
|
+
skippedItems.push(queueItem);
|
316
|
+
break;
|
317
|
+
}
|
318
|
+
// We connect Module and Chunk when not already done
|
319
|
+
if (chunk.addModule(module)) {
|
320
|
+
module.addChunk(chunk);
|
321
|
+
} else {
|
322
|
+
// already connected, skip it
|
323
|
+
break;
|
324
|
+
}
|
325
|
+
}
|
326
|
+
// fallthrough
|
327
|
+
case ENTER_MODULE: {
|
328
|
+
if (chunkGroup !== undefined) {
|
329
|
+
const index = chunkGroup.getModuleIndex(module);
|
330
|
+
if (index === undefined) {
|
331
|
+
chunkGroup.setModuleIndex(
|
332
|
+
module,
|
333
|
+
chunkGroupCounters.get(chunkGroup).index++
|
334
|
+
);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
|
338
|
+
if (module.index === null) {
|
339
|
+
module.index = nextFreeModuleIndex++;
|
340
|
+
}
|
341
|
+
|
342
|
+
queue.push({
|
343
|
+
action: LEAVE_MODULE,
|
344
|
+
block,
|
345
|
+
module,
|
346
|
+
chunk,
|
347
|
+
chunkGroup
|
348
|
+
});
|
349
|
+
}
|
350
|
+
// fallthrough
|
351
|
+
case PROCESS_BLOCK: {
|
352
|
+
// get prepared block info
|
353
|
+
const blockInfo = blockInfoMap.get(block);
|
354
|
+
|
355
|
+
// Buffer items because order need to be reverse to get indicies correct
|
356
|
+
const skipBuffer = [];
|
357
|
+
const queueBuffer = [];
|
358
|
+
// Traverse all referenced modules
|
359
|
+
for (const refModule of blockInfo.modules) {
|
360
|
+
if (chunk.containsModule(refModule)) {
|
361
|
+
// skip early if already connected
|
362
|
+
continue;
|
363
|
+
}
|
364
|
+
if (minAvailableModules.has(refModule)) {
|
365
|
+
// already in parent chunks, skip it for now
|
366
|
+
skipBuffer.push({
|
367
|
+
action: ADD_AND_ENTER_MODULE,
|
368
|
+
block: refModule,
|
369
|
+
module: refModule,
|
370
|
+
chunk,
|
371
|
+
chunkGroup
|
372
|
+
});
|
373
|
+
continue;
|
374
|
+
}
|
375
|
+
// enqueue the add and enter to enter in the correct order
|
376
|
+
// this is relevant with circular dependencies
|
377
|
+
queueBuffer.push({
|
378
|
+
action: ADD_AND_ENTER_MODULE,
|
379
|
+
block: refModule,
|
380
|
+
module: refModule,
|
381
|
+
chunk,
|
382
|
+
chunkGroup
|
383
|
+
});
|
384
|
+
}
|
385
|
+
// Add buffered items in reversed order
|
386
|
+
for (let i = skipBuffer.length - 1; i >= 0; i--) {
|
387
|
+
skippedItems.push(skipBuffer[i]);
|
388
|
+
}
|
389
|
+
for (let i = queueBuffer.length - 1; i >= 0; i--) {
|
390
|
+
queue.push(queueBuffer[i]);
|
391
|
+
}
|
392
|
+
|
393
|
+
// Traverse all Blocks
|
394
|
+
for (const block of blockInfo.blocks) iteratorBlock(block);
|
395
|
+
|
396
|
+
if (blockInfo.blocks.length > 0 && module !== block) {
|
397
|
+
blocksWithNestedBlocks.add(block);
|
398
|
+
}
|
399
|
+
break;
|
400
|
+
}
|
401
|
+
case LEAVE_MODULE: {
|
402
|
+
if (chunkGroup !== undefined) {
|
403
|
+
const index = chunkGroup.getModuleIndex2(module);
|
404
|
+
if (index === undefined) {
|
405
|
+
chunkGroup.setModuleIndex2(
|
406
|
+
module,
|
407
|
+
chunkGroupCounters.get(chunkGroup).index2++
|
408
|
+
);
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
if (module.index2 === null) {
|
413
|
+
module.index2 = nextFreeModuleIndex2++;
|
414
|
+
}
|
415
|
+
break;
|
416
|
+
}
|
417
|
+
}
|
418
|
+
}
|
419
|
+
logger.timeEnd("visiting");
|
420
|
+
|
421
|
+
if (queueConnect.size > 0) {
|
422
|
+
logger.time("calculating available modules");
|
423
|
+
|
424
|
+
// Figure out new parents for chunk groups
|
425
|
+
// to get new available modules for these children
|
426
|
+
for (const [chunkGroup, targets] of queueConnect) {
|
427
|
+
const info = chunkGroupInfoMap.get(chunkGroup);
|
428
|
+
let minAvailableModules = info.minAvailableModules;
|
429
|
+
|
430
|
+
// 1. Create a new Set of available modules at this points
|
431
|
+
const resultingAvailableModules = new Set(minAvailableModules);
|
432
|
+
for (const chunk of chunkGroup.chunks) {
|
433
|
+
for (const m of chunk.modulesIterable) {
|
434
|
+
resultingAvailableModules.add(m);
|
435
|
+
}
|
436
|
+
}
|
437
|
+
info.resultingAvailableModules = resultingAvailableModules;
|
438
|
+
|
439
|
+
// 2. Update chunk group info
|
440
|
+
for (const target of targets) {
|
441
|
+
let chunkGroupInfo = chunkGroupInfoMap.get(target);
|
442
|
+
if (chunkGroupInfo === undefined) {
|
443
|
+
chunkGroupInfo = {
|
444
|
+
minAvailableModules: undefined,
|
445
|
+
minAvailableModulesOwned: undefined,
|
446
|
+
availableModulesToBeMerged: [],
|
447
|
+
skippedItems: [],
|
448
|
+
resultingAvailableModules: undefined
|
449
|
+
};
|
450
|
+
chunkGroupInfoMap.set(target, chunkGroupInfo);
|
451
|
+
}
|
452
|
+
chunkGroupInfo.availableModulesToBeMerged.push(
|
453
|
+
resultingAvailableModules
|
454
|
+
);
|
455
|
+
outdatedChunkGroupInfo.add(chunkGroupInfo);
|
456
|
+
}
|
457
|
+
}
|
458
|
+
queueConnect.clear();
|
459
|
+
logger.timeEnd("calculating available modules");
|
460
|
+
|
461
|
+
if (outdatedChunkGroupInfo.size > 0) {
|
462
|
+
logger.time("merging available modules");
|
463
|
+
// Execute the merge
|
464
|
+
for (const info of outdatedChunkGroupInfo) {
|
465
|
+
const availableModulesToBeMerged = info.availableModulesToBeMerged;
|
466
|
+
let minAvailableModules = info.minAvailableModules;
|
467
|
+
|
468
|
+
// 1. Get minimal available modules
|
469
|
+
// It doesn't make sense to traverse a chunk again with more available modules.
|
470
|
+
// This step calculates the minimal available modules and skips traversal when
|
471
|
+
// the list didn't shrink.
|
472
|
+
if (availableModulesToBeMerged.length > 1) {
|
473
|
+
availableModulesToBeMerged.sort(bySetSize);
|
474
|
+
}
|
475
|
+
let changed = false;
|
476
|
+
for (const availableModules of availableModulesToBeMerged) {
|
477
|
+
if (minAvailableModules === undefined) {
|
478
|
+
minAvailableModules = availableModules;
|
479
|
+
info.minAvailableModules = minAvailableModules;
|
480
|
+
info.minAvailableModulesOwned = false;
|
481
|
+
changed = true;
|
482
|
+
} else {
|
483
|
+
if (info.minAvailableModulesOwned) {
|
484
|
+
// We own it and can modify it
|
485
|
+
for (const m of minAvailableModules) {
|
486
|
+
if (!availableModules.has(m)) {
|
487
|
+
minAvailableModules.delete(m);
|
488
|
+
changed = true;
|
489
|
+
}
|
490
|
+
}
|
491
|
+
} else {
|
492
|
+
for (const m of minAvailableModules) {
|
493
|
+
if (!availableModules.has(m)) {
|
494
|
+
// minAvailableModules need to be modified
|
495
|
+
// but we don't own it
|
496
|
+
// construct a new Set as intersection of minAvailableModules and availableModules
|
497
|
+
/** @type {Set<Module>} */
|
498
|
+
const newSet = new Set();
|
499
|
+
const iterator = minAvailableModules[Symbol.iterator]();
|
500
|
+
/** @type {IteratorResult<Module>} */
|
501
|
+
let it;
|
502
|
+
while (!(it = iterator.next()).done) {
|
503
|
+
const module = it.value;
|
504
|
+
if (module === m) break;
|
505
|
+
newSet.add(module);
|
506
|
+
}
|
507
|
+
while (!(it = iterator.next()).done) {
|
508
|
+
const module = it.value;
|
509
|
+
if (availableModules.has(module)) {
|
510
|
+
newSet.add(module);
|
511
|
+
}
|
512
|
+
}
|
513
|
+
minAvailableModules = newSet;
|
514
|
+
info.minAvailableModulesOwned = true;
|
515
|
+
info.minAvailableModules = newSet;
|
516
|
+
changed = true;
|
517
|
+
break;
|
518
|
+
}
|
519
|
+
}
|
520
|
+
}
|
521
|
+
}
|
522
|
+
}
|
523
|
+
availableModulesToBeMerged.length = 0;
|
524
|
+
if (!changed) continue;
|
525
|
+
|
526
|
+
// 2. Reconsider skipped items
|
527
|
+
for (const queueItem of info.skippedItems) {
|
528
|
+
queue.push(queueItem);
|
529
|
+
}
|
530
|
+
info.skippedItems.length = 0;
|
531
|
+
}
|
532
|
+
outdatedChunkGroupInfo.clear();
|
533
|
+
logger.timeEnd("merging available modules");
|
534
|
+
}
|
535
|
+
}
|
536
|
+
|
537
|
+
// Run queueDelayed when all items of the queue are processed
|
538
|
+
// This is important to get the global indicing correct
|
539
|
+
// Async blocks should be processed after all sync blocks are processed
|
540
|
+
if (queue.length === 0) {
|
541
|
+
const tempQueue = queue;
|
542
|
+
queue = queueDelayed.reverse();
|
543
|
+
queueDelayed = tempQueue;
|
544
|
+
}
|
545
|
+
}
|
546
|
+
};
|
547
|
+
|
548
|
+
/**
|
549
|
+
*
|
550
|
+
* @param {Set<DependenciesBlock>} blocksWithNestedBlocks flag for blocks that have nested blocks
|
551
|
+
* @param {Map<ChunkGroup, ChunkGroupDep[]>} chunkDependencies dependencies for chunk groups
|
552
|
+
* @param {Map<ChunkGroup, ChunkGroupInfo>} chunkGroupInfoMap mapping from chunk group to available modules
|
553
|
+
*/
|
554
|
+
const connectChunkGroups = (
|
555
|
+
blocksWithNestedBlocks,
|
556
|
+
chunkDependencies,
|
557
|
+
chunkGroupInfoMap
|
558
|
+
) => {
|
559
|
+
/** @type {Set<Module>} */
|
560
|
+
let resultingAvailableModules;
|
561
|
+
|
562
|
+
/**
|
563
|
+
* Helper function to check if all modules of a chunk are available
|
564
|
+
*
|
565
|
+
* @param {ChunkGroup} chunkGroup the chunkGroup to scan
|
566
|
+
* @param {Set<Module>} availableModules the comparitor set
|
567
|
+
* @returns {boolean} return true if all modules of a chunk are available
|
568
|
+
*/
|
569
|
+
const areModulesAvailable = (chunkGroup, availableModules) => {
|
570
|
+
for (const chunk of chunkGroup.chunks) {
|
571
|
+
for (const module of chunk.modulesIterable) {
|
572
|
+
if (!availableModules.has(module)) return false;
|
573
|
+
}
|
574
|
+
}
|
575
|
+
return true;
|
576
|
+
};
|
577
|
+
|
578
|
+
// For each edge in the basic chunk graph
|
579
|
+
/**
|
580
|
+
* @param {ChunkGroupDep} dep the dependency used for filtering
|
581
|
+
* @returns {boolean} used to filter "edges" (aka Dependencies) that were pointing
|
582
|
+
* to modules that are already available. Also filters circular dependencies in the chunks graph
|
583
|
+
*/
|
584
|
+
const filterFn = dep => {
|
585
|
+
const depChunkGroup = dep.chunkGroup;
|
586
|
+
// TODO is this needed?
|
587
|
+
if (blocksWithNestedBlocks.has(dep.block)) return true;
|
588
|
+
if (areModulesAvailable(depChunkGroup, resultingAvailableModules)) {
|
589
|
+
return false; // break all modules are already available
|
590
|
+
}
|
591
|
+
return true;
|
592
|
+
};
|
593
|
+
|
594
|
+
// For all deps, check if chunk groups need to be connected
|
595
|
+
for (const [chunkGroup, deps] of chunkDependencies) {
|
596
|
+
if (deps.length === 0) continue;
|
597
|
+
|
598
|
+
// 1. Get info from chunk group info map
|
599
|
+
const info = chunkGroupInfoMap.get(chunkGroup);
|
600
|
+
resultingAvailableModules = info.resultingAvailableModules;
|
601
|
+
|
602
|
+
// 2. Foreach edge
|
603
|
+
for (let i = 0; i < deps.length; i++) {
|
604
|
+
const dep = deps[i];
|
605
|
+
|
606
|
+
// Filter inline, rather than creating a new array from `.filter()`
|
607
|
+
// TODO check if inlining filterFn makes sense here
|
608
|
+
if (!filterFn(dep)) {
|
609
|
+
continue;
|
610
|
+
}
|
611
|
+
const depChunkGroup = dep.chunkGroup;
|
612
|
+
const depBlock = dep.block;
|
613
|
+
|
614
|
+
// 5. Connect block with chunk
|
615
|
+
GraphHelpers.connectDependenciesBlockAndChunkGroup(
|
616
|
+
depBlock,
|
617
|
+
depChunkGroup
|
618
|
+
);
|
619
|
+
|
620
|
+
// 6. Connect chunk with parent
|
621
|
+
GraphHelpers.connectChunkGroupParentAndChild(chunkGroup, depChunkGroup);
|
622
|
+
}
|
623
|
+
}
|
624
|
+
};
|
625
|
+
|
626
|
+
/**
|
627
|
+
* Remove all unconnected chunk groups
|
628
|
+
* @param {Compilation} compilation the compilation
|
629
|
+
* @param {Iterable<ChunkGroup>} allCreatedChunkGroups all chunk groups that where created before
|
630
|
+
*/
|
631
|
+
const cleanupUnconnectedGroups = (compilation, allCreatedChunkGroups) => {
|
632
|
+
for (const chunkGroup of allCreatedChunkGroups) {
|
633
|
+
if (chunkGroup.getNumberOfParents() === 0) {
|
634
|
+
for (const chunk of chunkGroup.chunks) {
|
635
|
+
const idx = compilation.chunks.indexOf(chunk);
|
636
|
+
if (idx >= 0) compilation.chunks.splice(idx, 1);
|
637
|
+
chunk.remove("unconnected");
|
638
|
+
}
|
639
|
+
chunkGroup.remove("unconnected");
|
640
|
+
}
|
641
|
+
}
|
642
|
+
};
|
643
|
+
|
644
|
+
/**
|
645
|
+
* This method creates the Chunk graph from the Module graph
|
646
|
+
* @param {Compilation} compilation the compilation
|
647
|
+
* @param {Entrypoint[]} inputChunkGroups chunk groups which are processed
|
648
|
+
* @returns {void}
|
649
|
+
*/
|
650
|
+
const buildChunkGraph = (compilation, inputChunkGroups) => {
|
651
|
+
// SHARED STATE
|
652
|
+
|
653
|
+
/** @type {Map<ChunkGroup, ChunkGroupDep[]>} */
|
654
|
+
const chunkDependencies = new Map();
|
655
|
+
|
656
|
+
/** @type {Set<ChunkGroup>} */
|
657
|
+
const allCreatedChunkGroups = new Set();
|
658
|
+
|
659
|
+
/** @type {Map<ChunkGroup, ChunkGroupInfo>} */
|
660
|
+
const chunkGroupInfoMap = new Map();
|
661
|
+
|
662
|
+
/** @type {Set<DependenciesBlock>} */
|
663
|
+
const blocksWithNestedBlocks = new Set();
|
664
|
+
|
665
|
+
// PART ONE
|
666
|
+
|
667
|
+
visitModules(
|
668
|
+
compilation,
|
669
|
+
inputChunkGroups,
|
670
|
+
chunkGroupInfoMap,
|
671
|
+
chunkDependencies,
|
672
|
+
blocksWithNestedBlocks,
|
673
|
+
allCreatedChunkGroups
|
674
|
+
);
|
675
|
+
|
676
|
+
// PART TWO
|
677
|
+
|
678
|
+
connectChunkGroups(
|
679
|
+
blocksWithNestedBlocks,
|
680
|
+
chunkDependencies,
|
681
|
+
chunkGroupInfoMap
|
682
|
+
);
|
683
|
+
|
684
|
+
// Cleaup work
|
685
|
+
|
686
|
+
cleanupUnconnectedGroups(compilation, allCreatedChunkGroups);
|
687
|
+
};
|
688
|
+
|
689
|
+
module.exports = buildChunkGraph;
|