NaturalChunkOrderPlugin.js
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/** @typedef {import("../Compiler")} Compiler */
class NaturalChunkOrderPlugin {
/**
* @param {Compiler} compiler webpack compiler
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("NaturalChunkOrderPlugin", compilation => {
compilation.hooks.optimizeChunkOrder.tap(
"NaturalChunkOrderPlugin",
chunks => {
chunks.sort((chunkA, chunkB) => {
const a = chunkA.modulesIterable[Symbol.iterator]();
const b = chunkB.modulesIterable[Symbol.iterator]();
// eslint-disable-next-line no-constant-condition
while (true) {
const aItem = a.next();
const bItem = b.next();
if (aItem.done && bItem.done) return 0;
if (aItem.done) return -1;
if (bItem.done) return 1;
const aModuleId = aItem.value.id;
const bModuleId = bItem.value.id;
if (aModuleId < bModuleId) return -1;
if (aModuleId > bModuleId) return 1;
}
});
}
);
});
}
}
module.exports = NaturalChunkOrderPlugin;