GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — enforce doesn't affect minSize for maxSize · webpack/webpack@3b46b48

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/optimize
    • MinMaxSizeWarning.js
    • SplitChunksPlugin.js
  • test
    • __snapshots__
      • StatsTestCases.test.js.snap
    • statsCases/split-chunks-max-size
      • webpack.config.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/optimize/MinMaxSizeWarning.js‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const WebpackError = require("../WebpackError");
8+
const SizeFormatHelpers = require("../SizeFormatHelpers");
9+
10+
class MinMaxSizeWarning extends WebpackError {
11+
constructor(keys, minSize, maxSize) {
12+
let keysMessage = "Fallback cache group";
13+
if (keys) {
14+
keysMessage =
15+
keys.length > 1
16+
? `Cache groups ${keys.sort().join(", ")}`
17+
: `Cache group ${keys[0]}`;
18+
}
19+
super(
20+
`SplitChunksPlugin\n` +
21+
`${keysMessage}\n` +
22+
`Configured minSize (${SizeFormatHelpers.formatSize(minSize)}) is ` +
23+
`bigger than maxSize (${SizeFormatHelpers.formatSize(maxSize)}).\n` +
24+
"This seem to be a invalid optimiziation.splitChunks configuration."
25+
);
26+
}
27+
}
28+
29+
module.exports = MinMaxSizeWarning;
Collapse file

‎lib/optimize/SplitChunksPlugin.js‎

Copy file name to clipboardExpand all lines: lib/optimize/SplitChunksPlugin.js
+122-79Lines changed: 122 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const SortableSet = require("../util/SortableSet");
99
const GraphHelpers = require("../GraphHelpers");
1010
const { isSubset } = require("../util/SetHelpers");
1111
const deterministicGrouping = require("../util/deterministicGrouping");
12+
const MinMaxSizeWarning = require("./MinMaxSizeWarning");
1213
const contextify = require("../util/identifier").contextify;
1314

1415
/** @typedef {import("../Compiler")} Compiler */
@@ -96,6 +97,8 @@ const compareEntries = (a, b) => {
9697
}
9798
};
9899

