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