Skip to content

Commit bc1b4e9

Browse files
authored
docs(CommonsChunkPlugin): Add minChunks fn usage
Feel free to help me clarify this. I really wanted to get the knowledge on paper to be refined.
1 parent c4cd21a commit bc1b4e9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

content/plugins/commons-chunk-plugin.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,28 @@ new CommonsChunkPlugin({
142142
// (3 children must share the module before it's separated)
143143
})
144144
```
145+
146+
### Passing the `minChunks` property a function
147+
148+
You also have the ability to pass the `minChunks` property a function. This function is called by the `CommonsChunkPlugin` and calls the function with `module` and `count` arguments.
149+
150+
The `module` property represents each module in the chunks you have provided via the `names` property.
151+
152+
The `count` property represents how many chunks the `module` is used in.
153+
154+
This option is useful when you want to have fine-grained control over how the CommonsChunk algorithm determins where modules should be moved to.
155+
156+
```javascript
157+
new CommonsChunkPlugin({
158+
name: "my-single-lib-chunk",
159+
filename: "my-single-lib-chunk.js",
160+
minChunks: function(module, countOfHowManyTimesThisModuleIsUsedAcrossAllChunks) {
161+
// If module has a path, and inside of the path exists the name "somelib",
162+
// and it is used in 3 separate chunks/entries, then break it out into
163+
// a separate chunk with chunk keyname "my-single-lib-chunk", and filename "my-single-lib-chunk.js"
164+
return module.resouce && (/somelib/).test(module.resource) && count === 3;
165+
}
166+
});
167+
```
168+
169+
As seen above, this example allows you to move only one lib to a separate file if and only if all conditions are met inside the function.

0 commit comments

Comments
 (0)