@@ -26,16 +26,45 @@ const sortOrigin = (a, b) => {
2626} ;
2727
2828class ChunkGroup {
29- constructor ( name ) {
29+ constructor ( options ) {
30+ if ( typeof options === "string" ) {
31+ options = { name : options } ;
32+ } else if ( ! options ) {
33+ options = { name : undefined } ;
34+ }
3035 this . groupDebugId = debugId ++ ;
31- this . name = name ;
36+ this . options = options ;
3237 this . _children = new SortableSet ( undefined , sortById ) ;
3338 this . _parents = new SortableSet ( undefined , sortById ) ;
3439 this . _blocks = new SortableSet ( ) ;
3540 this . chunks = [ ] ;
3641 this . origins = [ ] ;
3742 }
3843
44+ addOptions ( options ) {
45+ for ( const key of Object . keys ( options ) ) {
46+ if ( this . options [ key ] === undefined ) {
47+ this . options [ key ] = options [ key ] ;
48+ } else if ( this . options [ key ] !== options [ key ] ) {
49+ if ( key . endsWith ( "Priority" ) ) {
50+ this . options [ key ] = Math . max ( this . options [ key ] , options [ key ] ) ;
51+ } else {
52+ throw new Error (
53+ `ChunkGroup.addOptions: No option merge strategy for ${ key } `
54+ ) ;
55+ }
56+ }
57+ }
58+ }
59+
60+ get name ( ) {
61+ return this . options . name ;
62+ }
63+
64+ set name ( value ) {
65+ this . options . name = value ;
66+ }
67+
3968 /* istanbul ignore next */
4069 get debugId ( ) {
4170 return Array . from ( this . chunks , x => x . debugId ) . join ( "+" ) ;
@@ -222,6 +251,18 @@ class ChunkGroup {
222251 return false ;
223252 }
224253
254+ getFiles ( ) {
255+ const files = new Set ( ) ;
256+
257+ for ( const chunk of this . chunks ) {
258+ for ( const file of chunk . files ) {
259+ files . add ( file ) ;
260+ }
261+ }
262+
263+ return Array . from ( files ) ;
264+ }
265+
225266 remove ( reason ) {
226267 // cleanup parents
227268 for ( const parentChunkGroup of this . _parents ) {
@@ -269,6 +310,53 @@ class ChunkGroup {
269310 this . _children . sort ( ) ;
270311 }
271312
313+ compareTo ( otherGroup ) {
314+ if ( this . chunks . length > otherGroup . chunks . length ) return - 1 ;
315+ if ( this . chunks . length < otherGroup . chunks . length ) return 1 ;
316+ const a = this . chunks [ Symbol . iterator ] ( ) ;
317+ const b = otherGroup . chunks [ Symbol . iterator ] ( ) ;
318+ // eslint-disable-next-line
319+ while ( true ) {
320+ const aItem = a . next ( ) ;
321+ const bItem = b . next ( ) ;
322+ if ( aItem . done ) return 0 ;
323+ const cmp = aItem . value . compareTo ( bItem . value ) ;
324+ if ( cmp !== 0 ) return cmp ;
325+ }
326+ }
327+
328+ getChildrenByPriorities ( ) {
329+ const lists = new Map ( ) ;
330+ for ( const childGroup of this . _children ) {
331+ // TODO webpack 5 remove this check for options
332+ if ( typeof childGroup . options === "object" ) {
333+ for ( const key of Object . keys ( childGroup . options ) ) {
334+ if ( key . endsWith ( "Priority" ) ) {
335+ const name = key . substr ( 0 , key . length - "Priority" . length ) ;
336+ let list = lists . get ( name ) ;
337+ if ( list === undefined ) lists . set ( name , ( list = [ ] ) ) ;
338+ list . push ( {
339+ priority : childGroup . options [ key ] ,
340+ group : childGroup
341+ } ) ;
342+ }
343+ }
344+ }
345+ }
346+ const result = Object . create ( null ) ;
347+ for ( const [ name , list ] of lists ) {
348+ list . sort ( ( a , b ) => {
349+ const cmp = b . priority - a . priority ;
350+ if ( cmp !== 0 ) return cmp ;
351+ // TOOD webpack 5 remove this check of compareTo
352+ if ( a . compareTo ) return a . compareTo ( b ) ;
353+ return 0 ;
354+ } ) ;
355+ result [ name ] = list . map ( i => i . group ) ;
356+ }
357+ return result ;
358+ }
359+
272360 checkConstraints ( ) {
273361 const chunk = this ;
274362 for ( const child of chunk . _children ) {
0 commit comments