DeterministicChunkIdsPlugin.js
1.65 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
const { compareChunksNatural } = require("../util/comparators");
const {
getFullChunkName,
getUsedChunkIds,
assignDeterministicIds
} = require("./IdHelpers");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class DeterministicChunkIdsPlugin {
constructor(options) {
this.options = options || {};
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"DeterministicChunkIdsPlugin",
compilation => {
compilation.hooks.chunkIds.tap(
"DeterministicChunkIdsPlugin",
chunks => {
const chunkGraph = compilation.chunkGraph;
const context = this.options.context
? this.options.context
: compiler.context;
const maxLength = this.options.maxLength || 3;
const compareNatural = compareChunksNatural(chunkGraph);
const usedIds = getUsedChunkIds(compilation);
assignDeterministicIds(
Array.from(chunks).filter(chunk => {
return chunk.id === null;
}),
chunk =>
getFullChunkName(chunk, chunkGraph, context, compiler.root),
compareNatural,
(chunk, id) => {
const size = usedIds.size;
usedIds.add(`${id}`);
if (size === usedIds.size) return false;
chunk.id = id;
chunk.ids = [id];
return true;
},
[Math.pow(10, maxLength)],
10,
usedIds.size
);
}
);
}
);
}
}
module.exports = DeterministicChunkIdsPlugin;