@@ -9,6 +9,7 @@ const SortableSet = require("../util/SortableSet"); |
9 | 9 | const GraphHelpers = require("../GraphHelpers"); |
10 | 10 | const { isSubset } = require("../util/SetHelpers"); |
11 | 11 | const deterministicGrouping = require("../util/deterministicGrouping"); |
| 12 | +const MinMaxSizeWarning = require("./MinMaxSizeWarning"); |
12 | 13 | const contextify = require("../util/identifier").contextify; |
13 | 14 |
|
14 | 15 | /** @typedef {import("../Compiler")} Compiler */ |
@@ -96,6 +97,8 @@ const compareEntries = (a, b) => { |
96 | 97 | } |
97 | 98 | }; |
98 | 99 |
|
| 100 | +const compareNumbers = (a, b) => a - b; |
| 101 | + |
99 | 102 | const INITIAL_CHUNK_FILTER = chunk => chunk.canBeInitial(); |
100 | 103 | const ASYNC_CHUNK_FILTER = chunk => !chunk.canBeInitial(); |
101 | 104 | const ALL_CHUNK_FILTER = chunk => true; |
@@ -340,7 +343,7 @@ module.exports = class SplitChunksPlugin { |
340 | 343 | } |
341 | 344 | const getKey = chunks => { |
342 | 345 | return Array.from(chunks, c => indexMap.get(c)) |
343 | | - .sort() |
| 346 | + .sort(compareNumbers) |
344 | 347 | .join(); |
345 | 348 | }; |
346 | 349 | /** @type {Map<string, Set<Chunk>>} */ |
@@ -436,6 +439,7 @@ module.exports = class SplitChunksPlugin { |
436 | 439 | * @property {SortableSet} modules |
437 | 440 | * @property {TODO} cacheGroup |
438 | 441 | * @property {string} name |
| 442 | + * @property {boolean} validateSize |
439 | 443 | * @property {number} size |
440 | 444 | * @property {Set<Chunk>} chunks |
441 | 445 | * @property {Set<Chunk>} reuseableChunks |
@@ -461,7 +465,11 @@ module.exports = class SplitChunksPlugin { |
461 | 465 | module |
462 | 466 | ) => { |
463 | 467 | // Break if minimum number of chunks is not reached |
464 | | - if (selectedChunks.length < cacheGroup.minChunks) return; |
| 468 | + if ( |
| 469 | + !cacheGroup.enforce && |
| 470 | + selectedChunks.length < cacheGroup.minChunks |
| 471 | + ) |
| 472 | + return; |
465 | 473 | // Determine name for split chunk |
466 | 474 | const name = cacheGroup.getName( |
467 | 475 | module, |
@@ -473,8 +481,8 @@ module.exports = class SplitChunksPlugin { |
473 | 481 | // Elsewise we create the key from chunks and cache group key |
474 | 482 | // This automatically merges equal names |
475 | 483 | const key = |
476 | | - (name && `name:${name}`) || |
477 | | - `chunks:${selectedChunksKey} key:${cacheGroup.key}`; |
| 484 | + cacheGroup.key + |
| 485 | + (name ? ` name:${name}` : ` chunks:${selectedChunksKey}`); |
478 | 486 | // Add module to maps |
479 | 487 | let info = chunksInfoMap.get(key); |
480 | 488 | if (info === undefined) { |
@@ -484,21 +492,18 @@ module.exports = class SplitChunksPlugin { |
484 | 492 | modules: new SortableSet(undefined, sortByIdentifier), |
485 | 493 | cacheGroup, |
486 | 494 | name, |
| 495 | + validateSize: !cacheGroup.enforce && cacheGroup.minSize > 0, |
487 | 496 | size: 0, |
488 | 497 | chunks: new Set(), |
489 | 498 | reuseableChunks: new Set(), |
490 | 499 | chunksKeys: new Set() |
491 | 500 | }) |
492 | 501 | ); |
493 | | - } else { |
494 | | - if (info.cacheGroup !== cacheGroup) { |
495 | | - if (info.cacheGroup.priority < cacheGroup.priority) { |
496 | | - info.cacheGroup = cacheGroup; |
497 | | - } |
498 | | - } |
499 | 502 | } |
500 | 503 | info.modules.add(module); |
501 | | - info.size += module.size(); |
| 504 | + if (info.validateSize) { |
| 505 | + info.size += module.size(); |
| 506 | + } |
502 | 507 | if (!info.chunksKeys.has(selectedChunksKey)) { |
503 | 508 | info.chunksKeys.add(selectedChunksKey); |
504 | 509 | for (const chunk of selectedChunks) { |
@@ -529,36 +534,27 @@ module.exports = class SplitChunksPlugin { |
529 | 534 | priority: cacheGroupSource.priority || 0, |
530 | 535 | chunksFilter: |
531 | 536 | cacheGroupSource.chunksFilter || this.options.chunksFilter, |
| 537 | + enforce: cacheGroupSource.enforce, |
532 | 538 | minSize: |
533 | 539 | cacheGroupSource.minSize !== undefined |
534 | 540 | ? cacheGroupSource.minSize |
535 | | - : cacheGroupSource.enforce |
536 | | - ? 0 |
537 | | - : this.options.minSize, |
| 541 | + : this.options.minSize, |
538 | 542 | maxSize: |
539 | 543 | cacheGroupSource.maxSize !== undefined |
540 | 544 | ? cacheGroupSource.maxSize |
541 | | - : cacheGroupSource.enforce |
542 | | - ? 0 |
543 | | - : this.options.maxSize, |
| 545 | + : this.options.maxSize, |
544 | 546 | minChunks: |
545 | 547 | cacheGroupSource.minChunks !== undefined |
546 | 548 | ? cacheGroupSource.minChunks |
547 | | - : cacheGroupSource.enforce |
548 | | - ? 1 |
549 | | - : this.options.minChunks, |
| 549 | + : this.options.minChunks, |
550 | 550 | maxAsyncRequests: |
551 | 551 | cacheGroupSource.maxAsyncRequests !== undefined |
552 | 552 | ? cacheGroupSource.maxAsyncRequests |
553 | | - : cacheGroupSource.enforce |
554 | | - ? Infinity |
555 | | - : this.options.maxAsyncRequests, |
| 553 | + : this.options.maxAsyncRequests, |
556 | 554 | maxInitialRequests: |
557 | 555 | cacheGroupSource.maxInitialRequests !== undefined |
558 | 556 | ? cacheGroupSource.maxInitialRequests |
559 | | - : cacheGroupSource.enforce |
560 | | - ? Infinity |
561 | | - : this.options.maxInitialRequests, |
| 557 | + : this.options.maxInitialRequests, |
562 | 558 | getName: |
563 | 559 | cacheGroupSource.getName !== undefined |
564 | 560 | ? cacheGroupSource.getName |
@@ -576,7 +572,11 @@ module.exports = class SplitChunksPlugin { |
576 | 572 | // For all combination of chunk selection |
577 | 573 | for (const chunkCombination of combs) { |
578 | 574 | // Break if minimum number of chunks is not reached |
579 | | - if (chunkCombination.size < cacheGroup.minChunks) continue; |
| 575 | + if ( |
| 576 | + !cacheGroup.enforce && |
| 577 | + chunkCombination.size < cacheGroup.minChunks |
| 578 | + ) |
| 579 | + continue; |
580 | 580 | // Select chunks by configuration |
581 | 581 | const { |
582 | 582 | chunks: selectedChunks, |
@@ -586,17 +586,27 @@ module.exports = class SplitChunksPlugin { |
586 | 586 | cacheGroup.chunksFilter |
587 | 587 | ); |
588 | 588 |
|
589 | | - addModuleToChunksInfoMap( |
590 | | - cacheGroup, |
591 | | - selectedChunks, |
592 | | - selectedChunksKey, |
593 | | - module |
594 | | - ); |
| 589 | + if (selectedChunks.length > 0) { |
| 590 | + addModuleToChunksInfoMap( |
| 591 | + cacheGroup, |
| 592 | + selectedChunks, |
| 593 | + selectedChunksKey, |
| 594 | + module |
| 595 | + ); |
| 596 | + } |
595 | 597 | } |
596 | 598 | } |
597 | 599 | } |
598 | 600 |
|
599 | | - /** @type {Map<Chunk, {minSize: number, maxSize: number, automaticNameDelimiter: string}>} */ |
| 601 | + // Filter items were size < minSize |
| 602 | + for (const pair of chunksInfoMap) { |
| 603 | + const info = pair[1]; |
| 604 | + if (info.validateSize && info.size < info.cacheGroup.minSize) { |
| 605 | + chunksInfoMap.delete(pair[0]); |
| 606 | + } |
| 607 | + } |
| 608 | + |
| 609 | + /** @type {Map<Chunk, {minSize: number, maxSize: number, automaticNameDelimiter: string, keys: string[]}>} */ |
600 | 610 | const maxSizeQueueMap = new Map(); |
601 | 611 |
|
602 | 612 | while (chunksInfoMap.size > 0) { |
@@ -606,20 +616,15 @@ module.exports = class SplitChunksPlugin { |
606 | 616 | for (const pair of chunksInfoMap) { |
607 | 617 | const key = pair[0]; |
608 | 618 | const info = pair[1]; |
609 | | - if (info.size >= info.cacheGroup.minSize) { |
610 | | - if (bestEntry === undefined) { |
611 | | - bestEntry = info; |
612 | | - bestEntryKey = key; |
613 | | - } else if (compareEntries(bestEntry, info) < 0) { |
614 | | - bestEntry = info; |
615 | | - bestEntryKey = key; |
616 | | - } |
| 619 | + if (bestEntry === undefined) { |
| 620 | + bestEntry = info; |
| 621 | + bestEntryKey = key; |
| 622 | + } else if (compareEntries(bestEntry, info) < 0) { |
| 623 | + bestEntry = info; |
| 624 | + bestEntryKey = key; |
617 | 625 | } |
618 | 626 | } |
619 | 627 |
|
620 | | - // No suitable item left |
621 | | - if (bestEntry === undefined) break; |
622 | | - |
623 | 628 | const item = bestEntry; |
624 | 629 | chunksInfoMap.delete(bestEntryKey); |
625 | 630 |
|
@@ -666,29 +671,41 @@ module.exports = class SplitChunksPlugin { |
666 | 671 | // Skip when no chunk selected |
667 | 672 | if (usedChunks.length === 0) continue; |
668 | 673 |
|
669 | | - const chunkInLimit = usedChunks.filter(chunk => { |
670 | | - // respect max requests when not enforced |
671 | | - const maxRequests = chunk.isOnlyInitial() |
672 | | - ? item.cacheGroup.maxInitialRequests |
673 | | - : chunk.canBeInitial() |
674 | | - ? Math.min( |
675 | | - item.cacheGroup.maxInitialRequests, |
676 | | - item.cacheGroup.maxAsyncRequests |
677 | | - ) |
678 | | - : item.cacheGroup.maxAsyncRequests; |
679 | | - return !isFinite(maxRequests) || getRequests(chunk) < maxRequests; |
680 | | - }); |
681 | | - |
682 | | - if (chunkInLimit.length < usedChunks.length) { |
683 | | - for (const module of item.modules) { |
684 | | - addModuleToChunksInfoMap( |
685 | | - item.cacheGroup, |
686 | | - chunkInLimit, |
687 | | - getKey(chunkInLimit), |
688 | | - module |
| 674 | + if ( |
| 675 | + !item.cacheGroup.enforce && |
| 676 | + (Number.isFinite(item.cacheGroup.maxInitialRequests) || |
| 677 | + Number.isFinite(item.cacheGroup.maxAsyncRequests)) |
| 678 | + ) { |
| 679 | + const chunkInLimit = usedChunks.filter(chunk => { |
| 680 | + // respect max requests when not enforced |
| 681 | + const maxRequests = chunk.isOnlyInitial() |
| 682 | + ? item.cacheGroup.maxInitialRequests |
| 683 | + : chunk.canBeInitial() |
| 684 | + ? Math.min( |
| 685 | + item.cacheGroup.maxInitialRequests, |
| 686 | + item.cacheGroup.maxAsyncRequests |
| 687 | + ) |
| 688 | + : item.cacheGroup.maxAsyncRequests; |
| 689 | + return ( |
| 690 | + !isFinite(maxRequests) || getRequests(chunk) < maxRequests |
689 | 691 | ); |
| 692 | + }); |
| 693 | + |
| 694 | + if (chunkInLimit.length < usedChunks.length) { |
| 695 | + // We do not need to check enforce here as it was |
| 696 | + // already checked above. |
| 697 | + if (chunkInLimit.length >= item.cacheGroup.minChunks) { |
| 698 | + for (const module of item.modules) { |
| 699 | + addModuleToChunksInfoMap( |
| 700 | + item.cacheGroup, |
| 701 | + chunkInLimit, |
| 702 | + getKey(chunkInLimit), |
| 703 | + module |
| 704 | + ); |
| 705 | + } |
| 706 | + } |
| 707 | + continue; |
690 | 708 | } |
691 | | - continue; |
692 | 709 | } |
693 | 710 |
|
694 | 711 | // Create the new chunk if not reusing one |
@@ -764,36 +781,62 @@ module.exports = class SplitChunksPlugin { |
764 | 781 | oldMaxSizeSettings ? oldMaxSizeSettings.maxSize : Infinity, |
765 | 782 | item.cacheGroup.maxSize |
766 | 783 | ), |
767 | | - automaticNameDelimiter: item.cacheGroup.automaticNameDelimiter |
| 784 | + automaticNameDelimiter: item.cacheGroup.automaticNameDelimiter, |
| 785 | + keys: oldMaxSizeSettings |
| 786 | + ? oldMaxSizeSettings.keys.concat(item.cacheGroup.key) |
| 787 | + : [item.cacheGroup.key] |
768 | 788 | }); |
769 | 789 | } |
770 | 790 |
|
771 | 791 | // remove all modules from other entries and update size |
772 | 792 | for (const [key, info] of chunksInfoMap) { |
773 | 793 | if (isOverlap(info.chunks, item.chunks)) { |
774 | | - const oldSize = info.modules.size; |
775 | | - for (const module of item.modules) { |
776 | | - info.modules.delete(module); |
777 | | - } |
778 | | - if (info.modules.size === 0) { |
779 | | - chunksInfoMap.delete(key); |
780 | | - continue; |
781 | | - } |
782 | | - if (info.modules.size !== oldSize) { |
783 | | - info.size = getModulesSize(info.modules); |
784 | | - if (info.size < info.cacheGroup.minSize) { |
| 794 | + if (info.validateSize) { |
| 795 | + // update modules and total size |
| 796 | + // may remove it from the map when < minSize |
| 797 | + const oldSize = info.modules.size; |
| 798 | + for (const module of item.modules) { |
| 799 | + info.modules.delete(module); |
| 800 | + } |
| 801 | + if (info.modules.size === 0) { |
| 802 | + chunksInfoMap.delete(key); |
| 803 | + continue; |
| 804 | + } |
| 805 | + if (info.modules.size !== oldSize) { |
| 806 | + info.size = getModulesSize(info.modules); |
| 807 | + if (info.size < info.cacheGroup.minSize) { |
| 808 | + chunksInfoMap.delete(key); |
| 809 | + } |
| 810 | + } |
| 811 | + } else { |
| 812 | + // only update the modules |
| 813 | + for (const module of item.modules) { |
| 814 | + info.modules.delete(module); |
| 815 | + } |
| 816 | + if (info.modules.size === 0) { |
785 | 817 | chunksInfoMap.delete(key); |
786 | 818 | } |
787 | 819 | } |
788 | 820 | } |
789 | 821 | } |
790 | 822 | } |
791 | 823 |
|
| 824 | + const incorrectMinMaxSizeSet = new Set(); |
| 825 | + |
792 | 826 | // Make sure that maxSize is fulfilled |
793 | 827 | for (const chunk of compilation.chunks.slice()) { |
794 | | - const { minSize, maxSize, automaticNameDelimiter } = |
| 828 | + const { minSize, maxSize, automaticNameDelimiter, keys } = |
795 | 829 | maxSizeQueueMap.get(chunk) || this.options.fallbackCacheGroup; |
796 | 830 | if (!maxSize) continue; |
| 831 | + if (minSize > maxSize) { |
| 832 | + const warningKey = `${keys && keys.join()} ${minSize} ${maxSize}`; |
| 833 | + if (!incorrectMinMaxSizeSet.has(warningKey)) { |
| 834 | + incorrectMinMaxSizeSet.add(warningKey); |
| 835 | + compilation.warnings.push( |
| 836 | + new MinMaxSizeWarning(keys, minSize, maxSize) |
| 837 | + ); |
| 838 | + } |
| 839 | + } |
797 | 840 | const results = deterministicGroupingForModules({ |
798 | 841 | maxSize: Math.max(minSize, maxSize), |
799 | 842 | minSize, |
|