@@ -17,11 +17,51 @@ var pluginSchema = require('./schema/plugin-schema.json');
17
17
var NS = fs . realpathSync ( __dirname ) ;
18
18
var nextId = 0 ;
19
19
20
+ function isChunk ( chunk , error ) {
21
+ if ( ! ( chunk instanceof Chunk ) ) {
22
+ throw new Error ( typeof error === 'string' ? error : 'chunk is not an instance of Chunk' ) ;
23
+ }
24
+
25
+ return true ;
26
+ }
27
+
28
+ function forEachChunkModule ( chunk , cb ) {
29
+ isChunk ( chunk ) ;
30
+
31
+ // webpack >= 3.x.x
32
+ if ( typeof chunk . forEachModule === 'function' ) {
33
+ cb . forEachModule ( cb ) ;
34
+ }
35
+ else {
36
+ // webpack < 3.x.x
37
+ chunk . modules . forEach ( cb ) ;
38
+ }
39
+
40
+ // Nothing better to return...
41
+ return chunk ;
42
+ }
43
+
44
+ function getChunkModulesArray ( chunk ) {
45
+ isChunk ( chunk ) ;
46
+
47
+ var arr = [ ] ;
48
+
49
+ // webpack >= 3.x.x
50
+ if ( typeof chunk . mapModules === 'function' ) {
51
+ arr = chunk . mapModules ( ) ;
52
+ }
53
+ else {
54
+ // webpack < 3.x.x
55
+ arr = chunk . modules . slice ( ) ;
56
+ }
57
+
58
+ return arr ;
59
+ }
60
+
20
61
function ExtractTextPluginCompilation ( ) {
21
62
this . modulesByIdentifier = { } ;
22
63
}
23
64
24
-
25
65
ExtractTextPluginCompilation . prototype . addModule = function ( identifier , originalModule , source , additionalInformation , sourceMap , prevModules ) {
26
66
var m ;
27
67
if ( ! this . modulesByIdentifier [ identifier ] ) {
@@ -53,11 +93,15 @@ ExtractTextPluginCompilation.prototype.addResultToChunk = function(identifier, r
53
93
} ;
54
94
55
95
ExtractTextPlugin . prototype . renderExtractedChunk = function ( chunk ) {
96
+ var that = this ;
56
97
var source = new ConcatSource ( ) ;
57
- chunk . modules . forEach ( function ( module ) {
98
+
99
+ forEachChunkModule ( chunk , function ( module ) {
58
100
var moduleSource = module . source ( ) ;
59
- source . add ( this . applyAdditionalInformation ( moduleSource , module . additionalInformation ) ) ;
60
- } , this ) ;
101
+
102
+ source . add ( that . applyAdditionalInformation ( moduleSource , module . additionalInformation ) ) ;
103
+ } ) ;
104
+
61
105
return source ;
62
106
} ;
63
107
@@ -251,7 +295,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
251
295
// SETTING THIS TO TRUE INSURES ALL CHUNKS ARE HANDLED:
252
296
var shouldExtract = true ; //!!(options.allChunks || chunk.isInitial());
253
297
254
- async . forEach ( chunk . modules . slice ( ) , function ( module , callback ) {
298
+ async . forEach ( getChunkModulesArray ( chunk ) , function ( module , callback ) {
255
299
var meta = module [ NS ] ;
256
300
if ( meta && ( ! meta . options . id || meta . options . id === id ) ) {
257
301
var wasExtracted = Array . isArray ( meta . content ) ;
@@ -294,7 +338,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
294
338
if ( process . env . NODE_ENV === 'development' ) {
295
339
compilation . plugin ( "before-chunk-assets" , function ( ) {
296
340
extractedChunks . forEach ( function ( extractedChunk ) {
297
- extractedChunk . modules . forEach ( function ( module ) {
341
+ forEachChunkModule ( extractedChunk , function ( module ) {
298
342
if ( module . __fileInjected ) return ;
299
343
module . __fileInjected = true ;
300
344
@@ -308,9 +352,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
308
352
309
353
// add the css files to assets and the files array corresponding to its chunks
310
354
compilation . plugin ( "additional-assets" , function ( callback ) {
311
- extractedChunks . forEach ( function ( extractedChunk ) {
312
- if ( extractedChunk . modules . length ) {
313
- extractedChunk . modules . sort ( function ( a , b ) {
355
+ extractedChunks . forEach ( function ( extractedChunk ) {
356
+ var extractedChunkModules = getChunkModulesArray ( extractedChunk ) ;
357
+
358
+ if ( extractedChunkModules . length ) {
359
+ extractedChunkModules . sort ( function ( a , b ) {
314
360
if ( ! options . ignoreOrder && isInvalidOrder ( a , b ) ) {
315
361
compilation . errors . push ( new OrderUndefinedError ( a . getOriginalModule ( ) ) ) ;
316
362
compilation . errors . push ( new OrderUndefinedError ( b . getOriginalModule ( ) ) ) ;
@@ -320,7 +366,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
320
366
321
367
var stylesheet = this . renderExtractedChunk ( extractedChunk ) ;
322
368
var chunk = extractedChunk . originalChunk ;
323
- var file = getFile ( compilation , filename , stylesheet , chunk )
369
+ var file = getFile ( compilation , filename , stylesheet , chunk ) ;
324
370
325
371
compilation . assets [ file ] = stylesheet ;
326
372
chunk . files . push ( file ) ;
0 commit comments