forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsplitChunks.js
107 lines (100 loc) · 3.12 KB
/
splitChunks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Defines a webpack splitChunks configuration, optionally based on consumer configuration.
*
* For automatic configuration set enableAutomaticChunking and optionally provide a vendorsChunkRegex string, e.g:
*
* // package.json
* ...
* "backpack-react-scripts": {
* ...
* "enableAutomaticChunking": true,
* "vendorsChunkRegex": "...",
* ...
* }
* ...
*
* For custom configuration disable enableAutomaticChunking and provide a configuration object, e.g:
*
* // package.json
* ...
* "backpack-react-scripts": {
* ...
* "enableAutomaticChunking": false,
* "splitChunksConfig": {
* "chunks": "all",
* ...
* "cacheGroups": {
* "vendors": {
* "test": "..."
* },
* "customChunk": {
* "test": "..."
* "priority": 100,
* "chunks": "all",
* "name": "customChunk",
* },
* },
* ...
* }
* ...
*
* References:
* https://webpack.js.org/plugins/split-chunks-plugin/#optimizationsplitchunks
* https://webpack.js.org/plugins/split-chunks-plugin/#splitchunkscachegroups
* https://twitter.com/wSokra/status/969633336732905474
* https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
*/
'use strict';
const paths = require('../config/paths');
const appPackageJson = require(paths.appPackageJson);
const bpkReactScriptsConfig = appPackageJson['backpack-react-scripts'] || {};
const backpackStylesCacheGroup = {
name: 'bpk-styles',
type: 'css/mini-extract',
chunks: 'all',
enforce: true,
test: /[\\/]node_modules[\\/](@skyscanner[\\/]backpack-web[\\/]|@skyscanner-internal[\\/]bpk-logos)/,
priority: 1,
};
module.exports = () => {
let splitChunksConfig = {};
// If opted in to automatic chunking, apply default configuration
if (bpkReactScriptsConfig.enableAutomaticChunking) {
splitChunksConfig = {
chunks: 'all',
cacheGroups: {},
};
// Apply vendorsChunkRegex if provided
if (bpkReactScriptsConfig.vendorsChunkRegex) {
splitChunksConfig.cacheGroups = {
defaultVendors: {
// Regexes are passed as strings in package.json config, but need constructed here.
test: new RegExp(bpkReactScriptsConfig.vendorsChunkRegex),
},
};
}
}
// If not opted in to automatic chunking, use custom configuration - if defined.
else if (bpkReactScriptsConfig.splitChunksConfig) {
splitChunksConfig = {
...bpkReactScriptsConfig.splitChunksConfig,
};
if (splitChunksConfig.cacheGroups) {
// Regexes are passed as strings in package.json config, but need constructed here.
for (let cacheGroup of Object.keys(splitChunksConfig.cacheGroups)) {
splitChunksConfig.cacheGroups[cacheGroup].test = new RegExp(
splitChunksConfig.cacheGroups[cacheGroup].test
);
}
}
}
if (bpkReactScriptsConfig.enableBpkStylesChunk) {
if (!splitChunksConfig.cacheGroups) {
splitChunksConfig.cacheGroups = {};
}
splitChunksConfig.cacheGroups.bpkStyles = backpackStylesCacheGroup;
}
return {
splitChunks: splitChunksConfig,
};
};