Update ChunkModuleIdRangePlugin to webpack 4 API

This commit is contained in:
Florent Cailhol 2018-04-12 10:08:25 +02:00
parent b8b95cfebc
commit 36d576cc28
1 changed files with 21 additions and 13 deletions

View File

@ -3,10 +3,20 @@
Author Tobias Koppers @sokra Author Tobias Koppers @sokra
*/ */
"use strict"; "use strict";
const sortByIndex = (a, b) => {
return a.index - b.index;
};
const sortByIndex2 = (a, b) => {
return a.index2 - b.index2;
};
class ChunkModuleIdRangePlugin { class ChunkModuleIdRangePlugin {
constructor(options) { constructor(options) {
this.options = options; this.options = options;
} }
apply(compiler) { apply(compiler) {
const options = this.options; const options = this.options;
compiler.hooks.compilation.tap("ChunkModuleIdRangePlugin", compilation => { compiler.hooks.compilation.tap("ChunkModuleIdRangePlugin", compilation => {
@ -14,26 +24,23 @@ class ChunkModuleIdRangePlugin {
const chunk = compilation.chunks.find( const chunk = compilation.chunks.find(
chunk => chunk.name === options.name chunk => chunk.name === options.name
); );
if (!chunk) if (!chunk) {
throw new Error( throw new Error(
"ChunkModuleIdRangePlugin: Chunk with name '" + `ChunkModuleIdRangePlugin: Chunk with name '${
options.name + options.name
"' was not found" }"' was not found`
); );
let currentId = options.start; }
let chunkModules; let chunkModules;
if (options.order) { if (options.order) {
chunkModules = chunk.modules.slice(); chunkModules = Array.from(chunk.modulesIterable);
switch (options.order) { switch (options.order) {
case "index": case "index":
chunkModules.sort((a, b) => { chunkModules.sort(sortByIndex);
return a.index - b.index;
});
break; break;
case "index2": case "index2":
chunkModules.sort((a, b) => { chunkModules.sort(sortByIndex2);
return a.index2 - b.index2;
});
break; break;
default: default:
throw new Error( throw new Error(
@ -42,10 +49,11 @@ class ChunkModuleIdRangePlugin {
} }
} else { } else {
chunkModules = modules.filter(m => { chunkModules = modules.filter(m => {
return m.chunks.includes(chunk); return m.chunksIterable.has(chunk);
}); });
} }
let currentId = options.start || 0;
for (let i = 0; i < chunkModules.length; i++) { for (let i = 0; i < chunkModules.length; i++) {
const m = chunkModules[i]; const m = chunkModules[i];
if (m.id === null) { if (m.id === null) {