100+
const compareNumbers = (a, b) => a - b;
101+
99102
const INITIAL_CHUNK_FILTER = chunk => chunk.canBeInitial();
100103
const ASYNC_CHUNK_FILTER = chunk => !chunk.canBeInitial();
101104
const ALL_CHUNK_FILTER = chunk => true;
@@ -340,7 +343,7 @@ module.exports = class SplitChunksPlugin {
340343
}
341344
const getKey = chunks => {
342345
return Array.from(chunks, c => indexMap.get(c))
343-
.sort()
346+
.sort(compareNumbers)
344347
.join();
345348
};
346349
/** @type {Map<string, Set<Chunk>>} */
@@ -436,6 +439,7 @@ module.exports = class SplitChunksPlugin {
436439
* @property {SortableSet} modules
437440
* @property {TODO} cacheGroup
438441
* @property {string} name
442+
* @property {boolean} validateSize
439443
* @property {number} size
440444
* @property {Set<Chunk>} chunks
441445
* @property {Set<Chunk>} reuseableChunks
@@ -461,7 +465,11 @@ module.exports = class SplitChunksPlugin {
461465
module
462466
) => {
463467
// 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;
465473
// Determine name for split chunk
466474
const name = cacheGroup.getName(
467475
module,
@@ -473,8 +481,8 @@ module.exports = class SplitChunksPlugin {
473481
// Elsewise we create the key from chunks and cache group key
474482
// This automatically merges equal names
475483
const key =
476-
(name && `name:${name}`) ||
477-
`chunks:${selectedChunksKey} key:${cacheGroup.key}`;
484+
cacheGroup.key +
485+
(name ? ` name:${name}` : ` chunks:${selectedChunksKey}`);
478486
// Add module to maps
479487
let info = chunksInfoMap.get(key);
480488
if (info === undefined) {
@@ -484,21 +492,18 @@ module.exports = class SplitChunksPlugin {
484492
modules: new SortableSet(undefined, sortByIdentifier),
485493
cacheGroup,
486494
name,
495+
validateSize: !cacheGroup.enforce && cacheGroup.minSize > 0,
487496
size: 0,
488497
chunks: new Set(),
489498
reuseableChunks: new Set(),
490499
chunksKeys: new Set()
491500
})
492501
);
493-
} else {
494-
if (info.cacheGroup !== cacheGroup) {
495-
if (info.cacheGroup.priority < cacheGroup.priority) {
496-
info.cacheGroup = cacheGroup;
497-
}
498-
}
499502
}
500503
info.modules.add(module);
501-
info.size += module.size();
504+
if (info.validateSize) {
505+
info.size += module.size();
506+
}
502507
if (!info.chunksKeys.has(selectedChunksKey)) {
503508
info.chunksKeys.add(selectedChunksKey);
504509
for (const chunk of selectedChunks) {
@@ -529,36 +534,27 @@ module.exports = class SplitChunksPlugin {
529534
priority: cacheGroupSource.priority || 0,
530535
chunksFilter:
531536
cacheGroupSource.chunksFilter || this.options.chunksFilter,
537+
enforce: cacheGroupSource.enforce,
532538
minSize:
533539
cacheGroupSource.minSize !== undefined
534540
? cacheGroupSource.minSize
535-
: cacheGroupSource.enforce
536-
? 0
537-
: this.options.minSize,
541+
: this.options.minSize,
538542
maxSize:
539543
cacheGroupSource.maxSize !== undefined
540544
? cacheGroupSource.maxSize
541-
: cacheGroupSource.enforce
542-
? 0
543-
: this.options.maxSize,
545+
: this.options.maxSize,
544546
minChunks:
545547
cacheGroupSource.minChunks !== undefined
546548
? cacheGroupSource.minChunks
547-
: cacheGroupSource.enforce
548-
? 1
549-
: this.options.minChunks,
549+
: this.options.minChunks,
550550
maxAsyncRequests:
551551
cacheGroupSource.maxAsyncRequests !== undefined
552552
? cacheGroupSource.maxAsyncRequests
553-
: cacheGroupSource.enforce
554-
? Infinity
555-
: this.options.maxAsyncRequests,
553+
: this.options.maxAsyncRequests,
556554
maxInitialRequests:
557555
cacheGroupSource.maxInitialRequests !== undefined
558556
? cacheGroupSource.maxInitialRequests
559-
: cacheGroupSource.enforce
560-
? Infinity
561-
: this.options.maxInitialRequests,
557+
: this.options.maxInitialRequests,
562558
getName:
563559
cacheGroupSource.getName !== undefined
564560
? cacheGroupSource.getName
@@ -576,7 +572,11 @@ module.exports = class SplitChunksPlugin {
576572
// For all combination of chunk selection
577573
for (const chunkCombination of combs) {
578574
// 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;
580580
// Select chunks by configuration
581581
const {
582582
chunks: selectedChunks,
@@ -586,17 +586,27 @@ module.exports = class SplitChunksPlugin {
586586
cacheGroup.chunksFilter
587587
);
588588

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+
}
595597
}
596598
}
597599
}
598600

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[]}>} */
600610
const maxSizeQueueMap = new Map();
601611

602612
while (chunksInfoMap.size > 0) {
@@ -606,20 +616,15 @@ module.exports = class SplitChunksPlugin {
606616
for (const pair of chunksInfoMap) {
607617
const key = pair[0];
608618
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;
617625
}
618626
}
619627

620-
// No suitable item left
621-
if (bestEntry === undefined) break;
622-
623628
const item = bestEntry;
624629
chunksInfoMap.delete(bestEntryKey);
625630

@@ -666,29 +671,41 @@ module.exports = class SplitChunksPlugin {
666671
// Skip when no chunk selected
667672
if (usedChunks.length === 0) continue;
668673

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
689691
);
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;
690708
}
691-
continue;
692709
}
693710

694711
// Create the new chunk if not reusing one
@@ -764,36 +781,62 @@ module.exports = class SplitChunksPlugin {
764781
oldMaxSizeSettings ? oldMaxSizeSettings.maxSize : Infinity,
765782
item.cacheGroup.maxSize
766783
),
767-
automaticNameDelimiter: item.cacheGroup.automaticNameDelimiter
784+
automaticNameDelimiter: item.cacheGroup.automaticNameDelimiter,
785+
keys: oldMaxSizeSettings
786+
? oldMaxSizeSettings.keys.concat(item.cacheGroup.key)
787+
: [item.cacheGroup.key]
768788
});
769789
}
770790

771791
// remove all modules from other entries and update size
772792
for (const [key, info] of chunksInfoMap) {
773793
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) {
785817
chunksInfoMap.delete(key);
786818
}
787819
}
788820
}
789821
}
790822
}
791823

824+
const incorrectMinMaxSizeSet = new Set();
825+
792826
// Make sure that maxSize is fulfilled
793827
for (const chunk of compilation.chunks.slice()) {
794-
const { minSize, maxSize, automaticNameDelimiter } =
828+
const { minSize, maxSize, automaticNameDelimiter, keys } =
795829
maxSizeQueueMap.get(chunk) || this.options.fallbackCacheGroup;
796830
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+
}
797840
const results = deterministicGroupingForModules({
798841
maxSize: Math.max(minSize, maxSize),
799842
minSize,

0 commit comments

Comments
0 (0)
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.