Skip to content

Commit

Permalink
Merge pull request #8466 from webpack/bugfix/splitChunks-enforce
Browse files Browse the repository at this point in the history
enforce should not prevent using `minChunks` etc. on cacheGroup
  • Loading branch information
sokra committed Dec 5, 2018
2 parents f47bf8b + b56727e commit f29ca64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
60 changes: 30 additions & 30 deletions lib/optimize/SplitChunksPlugin.js
Expand Up @@ -465,11 +465,7 @@ module.exports = class SplitChunksPlugin {
module
) => {
// Break if minimum number of chunks is not reached
if (
!cacheGroup.enforce &&
selectedChunks.length < cacheGroup.minChunks
)
return;
if (selectedChunks.length < cacheGroup.minChunks) return;
// Determine name for split chunk
const name = cacheGroup.getName(
module,
Expand All @@ -492,7 +488,7 @@ module.exports = class SplitChunksPlugin {
modules: new SortableSet(undefined, sortByIdentifier),
cacheGroup,
name,
validateSize: !cacheGroup.enforce && cacheGroup.minSize > 0,
validateSize: cacheGroup.minSize > 0,
size: 0,
chunks: new Set(),
reuseableChunks: new Set(),
Expand Down Expand Up @@ -534,27 +530,40 @@ module.exports = class SplitChunksPlugin {
priority: cacheGroupSource.priority || 0,
chunksFilter:
cacheGroupSource.chunksFilter || this.options.chunksFilter,
enforce: cacheGroupSource.enforce,
minSize:
cacheGroupSource.minSize !== undefined
? cacheGroupSource.minSize
: cacheGroupSource.enforce
? 0
: this.options.minSize,
minSizeForMaxSize:
cacheGroupSource.minSize !== undefined
? cacheGroupSource.minSize
: this.options.minSize,
maxSize:
cacheGroupSource.maxSize !== undefined
? cacheGroupSource.maxSize
: this.options.maxSize,
: cacheGroupSource.enforce
? 0
: this.options.maxSize,
minChunks:
cacheGroupSource.minChunks !== undefined
? cacheGroupSource.minChunks
: this.options.minChunks,
: cacheGroupSource.enforce
? 1
: this.options.minChunks,
maxAsyncRequests:
cacheGroupSource.maxAsyncRequests !== undefined
? cacheGroupSource.maxAsyncRequests
: this.options.maxAsyncRequests,
: cacheGroupSource.enforce
? Infinity
: this.options.maxAsyncRequests,
maxInitialRequests:
cacheGroupSource.maxInitialRequests !== undefined
? cacheGroupSource.maxInitialRequests
: this.options.maxInitialRequests,
: cacheGroupSource.enforce
? Infinity
: this.options.maxInitialRequests,
getName:
cacheGroupSource.getName !== undefined
? cacheGroupSource.getName
Expand All @@ -572,11 +581,7 @@ module.exports = class SplitChunksPlugin {
// For all combination of chunk selection
for (const chunkCombination of combs) {
// Break if minimum number of chunks is not reached
if (
!cacheGroup.enforce &&
chunkCombination.size < cacheGroup.minChunks
)
continue;
if (chunkCombination.size < cacheGroup.minChunks) continue;
// Select chunks by configuration
const {
chunks: selectedChunks,
Expand All @@ -586,14 +591,12 @@ module.exports = class SplitChunksPlugin {
cacheGroup.chunksFilter
);

if (selectedChunks.length > 0) {
addModuleToChunksInfoMap(
cacheGroup,
selectedChunks,
selectedChunksKey,
module
);
}
addModuleToChunksInfoMap(
cacheGroup,
selectedChunks,
selectedChunksKey,
module
);
}
}
}
Expand Down Expand Up @@ -672,9 +675,8 @@ module.exports = class SplitChunksPlugin {
if (usedChunks.length === 0) continue;

if (
!item.cacheGroup.enforce &&
(Number.isFinite(item.cacheGroup.maxInitialRequests) ||
Number.isFinite(item.cacheGroup.maxAsyncRequests))
Number.isFinite(item.cacheGroup.maxInitialRequests) ||
Number.isFinite(item.cacheGroup.maxAsyncRequests)
) {
const chunkInLimit = usedChunks.filter(chunk => {
// respect max requests when not enforced
Expand All @@ -692,8 +694,6 @@ module.exports = class SplitChunksPlugin {
});

if (chunkInLimit.length < usedChunks.length) {
// We do not need to check enforce here as it was
// already checked above.
if (chunkInLimit.length >= item.cacheGroup.minChunks) {
for (const module of item.modules) {
addModuleToChunksInfoMap(
Expand Down Expand Up @@ -775,7 +775,7 @@ module.exports = class SplitChunksPlugin {
maxSizeQueueMap.set(newChunk, {
minSize: Math.max(
oldMaxSizeSettings ? oldMaxSizeSettings.minSize : 0,
item.cacheGroup.minSize
item.cacheGroup.minSizeForMaxSize
),
maxSize: Math.min(
oldMaxSizeSettings ? oldMaxSizeSettings.maxSize : Infinity,
Expand Down
5 changes: 3 additions & 2 deletions test/statsCases/split-chunks-automatic-name/webpack.config.js
Expand Up @@ -17,17 +17,18 @@ module.exports = {
optimization: {
splitChunks: {
chunks: "all",
minSize: 1,
cacheGroups: {
default: {
automaticNamePrefix: "common",
reuseExistingChunk: true,
minChunks: 2,
priority: -20
priority: -20,
enforce: true // minChunks should have higher priority
},
vendors: {
automaticNamePrefix: "common",
test: /[\\/]node_modules[\\/]/,
minSize: 1,
priority: -10
}
}
Expand Down

0 comments on commit f29ca64

Please sign in to comment.