55"use strict" ;
66
77const util = require ( "util" ) ;
8+
89const DependenciesBlock = require ( "./DependenciesBlock" ) ;
910const ModuleReason = require ( "./ModuleReason" ) ;
11+ const SortableSet = require ( "./util/SortableSet" ) ;
1012const Template = require ( "./Template" ) ;
1113
12- function byId ( a , b ) {
14+ let debugId = 1000 ;
15+
16+ const sortById = ( a , b ) => {
1317 return a . id - b . id ;
14- }
18+ } ;
1519
16- function byDebugId ( a , b ) {
20+ const sortByDebugId = ( a , b ) => {
1721 return a . debugId - b . debugId ;
18- }
19-
20- let debugId = 1000 ;
22+ } ;
2123
2224class Module extends DependenciesBlock {
25+
2326 constructor ( ) {
2427 super ( ) ;
2528 this . context = null ;
@@ -34,9 +37,7 @@ class Module extends DependenciesBlock {
3437 this . used = null ;
3538 this . usedExports = null ;
3639 this . providedExports = null ;
37- this . _chunks = new Set ( ) ;
38- this . _chunksIsSorted = true ;
39- this . _chunksIsSortedByDebugId = true ;
40+ this . _chunks = new SortableSet ( undefined , sortById ) ;
4041 this . _chunksDebugIdent = undefined ;
4142 this . warnings = [ ] ;
4243 this . dependenciesWarnings = [ ] ;
@@ -59,7 +60,6 @@ class Module extends DependenciesBlock {
5960 this . providedExports = null ;
6061 this . _chunks . clear ( ) ;
6162 this . _chunksDebugIdent = undefined ;
62- this . _chunksIsSorted = this . _chunksIsSortedByDebugId = false ;
6363 super . disconnect ( ) ;
6464 }
6565
@@ -71,14 +71,16 @@ class Module extends DependenciesBlock {
7171 this . depth = null ;
7272 this . _chunks . clear ( ) ;
7373 this . _chunksDebugIdent = undefined ;
74- this . _chunksIsSorted = this . _chunksIsSortedByDebugId = false ;
7574 super . unseal ( ) ;
7675 }
7776
77+ setChunks ( chunks ) {
78+ this . _chunks = new SortableSet ( chunks , sortById ) ;
79+ }
80+
7881 addChunk ( chunk ) {
7982 this . _chunks . add ( chunk ) ;
8083 this . _chunksDebugIdent = undefined ;
81- this . _chunksIsSorted = this . _chunksIsSortedByDebugId = false ;
8284 }
8385
8486 removeChunk ( chunk ) {
@@ -96,7 +98,7 @@ class Module extends DependenciesBlock {
9698
9799 getChunkIdsIdent ( ) {
98100 if ( this . _chunksDebugIdent !== undefined ) return this . _chunksDebugIdent ;
99- this . _ensureChunksSortedByDebugId ( ) ;
101+ this . _chunks . sortWith ( sortByDebugId ) ;
100102 const chunks = this . _chunks ;
101103 const list = [ ] ;
102104 for ( const chunk of chunks ) {
@@ -130,8 +132,8 @@ class Module extends DependenciesBlock {
130132
131133 hasEqualsChunks ( otherModule ) {
132134 if ( this . _chunks . size !== otherModule . _chunks . size ) return false ;
133- this . _ensureChunksSortedByDebugId ( ) ;
134- otherModule . _ensureChunksSortedByDebugId ( ) ;
135+ this . _chunks . sortWith ( sortByDebugId ) ;
136+ otherModule . _chunks . sortWith ( sortByDebugId ) ;
135137 const a = this . _chunks [ Symbol . iterator ] ( ) ;
136138 const b = otherModule . _chunks [ Symbol . iterator ] ( ) ;
137139 while ( true ) { // eslint-disable-line
@@ -142,20 +144,6 @@ class Module extends DependenciesBlock {
142144 }
143145 }
144146
145- _ensureChunksSorted ( ) {
146- if ( this . _chunksIsSorted ) return ;
147- this . _chunks = new Set ( Array . from ( this . _chunks ) . sort ( byId ) ) ;
148- this . _chunksIsSortedByDebugId = false ;
149- this . _chunksIsSorted = true ;
150- }
151-
152- _ensureChunksSortedByDebugId ( ) {
153- if ( this . _chunksIsSortedByDebugId ) return ;
154- this . _chunks = new Set ( Array . from ( this . _chunks ) . sort ( byDebugId ) ) ;
155- this . _chunksIsSorted = false ;
156- this . _chunksIsSortedByDebugId = true ;
157- }
158-
159147 addReason ( module , dependency ) {
160148 this . reasons . push ( new ModuleReason ( module , dependency ) ) ;
161149 }
@@ -221,8 +209,8 @@ class Module extends DependenciesBlock {
221209 sortItems ( sortChunks ) {
222210 super . sortItems ( ) ;
223211 if ( sortChunks )
224- this . _ensureChunksSorted ( ) ;
225- this . reasons . sort ( ( a , b ) => byId ( a . module , b . module ) ) ;
212+ this . _chunks . sort ( ) ;
213+ this . reasons . sort ( ( a , b ) => sortById ( a . module , b . module ) ) ;
226214 if ( Array . isArray ( this . usedExports ) ) {
227215 this . usedExports . sort ( ) ;
228216 }
0 commit comments