Skip to content

Commit

Permalink
Merge pull request #6791 from storybooks/spilt-chunks-selector
Browse files Browse the repository at this point in the history
Support selector function as optimization.splitChunks.chunks option
  • Loading branch information
sokra committed Mar 29, 2018
2 parents 3f6b78f + 1677144 commit 0f70fcb
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 22 deletions.
42 changes: 29 additions & 13 deletions lib/optimize/SplitChunksPlugin.js
Expand Up @@ -85,7 +85,9 @@ module.exports = class SplitChunksPlugin {

static normalizeOptions(options = {}) {
return {
chunks: options.chunks || "all",
chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
options.chunks || "all"
),
minSize: options.minSize || 0,
minChunks: options.minChunks || 1,
maxAsyncRequests: options.maxAsyncRequests || 1,
Expand Down Expand Up @@ -135,6 +137,19 @@ module.exports = class SplitChunksPlugin {
if (typeof name === "function") return name;
}

static normalizeChunksFilter(chunks) {
if (chunks === "initial") {
return chunk => chunk.canBeInitial();
}
if (chunks === "async") {
return chunk => !chunk.canBeInitial();
}
if (chunks === "all") {
return () => true;
}
if (typeof chunks === "function") return chunks;
}

static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
if (typeof cacheGroups === "function") {
return cacheGroups;
Expand Down Expand Up @@ -162,6 +177,11 @@ module.exports = class SplitChunksPlugin {
r
);
if (result.name) result.getName = () => result.name;
if (result.chunks) {
result.chunksFilter = SplitChunksPlugin.normalizeChunksFilter(
result.chunks
);
}
results.push(result);
}
}
Expand All @@ -174,7 +194,9 @@ module.exports = class SplitChunksPlugin {
name: option.name,
automaticNameDelimiter
}),
chunks: option.chunks,
chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
option.chunks
),
enforce: option.enforce,
minSize: option.minSize,
minChunks: option.minChunks,
Expand Down Expand Up @@ -264,7 +286,8 @@ module.exports = class SplitChunksPlugin {
const cacheGroup = {
key: cacheGroupSource.key,
priority: cacheGroupSource.priority || 0,
chunks: cacheGroupSource.chunks || this.options.chunks,
chunksFilter:
cacheGroupSource.chunksFilter || this.options.chunksFilter,
minSize:
cacheGroupSource.minSize !== undefined
? cacheGroupSource.minSize
Expand Down Expand Up @@ -304,16 +327,9 @@ module.exports = class SplitChunksPlugin {
// Break if minimum number of chunks is not reached
if (chunkIndices.length < cacheGroup.minChunks) continue;
// Select chunks by configuration
const selectedChunks =
cacheGroup.chunks === "initial"
? Array.from(chunkCombination).filter(chunk =>
chunk.canBeInitial()
)
: cacheGroup.chunks === "async"
? Array.from(chunkCombination).filter(
chunk => !chunk.canBeInitial()
)
: Array.from(chunkCombination);
const selectedChunks = Array.from(chunkCombination).filter(
cacheGroup.chunksFilter
);
// Break if minimum number of chunks is not reached
if (selectedChunks.length < cacheGroup.minChunks) continue;
// Determine name for split chunk
Expand Down
30 changes: 22 additions & 8 deletions schemas/WebpackOptions.json
Expand Up @@ -1330,10 +1330,17 @@
"properties": {
"chunks": {
"description": "Select chunks for determining shared modules (defaults to \"async\", \"initial\" and \"all\" requires adding these chunks to the HTML)",
"enum": [
"initial",
"async",
"all"
"oneOf": [
{
"enum": [
"initial",
"async",
"all"
]
},
{
"instanceof": "Function"
}
]
},
"minSize": {
Expand Down Expand Up @@ -1420,10 +1427,17 @@
},
"chunks": {
"description": "Select chunks for determining cache group content (defaults to \"initial\", \"initial\" and \"all\" requires adding these chunks to the HTML)",
"enum": [
"initial",
"async",
"all"
"oneOf": [
{
"enum": [
"initial",
"async",
"all"
]
},
{
"instanceof": "Function"
}
]
},
"enforce": {
Expand Down
127 changes: 126 additions & 1 deletion test/statsCases/split-chunks/expected.txt
Expand Up @@ -257,4 +257,129 @@ Child name-too-long:
> ./c cccccccccccccccccccccccccccccc
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
[1] ./f.js 20 bytes {2} {11} {12} [built]
[5] ./c.js 72 bytes {7} {12} [built]
[5] ./c.js 72 bytes {7} {12} [built]
Child custom-chunks-filter:
Entrypoint main = default/main.js
Entrypoint a = default/a.js
Entrypoint b = default/vendors~async-a~async-b~async-c~b~c.js default/vendors~async-a~async-b~b.js default/b.js
Entrypoint c = default/vendors~async-a~async-b~async-c~b~c.js default/vendors~async-c~c.js default/c.js
chunk {0} default/vendors~async-a~async-b~async-c~b~c.js (vendors~async-a~async-b~async-c~b~c) 20 bytes <{9}> ={1}= ={11}= ={12}= ={2}= ={3}= ={4}= ={6}= ={7}= ={8}= >{2}< >{5}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-a~async-b~async-c~b~c)
> ./b b
> ./c c
> ./a [8] ./index.js 1:0-47
> ./b [8] ./index.js 2:0-47
> ./c [8] ./index.js 3:0-47
[2] ./node_modules/x.js 20 bytes {0} {10} [built]
chunk {1} default/async-a~async-b~async-c~b~c.js (async-a~async-b~async-c~b~c) 20 bytes <{9}> ={0}= ={2}= ={3}= ={4}= ={6}= ={7}= ={8}= >{2}< >{5}< [rendered] split chunk (cache group: default) (name: async-a~async-b~async-c~b~c)
> ./a [8] ./index.js 1:0-47
> ./b [8] ./index.js 2:0-47
> ./c [8] ./index.js 3:0-47
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
chunk {2} default/async-b~async-c~async-g~b~c.js (async-b~async-c~async-g~b~c) 20 bytes <{0}> <{1}> <{10}> <{3}> <{6}> <{9}> ={0}= ={1}= ={3}= ={4}= ={5}= ={7}= ={8}= [rendered] split chunk (cache group: default) (name: async-b~async-c~async-g~b~c)
> ./g [] 6:0-47
> ./g [] 6:0-47
> ./b [8] ./index.js 2:0-47
> ./c [8] ./index.js 3:0-47
[1] ./f.js 20 bytes {2} {11} {12} [built]
chunk {3} default/vendors~async-a~async-b~b.js (vendors~async-a~async-b~b) 20 bytes <{9}> ={0}= ={1}= ={11}= ={2}= ={6}= ={7}= >{2}< >{5}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-a~async-b~b)
> ./b b
> ./a [8] ./index.js 1:0-47
> ./b [8] ./index.js 2:0-47
[3] ./node_modules/y.js 20 bytes {3} {10} [built]
chunk {4} default/vendors~async-c~c.js (vendors~async-c~c) 20 bytes <{9}> ={0}= ={1}= ={12}= ={2}= ={8}= [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-c~c)
> ./c c
> ./c [8] ./index.js 3:0-47
[7] ./node_modules/z.js 20 bytes {4} [built]
chunk {5} default/async-g.js (async-g) 34 bytes <{0}> <{1}> <{10}> <{3}> <{6}> ={2}= [rendered]
> ./g [] 6:0-47
> ./g [] 6:0-47
[9] ./g.js 34 bytes {5} [built]
chunk {6} default/async-a.js (async-a) 156 bytes <{9}> ={0}= ={1}= ={3}= >{2}< >{5}< [rendered]
> ./a [8] ./index.js 1:0-47
[6] ./a.js + 1 modules 156 bytes {6} {10} [built]
| ./a.js 121 bytes [built]
| ./e.js 20 bytes [built]
chunk {7} default/async-b.js (async-b) 72 bytes <{9}> ={0}= ={1}= ={2}= ={3}= [rendered]
> ./b [8] ./index.js 2:0-47
[4] ./b.js 72 bytes {7} {11} [built]
chunk {8} default/async-c.js (async-c) 72 bytes <{9}> ={0}= ={1}= ={2}= ={4}= [rendered]
> ./c [8] ./index.js 3:0-47
[5] ./c.js 72 bytes {8} {12} [built]
chunk {9} default/main.js (main) 147 bytes >{0}< >{1}< >{2}< >{3}< >{4}< >{6}< >{7}< >{8}< [entry] [rendered]
> ./ main
[8] ./index.js 147 bytes {9} [built]
chunk {10} default/a.js (a) 216 bytes >{2}< >{5}< [entry] [rendered]
> ./a a
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
[2] ./node_modules/x.js 20 bytes {0} {10} [built]
[3] ./node_modules/y.js 20 bytes {3} {10} [built]
[6] ./a.js + 1 modules 156 bytes {6} {10} [built]
| ./a.js 121 bytes [built]
| ./e.js 20 bytes [built]
chunk {11} default/b.js (b) 112 bytes ={0}= ={3}= [entry] [rendered]
> ./b b
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
[1] ./f.js 20 bytes {2} {11} {12} [built]
[4] ./b.js 72 bytes {7} {11} [built]
chunk {12} default/c.js (c) 112 bytes ={0}= ={4}= [entry] [rendered]
> ./c c
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
[1] ./f.js 20 bytes {2} {11} {12} [built]
[5] ./c.js 72 bytes {8} {12} [built]
Child custom-chunks-filter-in-cache-groups:
Entrypoint main = default/main.js
Entrypoint a = default/a.js
Entrypoint b = default/vendors.js default/b.js
Entrypoint c = default/vendors.js default/c.js
chunk {0} default/vendors.js (vendors) 112 bytes <{5}> ={2}= ={3}= ={4}= ={7}= ={8}= >{1}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors)
> ./b b
> ./c c
> ./a [8] ./index.js 1:0-47
> ./b [8] ./index.js 2:0-47
> ./c [8] ./index.js 3:0-47
[2] ./node_modules/x.js 20 bytes {0} {6} [built]
[3] ./node_modules/y.js 20 bytes {0} {6} [built]
[6] ./node_modules/z.js 20 bytes {0} [built]
[10] multi x y z 52 bytes {0} [built]
chunk {1} default/async-g.js (async-g) 54 bytes <{0}> <{2}> <{6}> [rendered]
> ./g [] 6:0-47
> ./g [] 6:0-47
[1] ./f.js 20 bytes {1} {3} {4} {7} {8} [built]
[9] ./g.js 34 bytes {1} [built]
chunk {2} default/async-a.js (async-a) 176 bytes <{5}> ={0}= >{1}< [rendered]
> ./a [8] ./index.js 1:0-47
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
[7] ./a.js + 1 modules 156 bytes {2} {6} [built]
| ./a.js 121 bytes [built]
| ./e.js 20 bytes [built]
chunk {3} default/async-b.js (async-b) 112 bytes <{5}> ={0}= [rendered]
> ./b [8] ./index.js 2:0-47
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
[1] ./f.js 20 bytes {1} {3} {4} {7} {8} [built]
[4] ./b.js 72 bytes {3} {7} [built]
chunk {4} default/async-c.js (async-c) 112 bytes <{5}> ={0}= [rendered]
> ./c [8] ./index.js 3:0-47
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
[1] ./f.js 20 bytes {1} {3} {4} {7} {8} [built]
[5] ./c.js 72 bytes {4} {8} [built]
chunk {5} default/main.js (main) 147 bytes >{0}< >{2}< >{3}< >{4}< [entry] [rendered]
> ./ main
[8] ./index.js 147 bytes {5} [built]
chunk {6} default/a.js (a) 216 bytes >{1}< [entry] [rendered]
> ./a a
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
[2] ./node_modules/x.js 20 bytes {0} {6} [built]
[3] ./node_modules/y.js 20 bytes {0} {6} [built]
[7] ./a.js + 1 modules 156 bytes {2} {6} [built]
| ./a.js 121 bytes [built]
| ./e.js 20 bytes [built]
chunk {7} default/b.js (b) 112 bytes ={0}= [entry] [rendered]
> ./b b
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
[1] ./f.js 20 bytes {1} {3} {4} {7} {8} [built]
[4] ./b.js 72 bytes {3} {7} [built]
chunk {8} default/c.js (c) 112 bytes ={0}= [entry] [rendered]
> ./c c
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
[1] ./f.js 20 bytes {1} {3} {4} {7} {8} [built]
[5] ./c.js 72 bytes {4} {8} [built]
52 changes: 52 additions & 0 deletions test/statsCases/split-chunks/webpack.config.js
Expand Up @@ -98,5 +98,57 @@ module.exports = [
}
},
stats
},

{
name: "custom-chunks-filter",
mode: "production",
entry: {
main: "./",
a: "./a",
b: "./b",
c: "./c"
},
output: {
filename: "default/[name].js"
},
optimization: {
splitChunks: {
minSize: 0,
chunks: chunk => chunk.name !== "a"
}
},
stats
},

{
name: "custom-chunks-filter-in-cache-groups",
mode: "production",
entry: {
main: "./",
a: "./a",
b: "./b",
c: "./c",
vendors: ["x", "y", "z"]
},
output: {
filename: "default/[name].js"
},
optimization: {
splitChunks: {
minSize: 0,
chunks: "all",
cacheGroups: {
default: false,
vendors: {
test: "vendors",
name: "vendors",
enforce: true,
chunks: chunk => chunk.name !== "a"
}
}
}
},
stats
}
];

0 comments on commit 0f70fcb

Please sign in to comment.