You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #680 Add Encore.addCacheGroup() method and depreciate Encore.createSharedEntry() (Lyrkan)
This PR was squashed before being merged into the master branch (closes#680).
Discussion
----------
Add Encore.addCacheGroup() method and depreciate Encore.createSharedEntry()
As discussed in #645 (comment) our `Encore.createSharedEntry()` hack won't work anymore with Webpack 5.
Since it was mostly something that was added to make the transition from Webpack 3 to Webpack 4 less painful it's probably time to depreciate it and encourage people to use cache groups properly instead.
This PR adds a new `Encore.addCacheGroup()` method that, as its name implies, simply adds a new cache group to the config:
```js
Encore.addCacheGroup('vendor', {
test: /[\\/]node_modules[\\/]react/
});
```
To make it a bit easier in case people want to directly reference things from the `node_modules` directory I also added a `node_modules` option which is basically a shorthand that sets the `test` option:
```js
Encore.addCacheGroup('vendor', {
node_modules: ['react', 'react-dom']
});
```
Commits
-------
7e32b0b Add a warning when calling addCacheGroup() with 'vendors' or the same than in createSharedEntry()
e4095b3 Add Encore.addCacheGroup() method and depreciate Encore.createSharedEntry()
logger.deprecation('Encore.createSharedEntry() is deprecated and will be removed in a future version, please use Encore.splitEntryChunks() or Encore.addCacheGroup() instead.');
517
+
514
518
if(this.shouldSplitEntryChunks){
515
519
thrownewError('Using splitEntryChunks() and createSharedEntry() together is not supported. Use one of these strategies only to optimize your build.');
516
520
}
@@ -530,6 +534,36 @@ class WebpackConfig {
530
534
this.addEntry(name,file);
531
535
}
532
536
537
+
addCacheGroup(name,options){
538
+
if(typeofname!=='string'){
539
+
thrownewError('Argument 1 to addCacheGroup() must be a string.');
540
+
}
541
+
542
+
if(typeofoptions!=='object'){
543
+
thrownewError('Argument 2 to addCacheGroup() must be an object.');
544
+
}
545
+
546
+
if(!options['test']&&!options['node_modules']){
547
+
thrownewError('Either the "test" option or the "node_modules" option of addCacheGroup() must be set');
548
+
}
549
+
550
+
if(options['node_modules']){
551
+
if(!Array.isArray(options['node_modules'])){
552
+
thrownewError('The "node_modules" option of addCacheGroup() must be an array');
Copy file name to clipboardExpand all lines: lib/config/validator.js
+14
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,8 @@ class Validator {
29
29
this._validateDevServer();
30
30
31
31
this._validateSharedEntryName();
32
+
33
+
this._validateCacheGroupNames();
32
34
}
33
35
34
36
_validateBasic(){
@@ -75,6 +77,18 @@ class Validator {
75
77
logger.warning(`Passing "${this.webpackConfig.sharedCommonsEntryName}" to createSharedEntry() is not recommended, as it will override the built-in cache group by this name.`);
0 commit comments