@@ -966,74 +966,90 @@ export default async function getBaseWebpackConfig(
966
966
}
967
967
}
968
968
969
+ const frameworkCacheGroup = {
970
+ chunks : 'all' as const ,
971
+ name : 'framework' ,
972
+ // Ensures the framework chunk is not created for App Router.
973
+ layer : isWebpackDefaultLayer ,
974
+ test ( module : any ) {
975
+ const resource = module . nameForCondition ?.( )
976
+ return resource
977
+ ? topLevelFrameworkPaths . some ( ( pkgPath ) =>
978
+ resource . startsWith ( pkgPath )
979
+ )
980
+ : false
981
+ } ,
982
+ priority : 40 ,
983
+ // Don't let webpack eliminate this chunk (prevents this chunk from
984
+ // becoming a part of the commons chunk)
985
+ enforce : true ,
986
+ }
987
+ const libCacheGroup = {
988
+ test ( module : {
989
+ size : Function
990
+ nameForCondition : Function
991
+ } ) : boolean {
992
+ return (
993
+ module . size ( ) > 160000 &&
994
+ / n o d e _ m o d u l e s [ / \\ ] / . test ( module . nameForCondition ( ) || '' )
995
+ )
996
+ } ,
997
+ name ( module : {
998
+ layer : string | null | undefined
999
+ type : string
1000
+ libIdent ?: Function
1001
+ updateHash : ( hash : crypto . Hash ) => void
1002
+ } ) : string {
1003
+ const hash = crypto . createHash ( 'sha1' )
1004
+ if ( isModuleCSS ( module ) ) {
1005
+ module . updateHash ( hash )
1006
+ } else {
1007
+ if ( ! module . libIdent ) {
1008
+ throw new Error (
1009
+ `Encountered unknown module type: ${ module . type } . Please open an issue.`
1010
+ )
1011
+ }
1012
+ hash . update ( module . libIdent ( { context : dir } ) )
1013
+ }
1014
+
1015
+ // Ensures the name of the chunk is not the same between two modules in different layers
1016
+ // E.g. if you import 'button-library' in App Router and Pages Router we don't want these to be bundled in the same chunk
1017
+ // as they're never used on the same page.
1018
+ if ( module . layer ) {
1019
+ hash . update ( module . layer )
1020
+ }
1021
+
1022
+ return hash . digest ( 'hex' ) . substring ( 0 , 8 )
1023
+ } ,
1024
+ priority : 30 ,
1025
+ minChunks : 1 ,
1026
+ reuseExistingChunk : true ,
1027
+ }
1028
+ const cssCacheGroup = {
1029
+ test : / \. ( c s s | s a s s | s c s s ) $ / i,
1030
+ chunks : 'all' as const ,
1031
+ enforce : true ,
1032
+ type : / c s s / ,
1033
+ minChunks : 2 ,
1034
+ priority : 100 ,
1035
+ }
969
1036
return {
970
1037
// Keep main and _app chunks unsplitted in webpack 5
971
1038
// as we don't need a separate vendor chunk from that
972
1039
// and all other chunk depend on them so there is no
973
1040
// duplication that need to be pulled out.
974
1041
chunks : ( chunk : any ) =>
975
1042
! / ^ ( p o l y f i l l s | m a i n | p a g e s \/ _ a p p ) $ / . test ( chunk . name ) ,
976
- cacheGroups : {
977
- framework : {
978
- chunks : 'all' ,
979
- name : 'framework' ,
980
- // Ensures the framework chunk is not created for App Router.
981
- layer : isWebpackDefaultLayer ,
982
- test ( module : any ) {
983
- const resource = module . nameForCondition ?.( )
984
- return resource
985
- ? topLevelFrameworkPaths . some ( ( pkgPath ) =>
986
- resource . startsWith ( pkgPath )
987
- )
988
- : false
989
- } ,
990
- priority : 40 ,
991
- // Don't let webpack eliminate this chunk (prevents this chunk from
992
- // becoming a part of the commons chunk)
993
- enforce : true ,
994
- } ,
995
- lib : {
996
- test ( module : {
997
- size : Function
998
- nameForCondition : Function
999
- } ) : boolean {
1000
- return (
1001
- module . size ( ) > 160000 &&
1002
- / n o d e _ m o d u l e s [ / \\ ] / . test ( module . nameForCondition ( ) || '' )
1003
- )
1004
- } ,
1005
- name ( module : {
1006
- layer : string | null | undefined
1007
- type : string
1008
- libIdent ?: Function
1009
- updateHash : ( hash : crypto . Hash ) => void
1010
- } ) : string {
1011
- const hash = crypto . createHash ( 'sha1' )
1012
- if ( isModuleCSS ( module ) ) {
1013
- module . updateHash ( hash )
1014
- } else {
1015
- if ( ! module . libIdent ) {
1016
- throw new Error (
1017
- `Encountered unknown module type: ${ module . type } . Please open an issue.`
1018
- )
1019
- }
1020
- hash . update ( module . libIdent ( { context : dir } ) )
1021
- }
1022
-
1023
- // Ensures the name of the chunk is not the same between two modules in different layers
1024
- // E.g. if you import 'button-library' in App Router and Pages Router we don't want these to be bundled in the same chunk
1025
- // as they're never used on the same page.
1026
- if ( module . layer ) {
1027
- hash . update ( module . layer )
1028
- }
1029
-
1030
- return hash . digest ( 'hex' ) . substring ( 0 , 8 )
1043
+ cacheGroups : appDir
1044
+ ? {
1045
+ css : cssCacheGroup ,
1046
+ framework : frameworkCacheGroup ,
1047
+ lib : libCacheGroup ,
1048
+ }
1049
+ : {
1050
+ framework : frameworkCacheGroup ,
1051
+ lib : libCacheGroup ,
1031
1052
} ,
1032
- priority : 30 ,
1033
- minChunks : 1 ,
1034
- reuseExistingChunk : true ,
1035
- } ,
1036
- } ,
1037
1053
maxInitialRequests : 25 ,
1038
1054
minSize : 20000 ,
1039
1055
}
0 commit comments