Skip to content

Commit ee4dbb9

Browse files
EugeneHlushkomontogeek
authored andcommitted
docs(plugins) improve splitChunks name function doc (#3120)
* docs(plugins) improve splitChunks name function doc * docs(plugins) remove the empty example * docs(plugins) splitChunks.name nesting fixed * docs(plugins) splitChunks.name note that config is only partial * docs(plugins) splitChunks.name and splitChunks.test function return type
1 parent 3ad6ca2 commit ee4dbb9

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/content/plugins/split-chunks-plugin.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ T> `maxSize` takes higher priority than `maxInitialRequest/maxAsyncRequests`. Ac
164164

165165
### `splitChunks.name`
166166

167-
`boolean: true | function (module, chunks, cacheGroupKey) | string`
167+
`boolean: true | function (module, chunks, cacheGroupKey):string | string`
168+
169+
Also available for each cacheGroup: `splitChunks.cacheGroups.{cacheGroup}.name`.
168170

169171
The name of the split chunk. Providing `true` will automatically generate a name based on chunks and cache group key.
170172

@@ -176,22 +178,40 @@ If the `splitChunks.name` matches an [entry point](/configuration/entry-context/
176178

177179
T> It is recommended to set `splitChunks.name` to `false` for production builds so that it doesn't change names unnecessarily.
178180

181+
__main.js__
182+
183+
```js
184+
import _ from 'lodash';
185+
186+
console.log(_.join(['Hello', 'webpack'], ' '));
187+
```
188+
179189
__webpack.config.js__
180190

181191
```js
182192
module.exports = {
183193
//...
184194
optimization: {
185195
splitChunks: {
186-
name (module, chunks, cacheGroupKey) {
187-
// generate a chunk name...
188-
return; //...
196+
cacheGroups: {
197+
commons: {
198+
test: /[\\/]node_modules[\\/]/,
199+
// cacheGroupKey here is `commons` as the key of the cacheGroup
200+
name(module, chunks, cacheGroupKey) {
201+
const moduleFileName = module.identifier().split('/').reduceRight(item => item);
202+
const allChunksNames = chunks.map((item) => item.name).join('~');
203+
return `${cacheGroupKey}-${allChunksNames}-${moduleFileName}`;
204+
},
205+
chunks: 'all'
206+
}
189207
}
190208
}
191209
}
192210
};
193211
```
194212

213+
Running webpack with following `splitChunks` configuration would also output a chunk of the group common with next name: `commons-main-lodash.js.e7519d2bb8777058fa27.js` (hash given as an example of real world output).
214+
195215
W> When assigning equal names to different split chunks, all vendor modules are placed into a single shared chunk, though it's not recommend since it can result in more code downloaded.
196216

197217
### `splitChunks.cacheGroups`
@@ -244,7 +264,7 @@ module.exports = {
244264

245265
#### `splitChunks.cacheGroups.{cacheGroup}.test`
246266

247-
`function (module, chunk) | RegExp | string`
267+
`function (module, chunk):boolean | RegExp | string`
248268

249269
Controls which modules are selected by this cache group. Omitting it selects all modules. It can match the absolute module resource path or chunk names. When a chunk name is matched, all modules in the chunk are selected.
250270

0 commit comments

Comments
 (0)