diff --git a/src/index.js b/src/index.js index cec08e21..fdb2b2aa 100644 --- a/src/index.js +++ b/src/index.js @@ -421,7 +421,7 @@ class MiniCssExtractPlugin { // This loop also gathers dependencies from the ordered lists // Lists are in reverse order to allow to use Array.pop() const modulesByChunkGroup = Array.from(chunk.groupsIterable, (cg) => { - const sortedModules = modules + let sortedModules = modules .map((m) => { return { module: m, @@ -433,6 +433,18 @@ class MiniCssExtractPlugin { .sort((a, b) => b.index - a.index) .map((item) => item.module); + // if no modules were found by getModuleIndex2, dive into each chunk + // in the group + if (!sortedModules || !sortedModules.length) { + sortedModules = cg.chunks + // reduce each chunk's modules into a flat array + .reduce((arr, ch) => [...arr, ...ch.modulesIterable], []) + // filter only the modules that match + .filter((m) => modules.find((mod) => mod === m)) + // sort in reverse order + .sort((a, b) => a.index2 - b.index2); + } + for (let i = 0; i < sortedModules.length; i++) { const set = moduleDependencies.get(sortedModules[i]);