UNPKG

1.69 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Florent Cailhol @ooflorent
4*/
5
6"use strict";
7
8const { compareChunksNatural } = require("../util/comparators");
9const {
10 getFullChunkName,
11 getUsedChunkIds,
12 assignDeterministicIds
13} = require("./IdHelpers");
14
15/** @typedef {import("../Compiler")} Compiler */
16/** @typedef {import("../Module")} Module */
17
18class DeterministicChunkIdsPlugin {
19 constructor(options) {
20 this.options = options || {};
21 }
22
23 /**
24 * Apply the plugin
25 * @param {Compiler} compiler the compiler instance
26 * @returns {void}
27 */
28 apply(compiler) {
29 compiler.hooks.compilation.tap(
30 "DeterministicChunkIdsPlugin",
31 compilation => {
32 compilation.hooks.chunkIds.tap(
33 "DeterministicChunkIdsPlugin",
34 chunks => {
35 const chunkGraph = compilation.chunkGraph;
36 const context = this.options.context
37 ? this.options.context
38 : compiler.context;
39 const maxLength = this.options.maxLength || 3;
40
41 const compareNatural = compareChunksNatural(chunkGraph);
42
43 const usedIds = getUsedChunkIds(compilation);
44 assignDeterministicIds(
45 Array.from(chunks).filter(chunk => {
46 return chunk.id === null;
47 }),
48 chunk =>
49 getFullChunkName(chunk, chunkGraph, context, compiler.root),
50 compareNatural,
51 (chunk, id) => {
52 const size = usedIds.size;
53 usedIds.add(`${id}`);
54 if (size === usedIds.size) return false;
55 chunk.id = id;
56 chunk.ids = [id];
57 return true;
58 },
59 [Math.pow(10, maxLength)],
60 10,
61 usedIds.size
62 );
63 }
64 );
65 }
66 );
67 }
68}
69
70module.exports = DeterministicChunkIdsPlugin;