Skip to content

Commit 5ba3c11

Browse files
committed
Adding Encore.splitEntryChunks() to enable that new feature easier
1 parent 3b65403 commit 5ba3c11

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,35 @@ class Encore {
404404
return this;
405405
}
406406

407+
/**
408+
* Tell Webpack to "split" your entry chunks.
409+
*
410+
* This will mean that, instead of adding 1 script tag
411+
* to your page, your server-side code will need to read
412+
* the entrypoints.json file in the build directory to
413+
* determine the *multiple* .js (and .css) files that
414+
* should be included for each entry.
415+
*
416+
* This is a performance optimization, but requires extra
417+
* work (described above) to support this.
418+
*
419+
* @returns {Encore}
420+
*/
421+
splitEntryChunks() {
422+
webpackConfig.splitEntryChunks();
423+
424+
return this;
425+
}
426+
407427
/**
408428
* Configure the optimization.splitChunks configuration.
409429
*
410430
* https://webpack.js.org/plugins/split-chunks-plugin/
411431
*
412432
* Encore.configureSplitChunks(function(splitChunks) {
413433
* // change the configuration
434+
*
435+
* splitChunks.minSize = 0;
414436
* });
415437
*
416438
* @param {function} callback

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class WebpackConfig {
8787
this.stylusLoaderOptionsCallback = () => {};
8888
this.babelConfigurationCallback = () => {};
8989
this.cssLoaderConfigurationCallback = () => {};
90+
this.shouldSplitEntryChunks = false;
9091
this.splitChunksConfigurationCallback = () => {};
9192
this.vueLoaderOptionsCallback = () => {};
9293
this.eslintLoaderOptionsCallback = () => {};
@@ -319,6 +320,10 @@ class WebpackConfig {
319320
this.cssLoaderConfigurationCallback = callback;
320321
}
321322

323+
splitEntryChunks() {
324+
this.shouldSplitEntryChunks = true;
325+
}
326+
322327
configureSplitChunks(callback) {
323328
if (typeof callback !== 'function') {
324329
throw new Error('Argument 1 to configureSplitChunks() must be a callback function.');

lib/config-generator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ class ConfigGenerator {
361361
optimization.namedModules = true;
362362
}
363363

364-
let splitChunks = {};
364+
let splitChunks = {
365+
chunks: this.webpackConfig.shouldSplitEntryChunks ? 'all' : 'async'
366+
};
365367
if (this.webpackConfig.sharedCommonsEntryName) {
366368
const cacheGroups = {};
367369
cacheGroups[this.webpackConfig.sharedCommonsEntryName] = {

test/functional.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,8 @@ module.exports = {
12491249
config.setPublicPath('/build');
12501250
// enable versioning to make sure entrypoints.json is not affected
12511251
config.enableVersioning();
1252+
config.splitEntryChunks();
12521253
config.configureSplitChunks((splitChunks) => {
1253-
splitChunks.chunks = 'all';
12541254
splitChunks.minSize = 0;
12551255
});
12561256

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ describe('Public API', () => {
134134

135135
});
136136

137+
describe('splitEntryChunks', () => {
138+
139+
it('must return the API object', () => {
140+
const returnedValue = api.splitEntryChunks();
141+
expect(returnedValue).to.equal(api);
142+
});
143+
144+
});
145+
137146
describe('configureSplitChunks', () => {
138147

139148
it('must return the API object', () => {

0 commit comments

Comments
 (0)