Skip to content

Commit 52eb7e0

Browse files
authored
Merge pull request #29 from eladchen/master
Align with webpack api.
2 parents f93375d + 41feeb2 commit 52eb7e0

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

index.js

+56-10
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,51 @@ var pluginSchema = require('./schema/plugin-schema.json');
1717
var NS = fs.realpathSync(__dirname);
1818
var nextId = 0;
1919

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+
2061
function ExtractTextPluginCompilation() {
2162
this.modulesByIdentifier = {};
2263
}
2364

24-
2565
ExtractTextPluginCompilation.prototype.addModule = function(identifier, originalModule, source, additionalInformation, sourceMap, prevModules) {
2666
var m;
2767
if(!this.modulesByIdentifier[identifier]) {
@@ -53,11 +93,15 @@ ExtractTextPluginCompilation.prototype.addResultToChunk = function(identifier, r
5393
};
5494

5595
ExtractTextPlugin.prototype.renderExtractedChunk = function(chunk) {
96+
var that = this;
5697
var source = new ConcatSource();
57-
chunk.modules.forEach(function(module) {
98+
99+
forEachChunkModule( chunk, function ( module ) {
58100
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+
61105
return source;
62106
};
63107

@@ -251,7 +295,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
251295
// SETTING THIS TO TRUE INSURES ALL CHUNKS ARE HANDLED:
252296
var shouldExtract = true; //!!(options.allChunks || chunk.isInitial());
253297

254-
async.forEach(chunk.modules.slice(), function(module, callback) {
298+
async.forEach(getChunkModulesArray(chunk), function(module, callback) {
255299
var meta = module[NS];
256300
if(meta && (!meta.options.id || meta.options.id === id)) {
257301
var wasExtracted = Array.isArray(meta.content);
@@ -294,7 +338,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
294338
if (process.env.NODE_ENV === 'development') {
295339
compilation.plugin("before-chunk-assets", function() {
296340
extractedChunks.forEach(function(extractedChunk) {
297-
extractedChunk.modules.forEach(function(module) {
341+
forEachChunkModule(extractedChunk, function(module) {
298342
if(module.__fileInjected) return;
299343
module.__fileInjected = true;
300344

@@ -308,9 +352,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
308352

309353
// add the css files to assets and the files array corresponding to its chunks
310354
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) {
314360
if(!options.ignoreOrder && isInvalidOrder(a, b)) {
315361
compilation.errors.push(new OrderUndefinedError(a.getOriginalModule()));
316362
compilation.errors.push(new OrderUndefinedError(b.getOriginalModule()));
@@ -320,7 +366,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
320366

321367
var stylesheet = this.renderExtractedChunk(extractedChunk);
322368
var chunk = extractedChunk.originalChunk;
323-
var file = getFile(compilation, filename, stylesheet, chunk)
369+
var file = getFile(compilation, filename, stylesheet, chunk);
324370

325371
compilation.assets[file] = stylesheet;
326372
chunk.files.push(file);

0 commit comments

Comments
 (0)