1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError");
|
9 | const { connectChunkGroupParentAndChild } = require("./GraphHelpers");
|
10 | const ModuleGraphConnection = require("./ModuleGraphConnection");
|
11 | const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const EMPTY_SET = (new Set());
|
62 | EMPTY_SET.plus = EMPTY_SET;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | const bySetSize = (a, b) => {
|
70 | return b.size + b.plus.size - a.size - a.plus.size;
|
71 | };
|
72 |
|
73 | const extractBlockModules = (module, moduleGraph, runtime, blockModulesMap) => {
|
74 | let blockCache;
|
75 | let modules;
|
76 |
|
77 | const arrays = [];
|
78 |
|
79 | const queue = [module];
|
80 | while (queue.length > 0) {
|
81 | const block = queue.pop();
|
82 | const arr = [];
|
83 | arrays.push(arr);
|
84 | blockModulesMap.set(block, arr);
|
85 | for (const b of block.blocks) {
|
86 | queue.push(b);
|
87 | }
|
88 | }
|
89 |
|
90 | for (const connection of moduleGraph.getOutgoingConnections(module)) {
|
91 | const d = connection.dependency;
|
92 |
|
93 | if (!d) continue;
|
94 | const m = connection.module;
|
95 |
|
96 | if (!m) continue;
|
97 |
|
98 | if (connection.weak) continue;
|
99 | const state = connection.getActiveState(runtime);
|
100 |
|
101 | if (state === false) continue;
|
102 |
|
103 | const block = moduleGraph.getParentBlock(d);
|
104 | let index = moduleGraph.getParentBlockIndex(d);
|
105 |
|
106 |
|
107 | if (index < 0) {
|
108 | index = block.dependencies.indexOf(d);
|
109 | }
|
110 |
|
111 | if (blockCache !== block) {
|
112 | modules = blockModulesMap.get((blockCache = block));
|
113 | }
|
114 |
|
115 | const i = index << 2;
|
116 | modules[i] = m;
|
117 | modules[i + 1] = state;
|
118 | }
|
119 |
|
120 | for (const modules of arrays) {
|
121 | if (modules.length === 0) continue;
|
122 | let indexMap;
|
123 | let length = 0;
|
124 | outer: for (let j = 0; j < modules.length; j += 2) {
|
125 | const m = modules[j];
|
126 | if (m === undefined) continue;
|
127 | const state = modules[j + 1];
|
128 | if (indexMap === undefined) {
|
129 | let i = 0;
|
130 | for (; i < length; i += 2) {
|
131 | if (modules[i] === m) {
|
132 | const merged = modules[i + 1];
|
133 | if (merged === true) continue outer;
|
134 | modules[i + 1] = ModuleGraphConnection.addConnectionStates(
|
135 | merged,
|
136 | state
|
137 | );
|
138 | }
|
139 | }
|
140 | modules[length] = m;
|
141 | length++;
|
142 | modules[length] = state;
|
143 | length++;
|
144 | if (length > 30) {
|
145 |
|
146 |
|
147 |
|
148 | indexMap = new Map();
|
149 | for (let i = 0; i < length; i += 2) {
|
150 | indexMap.set(modules[i], i + 1);
|
151 | }
|
152 | }
|
153 | } else {
|
154 | const idx = indexMap.get(m);
|
155 | if (idx !== undefined) {
|
156 | const merged = modules[idx];
|
157 | if (merged === true) continue outer;
|
158 | modules[idx] = ModuleGraphConnection.addConnectionStates(
|
159 | merged,
|
160 | state
|
161 | );
|
162 | } else {
|
163 | modules[length] = m;
|
164 | length++;
|
165 | modules[length] = state;
|
166 | indexMap.set(m, length);
|
167 | length++;
|
168 | }
|
169 | }
|
170 | }
|
171 | modules.length = length;
|
172 | }
|
173 | };
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | const visitModules = (
|
186 | logger,
|
187 | compilation,
|
188 | inputEntrypointsAndModules,
|
189 | chunkGroupInfoMap,
|
190 | blockConnections,
|
191 | blocksWithNestedBlocks,
|
192 | allCreatedChunkGroups
|
193 | ) => {
|
194 | const { moduleGraph, chunkGraph, moduleMemCaches } = compilation;
|
195 |
|
196 | const blockModulesRuntimeMap = new Map();
|
197 |
|
198 |
|
199 | let blockModulesMapRuntime = false;
|
200 | let blockModulesMap;
|
201 |
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | const getBlockModules = (block, runtime) => {
|
209 | if (blockModulesMapRuntime !== runtime) {
|
210 | blockModulesMap = blockModulesRuntimeMap.get(runtime);
|
211 | if (blockModulesMap === undefined) {
|
212 | blockModulesMap = new Map();
|
213 | blockModulesRuntimeMap.set(runtime, blockModulesMap);
|
214 | }
|
215 | }
|
216 | let blockModules = blockModulesMap.get(block);
|
217 | if (blockModules !== undefined) return blockModules;
|
218 | const module = (block.getRootBlock());
|
219 | const memCache = moduleMemCaches && moduleMemCaches.get(module);
|
220 | if (memCache !== undefined) {
|
221 | const map = memCache.provide(
|
222 | "bundleChunkGraph.blockModules",
|
223 | runtime,
|
224 | () => {
|
225 | logger.time("visitModules: prepare");
|
226 | const map = new Map();
|
227 | extractBlockModules(module, moduleGraph, runtime, map);
|
228 | logger.timeAggregate("visitModules: prepare");
|
229 | return map;
|
230 | }
|
231 | );
|
232 | for (const [block, blockModules] of map)
|
233 | blockModulesMap.set(block, blockModules);
|
234 | return map.get(block);
|
235 | } else {
|
236 | logger.time("visitModules: prepare");
|
237 | extractBlockModules(module, moduleGraph, runtime, blockModulesMap);
|
238 | blockModules = blockModulesMap.get(block);
|
239 | logger.timeAggregate("visitModules: prepare");
|
240 | return blockModules;
|
241 | }
|
242 | };
|
243 |
|
244 | let statProcessedQueueItems = 0;
|
245 | let statProcessedBlocks = 0;
|
246 | let statConnectedChunkGroups = 0;
|
247 | let statProcessedChunkGroupsForMerging = 0;
|
248 | let statMergedAvailableModuleSets = 0;
|
249 | let statForkedAvailableModules = 0;
|
250 | let statForkedAvailableModulesCount = 0;
|
251 | let statForkedAvailableModulesCountPlus = 0;
|
252 | let statForkedMergedModulesCount = 0;
|
253 | let statForkedMergedModulesCountPlus = 0;
|
254 | let statForkedResultModulesCount = 0;
|
255 | let statChunkGroupInfoUpdated = 0;
|
256 | let statChildChunkGroupsReconnected = 0;
|
257 |
|
258 | let nextChunkGroupIndex = 0;
|
259 | let nextFreeModulePreOrderIndex = 0;
|
260 | let nextFreeModulePostOrderIndex = 0;
|
261 |
|
262 |
|
263 | const blockChunkGroups = new Map();
|
264 |
|
265 |
|
266 | const namedChunkGroups = new Map();
|
267 |
|
268 |
|
269 | const namedAsyncEntrypoints = new Map();
|
270 |
|
271 | const ADD_AND_ENTER_ENTRY_MODULE = 0;
|
272 | const ADD_AND_ENTER_MODULE = 1;
|
273 | const ENTER_MODULE = 2;
|
274 | const PROCESS_BLOCK = 3;
|
275 | const PROCESS_ENTRY_BLOCK = 4;
|
276 | const LEAVE_MODULE = 5;
|
277 |
|
278 |
|
279 | let queue = [];
|
280 |
|
281 |
|
282 | const queueConnect = new Map();
|
283 |
|
284 | const chunkGroupsForCombining = new Set();
|
285 |
|
286 |
|
287 |
|
288 | for (const [chunkGroup, modules] of inputEntrypointsAndModules) {
|
289 | const runtime = getEntryRuntime(
|
290 | compilation,
|
291 | chunkGroup.name,
|
292 | chunkGroup.options
|
293 | );
|
294 |
|
295 | const chunkGroupInfo = {
|
296 | chunkGroup,
|
297 | runtime,
|
298 | minAvailableModules: undefined,
|
299 | minAvailableModulesOwned: false,
|
300 | availableModulesToBeMerged: [],
|
301 | skippedItems: undefined,
|
302 | resultingAvailableModules: undefined,
|
303 | children: undefined,
|
304 | availableSources: undefined,
|
305 | availableChildren: undefined,
|
306 | preOrderIndex: 0,
|
307 | postOrderIndex: 0
|
308 | };
|
309 | chunkGroup.index = nextChunkGroupIndex++;
|
310 | if (chunkGroup.getNumberOfParents() > 0) {
|
311 |
|
312 |
|
313 |
|
314 | const skippedItems = new Set();
|
315 | for (const module of modules) {
|
316 | skippedItems.add(module);
|
317 | }
|
318 | chunkGroupInfo.skippedItems = skippedItems;
|
319 | chunkGroupsForCombining.add(chunkGroupInfo);
|
320 | } else {
|
321 |
|
322 | chunkGroupInfo.minAvailableModules = EMPTY_SET;
|
323 | const chunk = chunkGroup.getEntrypointChunk();
|
324 | for (const module of modules) {
|
325 | queue.push({
|
326 | action: ADD_AND_ENTER_MODULE,
|
327 | block: module,
|
328 | module,
|
329 | chunk,
|
330 | chunkGroup,
|
331 | chunkGroupInfo
|
332 | });
|
333 | }
|
334 | }
|
335 | chunkGroupInfoMap.set(chunkGroup, chunkGroupInfo);
|
336 | if (chunkGroup.name) {
|
337 | namedChunkGroups.set(chunkGroup.name, chunkGroupInfo);
|
338 | }
|
339 | }
|
340 |
|
341 | for (const chunkGroupInfo of chunkGroupsForCombining) {
|
342 | const { chunkGroup } = chunkGroupInfo;
|
343 | chunkGroupInfo.availableSources = new Set();
|
344 | for (const parent of chunkGroup.parentsIterable) {
|
345 | const parentChunkGroupInfo = chunkGroupInfoMap.get(parent);
|
346 | chunkGroupInfo.availableSources.add(parentChunkGroupInfo);
|
347 | if (parentChunkGroupInfo.availableChildren === undefined) {
|
348 | parentChunkGroupInfo.availableChildren = new Set();
|
349 | }
|
350 | parentChunkGroupInfo.availableChildren.add(chunkGroupInfo);
|
351 | }
|
352 | }
|
353 |
|
354 |
|
355 |
|
356 | queue.reverse();
|
357 |
|
358 |
|
359 | const outdatedChunkGroupInfo = new Set();
|
360 |
|
361 | const chunkGroupsForMerging = new Set();
|
362 |
|
363 | let queueDelayed = [];
|
364 |
|
365 |
|
366 | const skipConnectionBuffer = [];
|
367 |
|
368 | const skipBuffer = [];
|
369 |
|
370 | const queueBuffer = [];
|
371 |
|
372 |
|
373 | let module;
|
374 |
|
375 | let chunk;
|
376 |
|
377 | let chunkGroup;
|
378 |
|
379 | let block;
|
380 |
|
381 | let chunkGroupInfo;
|
382 |
|
383 |
|
384 | |
385 |
|
386 |
|
387 |
|
388 | const iteratorBlock = b => {
|
389 |
|
390 |
|
391 | let cgi = blockChunkGroups.get(b);
|
392 |
|
393 | let c;
|
394 |
|
395 | let entrypoint;
|
396 | const entryOptions = b.groupOptions && b.groupOptions.entryOptions;
|
397 | if (cgi === undefined) {
|
398 | const chunkName = (b.groupOptions && b.groupOptions.name) || b.chunkName;
|
399 | if (entryOptions) {
|
400 | cgi = namedAsyncEntrypoints.get(chunkName);
|
401 | if (!cgi) {
|
402 | entrypoint = compilation.addAsyncEntrypoint(
|
403 | entryOptions,
|
404 | module,
|
405 | b.loc,
|
406 | b.request
|
407 | );
|
408 | entrypoint.index = nextChunkGroupIndex++;
|
409 | cgi = {
|
410 | chunkGroup: entrypoint,
|
411 | runtime: entrypoint.options.runtime || entrypoint.name,
|
412 | minAvailableModules: EMPTY_SET,
|
413 | minAvailableModulesOwned: false,
|
414 | availableModulesToBeMerged: [],
|
415 | skippedItems: undefined,
|
416 | resultingAvailableModules: undefined,
|
417 | children: undefined,
|
418 | availableSources: undefined,
|
419 | availableChildren: undefined,
|
420 | preOrderIndex: 0,
|
421 | postOrderIndex: 0
|
422 | };
|
423 | chunkGroupInfoMap.set(entrypoint, cgi);
|
424 |
|
425 | chunkGraph.connectBlockAndChunkGroup(b, entrypoint);
|
426 | if (chunkName) {
|
427 | namedAsyncEntrypoints.set(chunkName, cgi);
|
428 | }
|
429 | } else {
|
430 | entrypoint = (cgi.chunkGroup);
|
431 |
|
432 | entrypoint.addOrigin(module, b.loc, b.request);
|
433 | chunkGraph.connectBlockAndChunkGroup(b, entrypoint);
|
434 | }
|
435 |
|
436 |
|
437 | queueDelayed.push({
|
438 | action: PROCESS_ENTRY_BLOCK,
|
439 | block: b,
|
440 | module: module,
|
441 | chunk: entrypoint.chunks[0],
|
442 | chunkGroup: entrypoint,
|
443 | chunkGroupInfo: cgi
|
444 | });
|
445 | } else {
|
446 | cgi = namedChunkGroups.get(chunkName);
|
447 | if (!cgi) {
|
448 | c = compilation.addChunkInGroup(
|
449 | b.groupOptions || b.chunkName,
|
450 | module,
|
451 | b.loc,
|
452 | b.request
|
453 | );
|
454 | c.index = nextChunkGroupIndex++;
|
455 | cgi = {
|
456 | chunkGroup: c,
|
457 | runtime: chunkGroupInfo.runtime,
|
458 | minAvailableModules: undefined,
|
459 | minAvailableModulesOwned: undefined,
|
460 | availableModulesToBeMerged: [],
|
461 | skippedItems: undefined,
|
462 | resultingAvailableModules: undefined,
|
463 | children: undefined,
|
464 | availableSources: undefined,
|
465 | availableChildren: undefined,
|
466 | preOrderIndex: 0,
|
467 | postOrderIndex: 0
|
468 | };
|
469 | allCreatedChunkGroups.add(c);
|
470 | chunkGroupInfoMap.set(c, cgi);
|
471 | if (chunkName) {
|
472 | namedChunkGroups.set(chunkName, cgi);
|
473 | }
|
474 | } else {
|
475 | c = cgi.chunkGroup;
|
476 | if (c.isInitial()) {
|
477 | compilation.errors.push(
|
478 | new AsyncDependencyToInitialChunkError(chunkName, module, b.loc)
|
479 | );
|
480 | c = chunkGroup;
|
481 | }
|
482 | c.addOptions(b.groupOptions);
|
483 | c.addOrigin(module, b.loc, b.request);
|
484 | }
|
485 | blockConnections.set(b, []);
|
486 | }
|
487 | blockChunkGroups.set(b, cgi);
|
488 | } else if (entryOptions) {
|
489 | entrypoint = (cgi.chunkGroup);
|
490 | } else {
|
491 | c = cgi.chunkGroup;
|
492 | }
|
493 |
|
494 | if (c !== undefined) {
|
495 |
|
496 |
|
497 | blockConnections.get(b).push({
|
498 | originChunkGroupInfo: chunkGroupInfo,
|
499 | chunkGroup: c
|
500 | });
|
501 |
|
502 |
|
503 | let connectList = queueConnect.get(chunkGroupInfo);
|
504 | if (connectList === undefined) {
|
505 | connectList = new Set();
|
506 | queueConnect.set(chunkGroupInfo, connectList);
|
507 | }
|
508 | connectList.add(cgi);
|
509 |
|
510 |
|
511 |
|
512 |
|
513 | queueDelayed.push({
|
514 | action: PROCESS_BLOCK,
|
515 | block: b,
|
516 | module: module,
|
517 | chunk: c.chunks[0],
|
518 | chunkGroup: c,
|
519 | chunkGroupInfo: cgi
|
520 | });
|
521 | } else {
|
522 | chunkGroupInfo.chunkGroup.addAsyncEntrypoint(entrypoint);
|
523 | }
|
524 | };
|
525 |
|
526 | |
527 |
|
528 |
|
529 |
|
530 | const processBlock = block => {
|
531 | statProcessedBlocks++;
|
532 |
|
533 | const blockModules = getBlockModules(block, chunkGroupInfo.runtime);
|
534 |
|
535 | if (blockModules !== undefined) {
|
536 | const { minAvailableModules } = chunkGroupInfo;
|
537 |
|
538 |
|
539 | for (let i = 0; i < blockModules.length; i += 2) {
|
540 | const refModule = (blockModules[i]);
|
541 | if (chunkGraph.isModuleInChunk(refModule, chunk)) {
|
542 |
|
543 | continue;
|
544 | }
|
545 | const activeState = (
|
546 | blockModules[i + 1]
|
547 | );
|
548 | if (activeState !== true) {
|
549 | skipConnectionBuffer.push([refModule, activeState]);
|
550 | if (activeState === false) continue;
|
551 | }
|
552 | if (
|
553 | activeState === true &&
|
554 | (minAvailableModules.has(refModule) ||
|
555 | minAvailableModules.plus.has(refModule))
|
556 | ) {
|
557 |
|
558 | skipBuffer.push(refModule);
|
559 | continue;
|
560 | }
|
561 |
|
562 |
|
563 | queueBuffer.push({
|
564 | action: activeState === true ? ADD_AND_ENTER_MODULE : PROCESS_BLOCK,
|
565 | block: refModule,
|
566 | module: refModule,
|
567 | chunk,
|
568 | chunkGroup,
|
569 | chunkGroupInfo
|
570 | });
|
571 | }
|
572 |
|
573 | if (skipConnectionBuffer.length > 0) {
|
574 | let { skippedModuleConnections } = chunkGroupInfo;
|
575 | if (skippedModuleConnections === undefined) {
|
576 | chunkGroupInfo.skippedModuleConnections = skippedModuleConnections =
|
577 | new Set();
|
578 | }
|
579 | for (let i = skipConnectionBuffer.length - 1; i >= 0; i--) {
|
580 | skippedModuleConnections.add(skipConnectionBuffer[i]);
|
581 | }
|
582 | skipConnectionBuffer.length = 0;
|
583 | }
|
584 | if (skipBuffer.length > 0) {
|
585 | let { skippedItems } = chunkGroupInfo;
|
586 | if (skippedItems === undefined) {
|
587 | chunkGroupInfo.skippedItems = skippedItems = new Set();
|
588 | }
|
589 | for (let i = skipBuffer.length - 1; i >= 0; i--) {
|
590 | skippedItems.add(skipBuffer[i]);
|
591 | }
|
592 | skipBuffer.length = 0;
|
593 | }
|
594 | if (queueBuffer.length > 0) {
|
595 | for (let i = queueBuffer.length - 1; i >= 0; i--) {
|
596 | queue.push(queueBuffer[i]);
|
597 | }
|
598 | queueBuffer.length = 0;
|
599 | }
|
600 | }
|
601 |
|
602 |
|
603 | for (const b of block.blocks) {
|
604 | iteratorBlock(b);
|
605 | }
|
606 |
|
607 | if (block.blocks.length > 0 && module !== block) {
|
608 | blocksWithNestedBlocks.add(block);
|
609 | }
|
610 | };
|
611 |
|
612 | |
613 |
|
614 |
|
615 |
|
616 | const processEntryBlock = block => {
|
617 | statProcessedBlocks++;
|
618 |
|
619 | const blockModules = getBlockModules(block, chunkGroupInfo.runtime);
|
620 |
|
621 | if (blockModules !== undefined) {
|
622 |
|
623 | for (let i = 0; i < blockModules.length; i += 2) {
|
624 | const refModule = (blockModules[i]);
|
625 | const activeState = (
|
626 | blockModules[i + 1]
|
627 | );
|
628 |
|
629 |
|
630 | queueBuffer.push({
|
631 | action:
|
632 | activeState === true ? ADD_AND_ENTER_ENTRY_MODULE : PROCESS_BLOCK,
|
633 | block: refModule,
|
634 | module: refModule,
|
635 | chunk,
|
636 | chunkGroup,
|
637 | chunkGroupInfo
|
638 | });
|
639 | }
|
640 |
|
641 | if (queueBuffer.length > 0) {
|
642 | for (let i = queueBuffer.length - 1; i >= 0; i--) {
|
643 | queue.push(queueBuffer[i]);
|
644 | }
|
645 | queueBuffer.length = 0;
|
646 | }
|
647 | }
|
648 |
|
649 |
|
650 | for (const b of block.blocks) {
|
651 | iteratorBlock(b);
|
652 | }
|
653 |
|
654 | if (block.blocks.length > 0 && module !== block) {
|
655 | blocksWithNestedBlocks.add(block);
|
656 | }
|
657 | };
|
658 |
|
659 | const processQueue = () => {
|
660 | while (queue.length) {
|
661 | statProcessedQueueItems++;
|
662 | const queueItem = queue.pop();
|
663 | module = queueItem.module;
|
664 | block = queueItem.block;
|
665 | chunk = queueItem.chunk;
|
666 | chunkGroup = queueItem.chunkGroup;
|
667 | chunkGroupInfo = queueItem.chunkGroupInfo;
|
668 |
|
669 | switch (queueItem.action) {
|
670 | case ADD_AND_ENTER_ENTRY_MODULE:
|
671 | chunkGraph.connectChunkAndEntryModule(
|
672 | chunk,
|
673 | module,
|
674 | (chunkGroup)
|
675 | );
|
676 |
|
677 | case ADD_AND_ENTER_MODULE: {
|
678 | if (chunkGraph.isModuleInChunk(module, chunk)) {
|
679 |
|
680 | break;
|
681 | }
|
682 |
|
683 | chunkGraph.connectChunkAndModule(chunk, module);
|
684 | }
|
685 |
|
686 | case ENTER_MODULE: {
|
687 | const index = chunkGroup.getModulePreOrderIndex(module);
|
688 | if (index === undefined) {
|
689 | chunkGroup.setModulePreOrderIndex(
|
690 | module,
|
691 | chunkGroupInfo.preOrderIndex++
|
692 | );
|
693 | }
|
694 |
|
695 | if (
|
696 | moduleGraph.setPreOrderIndexIfUnset(
|
697 | module,
|
698 | nextFreeModulePreOrderIndex
|
699 | )
|
700 | ) {
|
701 | nextFreeModulePreOrderIndex++;
|
702 | }
|
703 |
|
704 |
|
705 | queueItem.action = LEAVE_MODULE;
|
706 | queue.push(queueItem);
|
707 | }
|
708 |
|
709 | case PROCESS_BLOCK: {
|
710 | processBlock(block);
|
711 | break;
|
712 | }
|
713 | case PROCESS_ENTRY_BLOCK: {
|
714 | processEntryBlock(block);
|
715 | break;
|
716 | }
|
717 | case LEAVE_MODULE: {
|
718 | const index = chunkGroup.getModulePostOrderIndex(module);
|
719 | if (index === undefined) {
|
720 | chunkGroup.setModulePostOrderIndex(
|
721 | module,
|
722 | chunkGroupInfo.postOrderIndex++
|
723 | );
|
724 | }
|
725 |
|
726 | if (
|
727 | moduleGraph.setPostOrderIndexIfUnset(
|
728 | module,
|
729 | nextFreeModulePostOrderIndex
|
730 | )
|
731 | ) {
|
732 | nextFreeModulePostOrderIndex++;
|
733 | }
|
734 | break;
|
735 | }
|
736 | }
|
737 | }
|
738 | };
|
739 |
|
740 | const calculateResultingAvailableModules = chunkGroupInfo => {
|
741 | if (chunkGroupInfo.resultingAvailableModules)
|
742 | return chunkGroupInfo.resultingAvailableModules;
|
743 |
|
744 | const minAvailableModules = chunkGroupInfo.minAvailableModules;
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 | let resultingAvailableModules;
|
754 | if (minAvailableModules.size > minAvailableModules.plus.size) {
|
755 |
|
756 | resultingAvailableModules =
|
757 | (new Set());
|
758 | for (const module of minAvailableModules.plus)
|
759 | minAvailableModules.add(module);
|
760 | minAvailableModules.plus = EMPTY_SET;
|
761 | resultingAvailableModules.plus = minAvailableModules;
|
762 | chunkGroupInfo.minAvailableModulesOwned = false;
|
763 | } else {
|
764 |
|
765 | resultingAvailableModules =
|
766 | (
|
767 | new Set(minAvailableModules)
|
768 | );
|
769 | resultingAvailableModules.plus = minAvailableModules.plus;
|
770 | }
|
771 |
|
772 |
|
773 | for (const chunk of chunkGroupInfo.chunkGroup.chunks) {
|
774 | for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
|
775 | resultingAvailableModules.add(m);
|
776 | }
|
777 | }
|
778 | return (chunkGroupInfo.resultingAvailableModules =
|
779 | resultingAvailableModules);
|
780 | };
|
781 |
|
782 | const processConnectQueue = () => {
|
783 |
|
784 |
|
785 | for (const [chunkGroupInfo, targets] of queueConnect) {
|
786 |
|
787 | if (chunkGroupInfo.children === undefined) {
|
788 | chunkGroupInfo.children = targets;
|
789 | } else {
|
790 | for (const target of targets) {
|
791 | chunkGroupInfo.children.add(target);
|
792 | }
|
793 | }
|
794 |
|
795 |
|
796 | const resultingAvailableModules =
|
797 | calculateResultingAvailableModules(chunkGroupInfo);
|
798 |
|
799 | const runtime = chunkGroupInfo.runtime;
|
800 |
|
801 |
|
802 | for (const target of targets) {
|
803 | target.availableModulesToBeMerged.push(resultingAvailableModules);
|
804 | chunkGroupsForMerging.add(target);
|
805 | const oldRuntime = target.runtime;
|
806 | const newRuntime = mergeRuntime(oldRuntime, runtime);
|
807 | if (oldRuntime !== newRuntime) {
|
808 | target.runtime = newRuntime;
|
809 | outdatedChunkGroupInfo.add(target);
|
810 | }
|
811 | }
|
812 |
|
813 | statConnectedChunkGroups += targets.size;
|
814 | }
|
815 | queueConnect.clear();
|
816 | };
|
817 |
|
818 | const processChunkGroupsForMerging = () => {
|
819 | statProcessedChunkGroupsForMerging += chunkGroupsForMerging.size;
|
820 |
|
821 |
|
822 | for (const info of chunkGroupsForMerging) {
|
823 | const availableModulesToBeMerged = info.availableModulesToBeMerged;
|
824 | let cachedMinAvailableModules = info.minAvailableModules;
|
825 |
|
826 | statMergedAvailableModuleSets += availableModulesToBeMerged.length;
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 | if (availableModulesToBeMerged.length > 1) {
|
833 | availableModulesToBeMerged.sort(bySetSize);
|
834 | }
|
835 | let changed = false;
|
836 | merge: for (const availableModules of availableModulesToBeMerged) {
|
837 | if (cachedMinAvailableModules === undefined) {
|
838 | cachedMinAvailableModules = availableModules;
|
839 | info.minAvailableModules = cachedMinAvailableModules;
|
840 | info.minAvailableModulesOwned = false;
|
841 | changed = true;
|
842 | } else {
|
843 | if (info.minAvailableModulesOwned) {
|
844 |
|
845 | if (cachedMinAvailableModules.plus === availableModules.plus) {
|
846 | for (const m of cachedMinAvailableModules) {
|
847 | if (!availableModules.has(m)) {
|
848 | cachedMinAvailableModules.delete(m);
|
849 | changed = true;
|
850 | }
|
851 | }
|
852 | } else {
|
853 | for (const m of cachedMinAvailableModules) {
|
854 | if (!availableModules.has(m) && !availableModules.plus.has(m)) {
|
855 | cachedMinAvailableModules.delete(m);
|
856 | changed = true;
|
857 | }
|
858 | }
|
859 | for (const m of cachedMinAvailableModules.plus) {
|
860 | if (!availableModules.has(m) && !availableModules.plus.has(m)) {
|
861 |
|
862 |
|
863 | const iterator =
|
864 | cachedMinAvailableModules.plus[Symbol.iterator]();
|
865 |
|
866 |
|
867 | let it;
|
868 | while (!(it = iterator.next()).done) {
|
869 | const module = it.value;
|
870 | if (module === m) break;
|
871 | cachedMinAvailableModules.add(module);
|
872 | }
|
873 |
|
874 | while (!(it = iterator.next()).done) {
|
875 | const module = it.value;
|
876 | if (
|
877 | availableModules.has(module) ||
|
878 | availableModules.plus.has(m)
|
879 | ) {
|
880 | cachedMinAvailableModules.add(module);
|
881 | }
|
882 | }
|
883 | cachedMinAvailableModules.plus = EMPTY_SET;
|
884 | changed = true;
|
885 | continue merge;
|
886 | }
|
887 | }
|
888 | }
|
889 | } else if (cachedMinAvailableModules.plus === availableModules.plus) {
|
890 |
|
891 |
|
892 | if (availableModules.size < cachedMinAvailableModules.size) {
|
893 |
|
894 |
|
895 | statForkedAvailableModules++;
|
896 | statForkedAvailableModulesCount += availableModules.size;
|
897 | statForkedMergedModulesCount += cachedMinAvailableModules.size;
|
898 |
|
899 | const newSet = (new Set());
|
900 | newSet.plus = availableModules.plus;
|
901 | for (const m of availableModules) {
|
902 | if (cachedMinAvailableModules.has(m)) {
|
903 | newSet.add(m);
|
904 | }
|
905 | }
|
906 | statForkedResultModulesCount += newSet.size;
|
907 | cachedMinAvailableModules = newSet;
|
908 | info.minAvailableModulesOwned = true;
|
909 | info.minAvailableModules = newSet;
|
910 | changed = true;
|
911 | continue merge;
|
912 | }
|
913 | for (const m of cachedMinAvailableModules) {
|
914 | if (!availableModules.has(m)) {
|
915 |
|
916 |
|
917 | statForkedAvailableModules++;
|
918 | statForkedAvailableModulesCount +=
|
919 | cachedMinAvailableModules.size;
|
920 | statForkedMergedModulesCount += availableModules.size;
|
921 |
|
922 |
|
923 | const newSet = (new Set());
|
924 | newSet.plus = availableModules.plus;
|
925 | const iterator = cachedMinAvailableModules[Symbol.iterator]();
|
926 |
|
927 |
|
928 | let it;
|
929 | while (!(it = iterator.next()).done) {
|
930 | const module = it.value;
|
931 | if (module === m) break;
|
932 | newSet.add(module);
|
933 | }
|
934 |
|
935 | while (!(it = iterator.next()).done) {
|
936 | const module = it.value;
|
937 | if (availableModules.has(module)) {
|
938 | newSet.add(module);
|
939 | }
|
940 | }
|
941 | statForkedResultModulesCount += newSet.size;
|
942 | cachedMinAvailableModules = newSet;
|
943 | info.minAvailableModulesOwned = true;
|
944 | info.minAvailableModules = newSet;
|
945 | changed = true;
|
946 | continue merge;
|
947 | }
|
948 | }
|
949 | } else {
|
950 | for (const m of cachedMinAvailableModules) {
|
951 | if (!availableModules.has(m) && !availableModules.plus.has(m)) {
|
952 |
|
953 |
|
954 | statForkedAvailableModules++;
|
955 | statForkedAvailableModulesCount +=
|
956 | cachedMinAvailableModules.size;
|
957 | statForkedAvailableModulesCountPlus +=
|
958 | cachedMinAvailableModules.plus.size;
|
959 | statForkedMergedModulesCount += availableModules.size;
|
960 | statForkedMergedModulesCountPlus += availableModules.plus.size;
|
961 |
|
962 | const newSet = (new Set());
|
963 | newSet.plus = EMPTY_SET;
|
964 | const iterator = cachedMinAvailableModules[Symbol.iterator]();
|
965 |
|
966 |
|
967 | let it;
|
968 | while (!(it = iterator.next()).done) {
|
969 | const module = it.value;
|
970 | if (module === m) break;
|
971 | newSet.add(module);
|
972 | }
|
973 |
|
974 | while (!(it = iterator.next()).done) {
|
975 | const module = it.value;
|
976 | if (
|
977 | availableModules.has(module) ||
|
978 | availableModules.plus.has(module)
|
979 | ) {
|
980 | newSet.add(module);
|
981 | }
|
982 | }
|
983 |
|
984 | for (const module of cachedMinAvailableModules.plus) {
|
985 | if (
|
986 | availableModules.has(module) ||
|
987 | availableModules.plus.has(module)
|
988 | ) {
|
989 | newSet.add(module);
|
990 | }
|
991 | }
|
992 | statForkedResultModulesCount += newSet.size;
|
993 | cachedMinAvailableModules = newSet;
|
994 | info.minAvailableModulesOwned = true;
|
995 | info.minAvailableModules = newSet;
|
996 | changed = true;
|
997 | continue merge;
|
998 | }
|
999 | }
|
1000 | for (const m of cachedMinAvailableModules.plus) {
|
1001 | if (!availableModules.has(m) && !availableModules.plus.has(m)) {
|
1002 |
|
1003 |
|
1004 | statForkedAvailableModules++;
|
1005 | statForkedAvailableModulesCount +=
|
1006 | cachedMinAvailableModules.size;
|
1007 | statForkedAvailableModulesCountPlus +=
|
1008 | cachedMinAvailableModules.plus.size;
|
1009 | statForkedMergedModulesCount += availableModules.size;
|
1010 | statForkedMergedModulesCountPlus += availableModules.plus.size;
|
1011 |
|
1012 |
|
1013 | const newSet = (
|
1014 | new Set(cachedMinAvailableModules)
|
1015 | );
|
1016 | newSet.plus = EMPTY_SET;
|
1017 | const iterator =
|
1018 | cachedMinAvailableModules.plus[Symbol.iterator]();
|
1019 |
|
1020 |
|
1021 | let it;
|
1022 | while (!(it = iterator.next()).done) {
|
1023 | const module = it.value;
|
1024 | if (module === m) break;
|
1025 | newSet.add(module);
|
1026 | }
|
1027 |
|
1028 | while (!(it = iterator.next()).done) {
|
1029 | const module = it.value;
|
1030 | if (
|
1031 | availableModules.has(module) ||
|
1032 | availableModules.plus.has(module)
|
1033 | ) {
|
1034 | newSet.add(module);
|
1035 | }
|
1036 | }
|
1037 | statForkedResultModulesCount += newSet.size;
|
1038 | cachedMinAvailableModules = newSet;
|
1039 | info.minAvailableModulesOwned = true;
|
1040 | info.minAvailableModules = newSet;
|
1041 | changed = true;
|
1042 | continue merge;
|
1043 | }
|
1044 | }
|
1045 | }
|
1046 | }
|
1047 | }
|
1048 | availableModulesToBeMerged.length = 0;
|
1049 | if (changed) {
|
1050 | info.resultingAvailableModules = undefined;
|
1051 | outdatedChunkGroupInfo.add(info);
|
1052 | }
|
1053 | }
|
1054 | chunkGroupsForMerging.clear();
|
1055 | };
|
1056 |
|
1057 | const processChunkGroupsForCombining = () => {
|
1058 | for (const info of chunkGroupsForCombining) {
|
1059 | for (const source of info.availableSources) {
|
1060 | if (!source.minAvailableModules) {
|
1061 | chunkGroupsForCombining.delete(info);
|
1062 | break;
|
1063 | }
|
1064 | }
|
1065 | }
|
1066 | for (const info of chunkGroupsForCombining) {
|
1067 | const availableModules = (new Set());
|
1068 | availableModules.plus = EMPTY_SET;
|
1069 | const mergeSet = set => {
|
1070 | if (set.size > availableModules.plus.size) {
|
1071 | for (const item of availableModules.plus) availableModules.add(item);
|
1072 | availableModules.plus = set;
|
1073 | } else {
|
1074 | for (const item of set) availableModules.add(item);
|
1075 | }
|
1076 | };
|
1077 | // combine minAvailableModules from all resultingAvailableModules
|
1078 | for (const source of info.availableSources) {
|
1079 | const resultingAvailableModules =
|
1080 | calculateResultingAvailableModules(source);
|
1081 | mergeSet(resultingAvailableModules);
|
1082 | mergeSet(resultingAvailableModules.plus);
|
1083 | }
|
1084 | info.minAvailableModules = availableModules;
|
1085 | info.minAvailableModulesOwned = false;
|
1086 | info.resultingAvailableModules = undefined;
|
1087 | outdatedChunkGroupInfo.add(info);
|
1088 | }
|
1089 | chunkGroupsForCombining.clear();
|
1090 | };
|
1091 |
|
1092 | const processOutdatedChunkGroupInfo = () => {
|
1093 | statChunkGroupInfoUpdated += outdatedChunkGroupInfo.size;
|
1094 |
|
1095 | for (const info of outdatedChunkGroupInfo) {
|
1096 |
|
1097 | if (info.skippedItems !== undefined) {
|
1098 | const { minAvailableModules } = info;
|
1099 | for (const module of info.skippedItems) {
|
1100 | if (
|
1101 | !minAvailableModules.has(module) &&
|
1102 | !minAvailableModules.plus.has(module)
|
1103 | ) {
|
1104 | queue.push({
|
1105 | action: ADD_AND_ENTER_MODULE,
|
1106 | block: module,
|
1107 | module,
|
1108 | chunk: info.chunkGroup.chunks[0],
|
1109 | chunkGroup: info.chunkGroup,
|
1110 | chunkGroupInfo: info
|
1111 | });
|
1112 | info.skippedItems.delete(module);
|
1113 | }
|
1114 | }
|
1115 | }
|
1116 |
|
1117 |
|
1118 | if (info.skippedModuleConnections !== undefined) {
|
1119 | const { minAvailableModules } = info;
|
1120 | for (const entry of info.skippedModuleConnections) {
|
1121 | const [module, activeState] = entry;
|
1122 | if (activeState === false) continue;
|
1123 | if (activeState === true) {
|
1124 | info.skippedModuleConnections.delete(entry);
|
1125 | }
|
1126 | if (
|
1127 | activeState === true &&
|
1128 | (minAvailableModules.has(module) ||
|
1129 | minAvailableModules.plus.has(module))
|
1130 | ) {
|
1131 | info.skippedItems.add(module);
|
1132 | continue;
|
1133 | }
|
1134 | queue.push({
|
1135 | action: activeState === true ? ADD_AND_ENTER_MODULE : PROCESS_BLOCK,
|
1136 | block: module,
|
1137 | module,
|
1138 | chunk: info.chunkGroup.chunks[0],
|
1139 | chunkGroup: info.chunkGroup,
|
1140 | chunkGroupInfo: info
|
1141 | });
|
1142 | }
|
1143 | }
|
1144 |
|
1145 |
|
1146 | if (info.children !== undefined) {
|
1147 | statChildChunkGroupsReconnected += info.children.size;
|
1148 | for (const cgi of info.children) {
|
1149 | let connectList = queueConnect.get(info);
|
1150 | if (connectList === undefined) {
|
1151 | connectList = new Set();
|
1152 | queueConnect.set(info, connectList);
|
1153 | }
|
1154 | connectList.add(cgi);
|
1155 | }
|
1156 | }
|
1157 |
|
1158 |
|
1159 | if (info.availableChildren !== undefined) {
|
1160 | for (const cgi of info.availableChildren) {
|
1161 | chunkGroupsForCombining.add(cgi);
|
1162 | }
|
1163 | }
|
1164 | }
|
1165 | outdatedChunkGroupInfo.clear();
|
1166 | };
|
1167 |
|
1168 |
|
1169 |
|
1170 | while (queue.length || queueConnect.size) {
|
1171 | logger.time("visitModules: visiting");
|
1172 | processQueue();
|
1173 | logger.timeAggregateEnd("visitModules: prepare");
|
1174 | logger.timeEnd("visitModules: visiting");
|
1175 |
|
1176 | if (chunkGroupsForCombining.size > 0) {
|
1177 | logger.time("visitModules: combine available modules");
|
1178 | processChunkGroupsForCombining();
|
1179 | logger.timeEnd("visitModules: combine available modules");
|
1180 | }
|
1181 |
|
1182 | if (queueConnect.size > 0) {
|
1183 | logger.time("visitModules: calculating available modules");
|
1184 | processConnectQueue();
|
1185 | logger.timeEnd("visitModules: calculating available modules");
|
1186 |
|
1187 | if (chunkGroupsForMerging.size > 0) {
|
1188 | logger.time("visitModules: merging available modules");
|
1189 | processChunkGroupsForMerging();
|
1190 | logger.timeEnd("visitModules: merging available modules");
|
1191 | }
|
1192 | }
|
1193 |
|
1194 | if (outdatedChunkGroupInfo.size > 0) {
|
1195 | logger.time("visitModules: check modules for revisit");
|
1196 | processOutdatedChunkGroupInfo();
|
1197 | logger.timeEnd("visitModules: check modules for revisit");
|
1198 | }
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 | if (queue.length === 0) {
|
1204 | const tempQueue = queue;
|
1205 | queue = queueDelayed.reverse();
|
1206 | queueDelayed = tempQueue;
|
1207 | }
|
1208 | }
|
1209 |
|
1210 | logger.log(
|
1211 | `${statProcessedQueueItems} queue items processed (${statProcessedBlocks} blocks)`
|
1212 | );
|
1213 | logger.log(`${statConnectedChunkGroups} chunk groups connected`);
|
1214 | logger.log(
|
1215 | `${statProcessedChunkGroupsForMerging} chunk groups processed for merging (${statMergedAvailableModuleSets} module sets, ${statForkedAvailableModules} forked, ${statForkedAvailableModulesCount} + ${statForkedAvailableModulesCountPlus} modules forked, ${statForkedMergedModulesCount} + ${statForkedMergedModulesCountPlus} modules merged into fork, ${statForkedResultModulesCount} resulting modules)`
|
1216 | );
|
1217 | logger.log(
|
1218 | `${statChunkGroupInfoUpdated} chunk group info updated (${statChildChunkGroupsReconnected} already connected chunk groups reconnected)`
|
1219 | );
|
1220 | };
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 | const connectChunkGroups = (
|
1230 | compilation,
|
1231 | blocksWithNestedBlocks,
|
1232 | blockConnections,
|
1233 | chunkGroupInfoMap
|
1234 | ) => {
|
1235 | const { chunkGraph } = compilation;
|
1236 |
|
1237 | |
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 |
|
1243 |
|
1244 | const areModulesAvailable = (chunkGroup, availableModules) => {
|
1245 | for (const chunk of chunkGroup.chunks) {
|
1246 | for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
|
1247 | if (!availableModules.has(module) && !availableModules.plus.has(module))
|
1248 | return false;
|
1249 | }
|
1250 | }
|
1251 | return true;
|
1252 | };
|
1253 |
|
1254 |
|
1255 | for (const [block, connections] of blockConnections) {
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 | if (
|
1263 |
|
1264 | !blocksWithNestedBlocks.has(block) &&
|
1265 | connections.every(({ chunkGroup, originChunkGroupInfo }) =>
|
1266 | areModulesAvailable(
|
1267 | chunkGroup,
|
1268 | originChunkGroupInfo.resultingAvailableModules
|
1269 | )
|
1270 | )
|
1271 | ) {
|
1272 | continue;
|
1273 | }
|
1274 |
|
1275 |
|
1276 | for (let i = 0; i < connections.length; i++) {
|
1277 | const { chunkGroup, originChunkGroupInfo } = connections[i];
|
1278 |
|
1279 |
|
1280 | chunkGraph.connectBlockAndChunkGroup(block, chunkGroup);
|
1281 |
|
1282 |
|
1283 | connectChunkGroupParentAndChild(
|
1284 | originChunkGroupInfo.chunkGroup,
|
1285 | chunkGroup
|
1286 | );
|
1287 | }
|
1288 | }
|
1289 | };
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 | const cleanupUnconnectedGroups = (compilation, allCreatedChunkGroups) => {
|
1297 | const { chunkGraph } = compilation;
|
1298 |
|
1299 | for (const chunkGroup of allCreatedChunkGroups) {
|
1300 | if (chunkGroup.getNumberOfParents() === 0) {
|
1301 | for (const chunk of chunkGroup.chunks) {
|
1302 | compilation.chunks.delete(chunk);
|
1303 | chunkGraph.disconnectChunk(chunk);
|
1304 | }
|
1305 | chunkGraph.disconnectChunkGroup(chunkGroup);
|
1306 | chunkGroup.remove();
|
1307 | }
|
1308 | }
|
1309 | };
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 | const buildChunkGraph = (compilation, inputEntrypointsAndModules) => {
|
1318 | const logger = compilation.getLogger("webpack.buildChunkGraph");
|
1319 |
|
1320 |
|
1321 |
|
1322 |
|
1323 | const blockConnections = new Map();
|
1324 |
|
1325 |
|
1326 | const allCreatedChunkGroups = new Set();
|
1327 |
|
1328 |
|
1329 | const chunkGroupInfoMap = new Map();
|
1330 |
|
1331 |
|
1332 | const blocksWithNestedBlocks = new Set();
|
1333 |
|
1334 |
|
1335 |
|
1336 | logger.time("visitModules");
|
1337 | visitModules(
|
1338 | logger,
|
1339 | compilation,
|
1340 | inputEntrypointsAndModules,
|
1341 | chunkGroupInfoMap,
|
1342 | blockConnections,
|
1343 | blocksWithNestedBlocks,
|
1344 | allCreatedChunkGroups
|
1345 | );
|
1346 | logger.timeEnd("visitModules");
|
1347 |
|
1348 |
|
1349 |
|
1350 | logger.time("connectChunkGroups");
|
1351 | connectChunkGroups(
|
1352 | compilation,
|
1353 | blocksWithNestedBlocks,
|
1354 | blockConnections,
|
1355 | chunkGroupInfoMap
|
1356 | );
|
1357 | logger.timeEnd("connectChunkGroups");
|
1358 |
|
1359 | for (const [chunkGroup, chunkGroupInfo] of chunkGroupInfoMap) {
|
1360 | for (const chunk of chunkGroup.chunks)
|
1361 | chunk.runtime = mergeRuntime(chunk.runtime, chunkGroupInfo.runtime);
|
1362 | }
|
1363 |
|
1364 |
|
1365 |
|
1366 | logger.time("cleanup");
|
1367 | cleanupUnconnectedGroups(compilation, allCreatedChunkGroups);
|
1368 | logger.timeEnd("cleanup");
|
1369 | };
|
1370 |
|
1371 | module.exports = buildChunkGraph;
|