source: imaps-frontend/node_modules/webpack/lib/ids/OccurrenceChunkIdsPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { compareChunksNatural } = require("../util/comparators");
9const createSchemaValidation = require("../util/create-schema-validation");
10const { assignAscendingChunkIds } = require("./IdHelpers");
11
12/** @typedef {import("../../declarations/plugins/ids/OccurrenceChunkIdsPlugin").OccurrenceChunkIdsPluginOptions} OccurrenceChunkIdsPluginOptions */
13/** @typedef {import("../Chunk")} Chunk */
14/** @typedef {import("../Compiler")} Compiler */
15/** @typedef {import("../Module")} Module */
16
17const validate = createSchemaValidation(
18 require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.check.js"),
19 () => require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json"),
20 {
21 name: "Occurrence Order Chunk Ids Plugin",
22 baseDataPath: "options"
23 }
24);
25
26class OccurrenceChunkIdsPlugin {
27 /**
28 * @param {OccurrenceChunkIdsPluginOptions=} options options object
29 */
30 constructor(options = {}) {
31 validate(options);
32 this.options = options;
33 }
34
35 /**
36 * Apply the plugin
37 * @param {Compiler} compiler the compiler instance
38 * @returns {void}
39 */
40 apply(compiler) {
41 const prioritiseInitial = this.options.prioritiseInitial;
42 compiler.hooks.compilation.tap("OccurrenceChunkIdsPlugin", compilation => {
43 compilation.hooks.chunkIds.tap("OccurrenceChunkIdsPlugin", chunks => {
44 const chunkGraph = compilation.chunkGraph;
45
46 /** @type {Map<Chunk, number>} */
47 const occursInInitialChunksMap = new Map();
48
49 const compareNatural = compareChunksNatural(chunkGraph);
50
51 for (const c of chunks) {
52 let occurs = 0;
53 for (const chunkGroup of c.groupsIterable) {
54 for (const parent of chunkGroup.parentsIterable) {
55 if (parent.isInitial()) occurs++;
56 }
57 }
58 occursInInitialChunksMap.set(c, occurs);
59 }
60
61 const chunksInOccurrenceOrder = Array.from(chunks).sort((a, b) => {
62 if (prioritiseInitial) {
63 const aEntryOccurs =
64 /** @type {number} */
65 (occursInInitialChunksMap.get(a));
66 const bEntryOccurs =
67 /** @type {number} */
68 (occursInInitialChunksMap.get(b));
69 if (aEntryOccurs > bEntryOccurs) return -1;
70 if (aEntryOccurs < bEntryOccurs) return 1;
71 }
72 const aOccurs = a.getNumberOfGroups();
73 const bOccurs = b.getNumberOfGroups();
74 if (aOccurs > bOccurs) return -1;
75 if (aOccurs < bOccurs) return 1;
76 return compareNatural(a, b);
77 });
78 assignAscendingChunkIds(chunksInOccurrenceOrder, compilation);
79 });
80 });
81 }
82}
83
84module.exports = OccurrenceChunkIdsPlugin;
Note: See TracBrowser for help on using the repository browser.