forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ST-5243] Make splitChunks configurable #167
Merged
xalechez
merged 2 commits into
Skyscanner:fork
from
james-od:ST-5243-Make_splitChunks_configurable
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,93 @@ | ||
/** | ||
* 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'] || {}; | ||
|
||
module.exports = (isEnvDevelopment) => { | ||
// Automatically split vendor and commons | ||
// https://twitter.com/wSokra/status/969633336732905474 | ||
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366 | ||
return { | ||
splitChunks: bpkReactScriptsConfig.enableAutomaticChunking | ||
? { | ||
module.exports = isEnvDevelopment => { | ||
let splitChunksConfig = {}; | ||
|
||
// If opted in to automatic chunking, apply default configuration | ||
if (bpkReactScriptsConfig.enableAutomaticChunking) { | ||
splitChunksConfig = { | ||
chunks: 'all', | ||
name: isEnvDevelopment, | ||
cacheGroups: bpkReactScriptsConfig.vendorsChunkRegex | ||
? { | ||
vendors: { | ||
test: new RegExp(bpkReactScriptsConfig.vendorsChunkRegex) | ||
}, | ||
} | ||
: {}, | ||
cacheGroups: {}, | ||
}; | ||
// Apply vendorsChunkRegex if provided | ||
if (bpkReactScriptsConfig.vendorsChunkRegex) { | ||
splitChunksConfig.cacheGroups = { | ||
vendors: { | ||
// 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, | ||
name: isEnvDevelopment, | ||
}; | ||
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 | ||
); | ||
} | ||
: {} | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
splitChunks: splitChunksConfig, | ||
}; | ||
}; |
234 changes: 234 additions & 0 deletions
234
packages/react-scripts/backpack-addons/splitChunks.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
'use strict'; | ||
|
||
jest.mock('../config/paths', () => ({ | ||
appPackageJson: './test/mockPackage.json', | ||
})); | ||
|
||
describe('splitChunks', () => { | ||
const mockData = { | ||
name: 'test', | ||
version: '1.0.0', | ||
'backpack-react-scripts': {}, | ||
}; | ||
|
||
let isEnvDevelopment = true; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test('should return default if no config defined', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': {}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ splitChunks: {} }); | ||
}); | ||
|
||
test('should apply basic defaults if automatic chunking enabled without vendors regex', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: true, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ | ||
splitChunks: { chunks: 'all', name: true, cacheGroups: {} }, | ||
}); | ||
}); | ||
|
||
test('should return empty if automatic chunking false and no other config is defined', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: false, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ splitChunks: {} }); | ||
}); | ||
|
||
test('should apply basic defaults and cacheGroup with vendors RegExp when automatic chunking enabled and vendors regex provided', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: true, | ||
vendorsChunkRegex: '[\\/]node_modules[\\/]', | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ | ||
splitChunks: { | ||
chunks: 'all', | ||
name: true, | ||
cacheGroups: { vendors: { test: expect.any(RegExp) } }, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should return empty when automatic chunking disabled and vendors regex provided', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: false, | ||
vendorsChunkRegex: '[\\/]node_modules[\\/]', | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ splitChunks: {} }); | ||
}); | ||
|
||
test('should ignore custom config when automatic chunking enabled and splitChunksConfig is also defined', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: true, | ||
splitChunksConfig: { | ||
cacheGroups: { | ||
vendors: { | ||
test: '[\\/]node_modules[\\/]', | ||
}, | ||
someCustomChunk: { | ||
test: '[\\/]some_regex[\\/]', | ||
priority: 100, | ||
chunks: 'all', | ||
name: 'someCustomChunk', | ||
}, | ||
}, | ||
}, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ | ||
splitChunks: { chunks: 'all', name: true, cacheGroups: {} }, | ||
}); | ||
}); | ||
|
||
test('should not ignore custom config when automatic chunking disabled and splitChunksConfig is defined', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: false, | ||
splitChunksConfig: { | ||
chunks: 'all', | ||
cacheGroups: { | ||
vendors: { | ||
test: '[\\/]node_modules[\\/]', | ||
}, | ||
}, | ||
}, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ | ||
splitChunks: { | ||
chunks: 'all', | ||
name: true, | ||
cacheGroups: { | ||
vendors: { | ||
test: expect.any(RegExp), | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should apply only the name field when splitChunks is empty', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: false, | ||
splitChunksConfig: {}, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ splitChunks: { name: true } }); | ||
}); | ||
|
||
test('should apply Regexes when multiple cacheGroups are applied', () => { | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: false, | ||
splitChunksConfig: { | ||
chunks: 'all', | ||
cacheGroups: { | ||
vendors: { | ||
test: '[\\/]node_modules[\\/]', | ||
}, | ||
someCustomChunk: { | ||
test: '[\\/]some_regex[\\/]', | ||
priority: 100, | ||
chunks: 'all', | ||
name: 'someCustomChunk', | ||
}, | ||
}, | ||
}, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ | ||
splitChunks: { | ||
chunks: 'all', | ||
name: true, | ||
cacheGroups: { | ||
vendors: { | ||
test: expect.any(RegExp), | ||
}, | ||
someCustomChunk: { | ||
test: expect.any(RegExp), | ||
priority: 100, | ||
chunks: 'all', | ||
name: 'someCustomChunk', | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should apply isEnvDevelopment boolean as name value', () => { | ||
let isEnvDevelopment = false; | ||
jest.doMock('./test/mockPackage.json', () => ({ | ||
...mockData, | ||
'backpack-react-scripts': { | ||
enableAutomaticChunking: true, | ||
}, | ||
})); | ||
const splitChunks = require('../backpack-addons/splitChunks'); | ||
|
||
let res = splitChunks(isEnvDevelopment); | ||
|
||
expect(res).toEqual({ | ||
splitChunks: { chunks: 'all', name: false, cacheGroups: {} }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,9 @@ | |
"utils", | ||
"backpack-addons" | ||
], | ||
"scripts": { | ||
"test:addons": "jest --testPathPattern=backpack-addons" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will also run |
||
}, | ||
"bin": { | ||
"react-scripts": "./bin/react-scripts.js" | ||
}, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The testing setup here was a bit awkward, open to suggestions as always.
Essentially,
splitChunks.js
pulls the JSON here:So in the test file, we're mocking the result of
paths
to be{appPackageJson: './test/mockPackage.json'}
.The exact path is arbitrary, only matters that we stay consistent.
Then the second line will resolve as
const appPackageJson = require('./test/mockPackage.json');
.So then in every test we can mock out that module using
doMock
. We needdoMock
instead of.mock
such that the calls aren't hoisted and overriden.We can't (or at least I can't) use the standard jest mocked module pattern because the module returns a JSON object, not a function.
https://jestjs.io/docs/jest-object#jestdomockmodulename-factory-options