Skip to content

Commit 98a04cb

Browse files
authored
[LOOM-1328]: Add test suite for backpack-addons/splitChunks remove unstable from backpack chunk config (#185)
* add tests for splitChunks addon & remove unstable from backpack chunk config * move where addon tests are executed * add docs for new config
1 parent 4e98f84 commit 98a04cb

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ jobs:
2121
run: npm ci --prefer-offline
2222
- name: Build
2323
run: npm run build
24+
- name: Run addon tests
25+
run: npm run test:addons

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"screencast:error": "svg-term --cast jyu19xGl88FQ3poMY8Hbmfw8y --out screencast-error.svg --window --at 12000 --no-cursor",
1818
"alex": "alex .",
1919
"test:integration": "jest test/integration",
20+
"test:addons": "jest packages/react-scripts/backpack-addons",
2021
"test": "cd packages/react-scripts && node bin/react-scripts.js test",
2122
"eslint": "eslint .",
2223
"prettier": "prettier .",

packages/react-scripts/backpack-addons/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Our react scripts fork includes a number of custom configuration items in order
1414
| **ssrExternals** | The same as above `externals` except used for server side rendering only in **ssr.js** | **{}** |
1515
| **enableAutomaticChunking** | Opts into automatic chunking of vender, common and app code.<br> When enabled the **splitChunks** plugin creates vender and common chunks which are split and when provided uses the `venderChunkRegex` to specify what is in each chunk.<br> When enabled **runtimeChunk** plugin creates a separate runtime chunk for projects to enable long term caching. | **false** |
1616
| **vendorsChunkRegex** | Regex for picking what goes into the vendors chunk. Requires enableAutomaticChunking to be enabled.<br> See [cacheGroups](https://webpack.js.org/plugins/split-chunks-plugin/#splitchunkscachegroups) docs for further details. | |
17-
| **splitChunksConfig** | Object, mapping to the [structure in the webpack docs](https://webpack.js.org/plugins/split-chunks-plugin/#optimizationsplitchunks).<br> Applied only if `enableAutomaticChunking` is false, ignores `vendorsChunkRegex` if defined. | |
17+
| **splitChunksConfig** | Object, mapping to the [structure in the webpack docs](https://webpack.js.org/plugins/split-chunks-plugin/#optimizationsplitchunks).<br> Applied only if `enableAutomaticChunking` is false, ignores `vendorsChunkRegex` if defined. |
18+
| **enableBpkStylesChunk** | Boolean, when enabled will create a single chunk for all Backpack CSS. | false |
19+
1820

1921
## How to add new feature
2022

packages/react-scripts/backpack-addons/splitChunks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const backpackStylesCacheGroup = {
5757
chunks: 'all',
5858
enforce: true,
5959
test: /[\\/]node_modules[\\/]@skyscanner[\\/]backpack-web[\\/]/,
60-
priority: 1
60+
priority: 1,
6161
};
6262

6363
module.exports = () => {
@@ -94,7 +94,7 @@ module.exports = () => {
9494
}
9595
}
9696

97-
if (bpkReactScriptsConfig.__unstableEnableBpkStylesChunk) {
97+
if (bpkReactScriptsConfig.enableBpkStylesChunk) {
9898
if (!splitChunksConfig.cacheGroups) {
9999
splitChunksConfig.cacheGroups = {};
100100
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
let mockPackageJson = {};
4+
5+
jest.mock('../package.json', () => ({
6+
'backpack-react-scripts': mockPackageJson,
7+
}));
8+
9+
describe('splitChunks addon', () => {
10+
let splitChunks;
11+
beforeEach(() => {
12+
mockPackageJson = {};
13+
jest.isolateModules(() => {
14+
splitChunks = require('./splitChunks');
15+
});
16+
});
17+
18+
it('by default is {}', () => {
19+
expect(splitChunks()).toEqual({ splitChunks: {} });
20+
});
21+
22+
it('enableBpkStylesChunk should define a bpkStyles cache group', () => {
23+
mockPackageJson.enableBpkStylesChunk = true;
24+
expect(splitChunks().splitChunks.cacheGroups.bpkStyles).toBeDefined();
25+
});
26+
27+
it('enableAutomaticChunking should add chunks: all and have an empty cacheGroups', () => {
28+
mockPackageJson.enableAutomaticChunking = true;
29+
expect(splitChunks()).toEqual({
30+
splitChunks: {
31+
chunks: 'all',
32+
cacheGroups: {},
33+
},
34+
});
35+
});
36+
37+
it('enableAutomaticChunking & vendorsChunkRegex should add chunks: all and have a user customised cacheGroup', () => {
38+
mockPackageJson.enableAutomaticChunking = true;
39+
mockPackageJson.vendorsChunkRegex =
40+
'[\\/]node_modules[\\/]((?!bpk-.*?)(?!@skyscanner.*?).*?)[\\/]';
41+
expect(splitChunks()).toEqual({
42+
splitChunks: {
43+
chunks: 'all',
44+
cacheGroups: {
45+
defaultVendors: {
46+
test: new RegExp(mockPackageJson.vendorsChunkRegex),
47+
},
48+
},
49+
},
50+
});
51+
});
52+
53+
it('splitChunksConfig should allow custom cache groups to be defined by users', () => {
54+
mockPackageJson.splitChunksConfig = {
55+
chunks: 'async',
56+
cacheGroups: {
57+
myCacheGroup: {
58+
test: '/myModule/',
59+
},
60+
},
61+
};
62+
63+
expect(splitChunks()).toEqual({
64+
splitChunks: {
65+
chunks: 'async',
66+
cacheGroups: {
67+
myCacheGroup: {
68+
test: /\/myModule\//,
69+
},
70+
},
71+
},
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)