Skip to content

Commit 4b3b0a5

Browse files
Support webpack@4.
Several of webpack's internal functions have changed. This changed functionality has been incorporated into the plugin.
1 parent 2e2bcc3 commit 4b3b0a5

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

index.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,14 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
222222
var id = this.id;
223223
var extractedChunks, entryChunks, initialChunks;
224224
compilation.plugin("optimize-tree", function(chunks, modules, callback) {
225-
extractedChunks = chunks.map(function() {
226-
return new Chunk();
225+
extractedChunks = chunks.map(function(chunk) {
226+
return new Chunk(chunk.name);
227227
});
228228
chunks.forEach(function(chunk, i) {
229229
var extractedChunk = extractedChunks[i];
230-
extractedChunk.index = i;
231-
extractedChunk.originalChunk = chunk;
232230
extractedChunk.name = chunk.name;
233-
extractedChunk.entrypoints = chunk.entrypoints;
234-
chunk.chunks.forEach(function(c) {
235-
extractedChunk.addChunk(extractedChunks[chunks.indexOf(c)]);
236-
});
237-
chunk.parents.forEach(function(c) {
238-
extractedChunk.addParent(extractedChunks[chunks.indexOf(c)]);
239-
});
231+
extractedChunk.originalChunk = chunk;
232+
splitChunk(chunk, extractedChunk);
240233
});
241234
async.forEach(chunks, function(chunk, callback) {
242235
var extractedChunk = extractedChunks[chunks.indexOf(chunk)];
@@ -355,32 +348,53 @@ function isChunk(chunk, error) {
355348
function forEachChunkModule(chunk, cb) {
356349
isChunk(chunk);
357350

351+
// webpack >= 4.x.x
352+
if (chunk.modulesIterable) {
353+
Array.from(chunk.modulesIterable, (x) => {
354+
cb(x);
355+
return x;
356+
})
357+
}
358358
// webpack >= 3.x.x
359-
if (typeof chunk.forEachModule === 'function') {
359+
else if (typeof chunk.forEachModule === 'function') {
360360
chunk.forEachModule(cb);
361361
}
362+
// webpack < 3.x.x
362363
else {
363-
// webpack < 3.x.x
364364
chunk.modules.forEach(cb);
365365
}
366-
367-
// Nothing better to return...
368-
return chunk;
369366
}
370367

371368
function getChunkModulesArray(chunk) {
372369
isChunk(chunk);
373370

374-
var arr = [];
375-
371+
// webpack >= 4.x.x
372+
if (chunk.modulesIterable) {
373+
return Array.from(chunk.modulesIterable);
374+
}
376375
// webpack >= 3.x.x
377-
if ( typeof chunk.mapModules === 'function' ) {
378-
arr = chunk.mapModules();
376+
else if ( typeof chunk.mapModules === 'function' ) {
377+
return chunk.mapModules();
379378
}
380379
else {
381380
// webpack < 3.x.x
382-
arr = chunk.modules.slice();
381+
return chunk.modules.slice();
383382
}
383+
}
384384

385-
return arr;
385+
function splitChunk(chunk, extractedChunk) {
386+
// webpack >= 4.x.x
387+
if (typeof chunk.split === 'function') {
388+
chunk.split(extractedChunk);
389+
}
390+
// webpack < 4.x.x
391+
else {
392+
extractedChunk.entrypoints = chunk.entrypoints;
393+
chunk.chunks.forEach(function(c) {
394+
extractedChunk.addChunk(extractedChunks[chunks.indexOf(c)]);
395+
});
396+
chunk.parents.forEach(function(c) {
397+
extractedChunk.addParent(extractedChunks[chunks.indexOf(c)]);
398+
});
399+
}
386400
}

0 commit comments

Comments
 (0)