22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Tobias Koppers @sokra
44*/
5+ var compareLocations = require ( "./compareLocations" ) ;
56var debugId = 1000 ;
67
78function Chunk ( name , module , loc ) {
@@ -10,14 +11,13 @@ function Chunk(name, module, loc) {
1011 this . debugId = debugId ++ ;
1112 this . name = name ;
1213 this . modules = [ ] ;
14+ this . entrypoints = [ ] ;
1315 this . chunks = [ ] ;
1416 this . parents = [ ] ;
1517 this . blocks = [ ] ;
1618 this . origins = [ ] ;
1719 this . files = [ ] ;
1820 this . rendered = false ;
19- this . entry = false ;
20- this . initial = false ;
2121 if ( module ) {
2222 this . origins . push ( {
2323 module : module ,
@@ -28,6 +28,39 @@ function Chunk(name, module, loc) {
2828}
2929module . exports = Chunk ;
3030
31+ Object . defineProperty ( Chunk . prototype , "entry" , {
32+ configurable : false ,
33+ get : function ( ) {
34+ throw new Error ( "Chunk.entry was removed. Use hasRuntime()" ) ;
35+ } ,
36+ set : function ( ) {
37+ throw new Error ( "Chunk.entry was removed. Use hasRuntime()" ) ;
38+ }
39+ } ) ;
40+
41+ Object . defineProperty ( Chunk . prototype , "initial" , {
42+ configurable : false ,
43+ get : function ( ) {
44+ throw new Error ( "Chunk.initial was removed. Use isInitial()" ) ;
45+ } ,
46+ set : function ( ) {
47+ throw new Error ( "Chunk.initial was removed. Use isInitial()" ) ;
48+ }
49+ } ) ;
50+
51+ Chunk . prototype . hasRuntime = function ( ) {
52+ if ( this . entrypoints . length === 0 ) return false ;
53+ return this . entrypoints [ 0 ] . chunks [ 0 ] === this ;
54+ } ;
55+
56+ Chunk . prototype . isInitial = function ( ) {
57+ return this . entrypoints . length > 0 ;
58+ } ;
59+
60+ Chunk . prototype . hasEntryModule = function ( ) {
61+ return ! ! this . entryModule ;
62+ } ;
63+
3164Chunk . prototype . addModule = function ( module ) {
3265 if ( this . modules . indexOf ( module ) >= 0 ) {
3366 return false ;
@@ -117,6 +150,13 @@ Chunk.prototype.remove = function(reason) {
117150 } , this ) ;
118151} ;
119152
153+ Chunk . prototype . moveModule = function ( module , other ) {
154+ module . removeChunk ( this ) ;
155+ module . addChunk ( other ) ;
156+ other . addModule ( module ) ;
157+ module . rewriteChunkInReasons ( this , [ other ] ) ;
158+ } ;
159+
120160Chunk . prototype . integrate = function ( other , reason ) {
121161 if ( ! this . canBeIntegrated ( other ) ) {
122162 return false ;
@@ -161,44 +201,65 @@ Chunk.prototype.integrate = function(other, reason) {
161201 } , this ) ;
162202 other . blocks . length = 0 ;
163203 other . origins . forEach ( function ( origin ) {
204+ this . origins . push ( origin ) ;
205+ } , this ) ;
206+ this . origins . forEach ( function ( origin ) {
164207 if ( ! origin . reasons ) {
165208 origin . reasons = [ reason ] ;
166209 } else if ( origin . reasons [ 0 ] !== reason ) {
167210 origin . reasons . unshift ( reason ) ;
168211 }
169- this . origins . push ( origin ) ;
170- } , this ) ;
212+ } )
171213 return true ;
172214} ;
173215
216+ Chunk . prototype . split = function ( newChunk ) {
217+ var _this = this ;
218+ this . blocks . forEach ( function ( b ) {
219+ newChunk . blocks . push ( b ) ;
220+ b . chunks . push ( newChunk ) ;
221+ } ) ;
222+ this . chunks . forEach ( function ( c ) {
223+ newChunk . chunks . push ( c ) ;
224+ c . parents . push ( newChunk ) ;
225+ } ) ;
226+ this . parents . forEach ( function ( p ) {
227+ p . chunks . push ( newChunk ) ;
228+ newChunk . parents . push ( p ) ;
229+ } ) ;
230+ this . entrypoints . forEach ( function ( e ) {
231+ e . insertChunk ( newChunk , _this ) ;
232+ } ) ;
233+ } ;
234+
174235Chunk . prototype . isEmpty = function ( ) {
175236 return this . modules . length === 0 ;
176237} ;
177238
178239Chunk . prototype . updateHash = function ( hash ) {
179240 hash . update ( this . id + " " ) ;
180241 hash . update ( this . ids ? this . ids . join ( "," ) : "" ) ;
181- hash . update ( this . name + "" ) ;
242+ hash . update ( ( this . name || "" ) + " " ) ;
182243 this . modules . forEach ( function ( m ) {
183244 m . updateHash ( hash ) ;
184245 } ) ;
185246} ;
186247
187248Chunk . prototype . size = function ( options ) {
188- var CHUNK_OVERHEAD = options . chunkOverhead || 10000 ;
249+ var CHUNK_OVERHEAD = typeof options . chunkOverhead === "number" ? options . chunkOverhead : 10000 ;
189250 var ENTRY_CHUNK_MULTIPLICATOR = options . entryChunkMultiplicator || 10 ;
190251
191252 var modulesSize = this . modules . reduce ( function ( a , b ) {
192253 return a + b . size ( ) ;
193254 } , 0 ) ;
194- return modulesSize * ( this . initial ? ENTRY_CHUNK_MULTIPLICATOR : 1 ) + CHUNK_OVERHEAD ;
255+ return modulesSize * ( this . isInitial ( ) ? ENTRY_CHUNK_MULTIPLICATOR : 1 ) + CHUNK_OVERHEAD ;
195256} ;
196257
197258Chunk . prototype . canBeIntegrated = function ( other ) {
198- if ( other . initial ) {
259+ if ( other . isInitial ( ) ) {
199260 return false ;
200261 }
201- if ( this . initial ) {
262+ if ( this . isInitial ( ) ) {
202263 if ( other . parents . length !== 1 || other . parents [ 0 ] !== this ) {
203264 return false ;
204265 }
@@ -225,23 +286,17 @@ Chunk.prototype.integratedSize = function(other, options) {
225286 var modulesSize = mergedModules . reduce ( function ( a , m ) {
226287 return a + m . size ( ) ;
227288 } , 0 ) ;
228- return modulesSize * ( this . initial || other . initial ? ENTRY_CHUNK_MULTIPLICATOR : 1 ) + CHUNK_OVERHEAD ;
289+ return modulesSize * ( this . isInitial ( ) || other . isInitial ( ) ? ENTRY_CHUNK_MULTIPLICATOR : 1 ) + CHUNK_OVERHEAD ;
229290} ;
230291
231- Chunk . prototype . hasEntryModule = function ( ) {
232- return this . modules . some ( function ( module ) {
233- return module . entry ;
234- } ) ;
235- }
236-
237292Chunk . prototype . getChunkMaps = function ( includeEntries , realHash ) {
238293 var chunksProcessed = [ ] ;
239294 var chunkHashMap = { } ;
240295 var chunkNameMap = { } ;
241296 ( function addChunk ( c ) {
242297 if ( chunksProcessed . indexOf ( c ) >= 0 ) return ;
243298 chunksProcessed . push ( c ) ;
244- if ( ! c . entry || includeEntries ) {
299+ if ( ! c . hasRuntime ( ) || includeEntries ) {
245300 chunkHashMap [ c . id ] = realHash ? c . hash : c . renderedHash ;
246301 if ( c . name )
247302 chunkNameMap [ c . id ] = c . name ;
@@ -254,6 +309,25 @@ Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
254309 } ;
255310} ;
256311
312+ function byId ( a , b ) {
313+ return a . id - b . id ;
314+ }
315+
316+ Chunk . prototype . sortItems = function ( ) {
317+ this . modules . sort ( byId ) ;
318+ this . origins . sort ( function ( a , b ) {
319+ var aIdent = a . module . identifier ( ) ;
320+ var bIdent = b . module . identifier ( ) ;
321+ if ( aIdent < bIdent ) return - 1 ;
322+ if ( aIdent > bIdent ) return 1 ;
323+ return compareLocations ( a . loc , b . loc ) ;
324+ } ) ;
325+ this . origins . forEach ( function ( origin ) {
326+ if ( origin . reasons )
327+ origin . reasons . sort ( ) ;
328+ } ) ;
329+ } ;
330+
257331Chunk . prototype . toString = function ( ) {
258332 return "Chunk[" + this . modules . join ( ) + "]" ;
259333} ;
0 commit comments