| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const {
|
|---|
| 9 | compareModulesByPreOrderIndexOrIdentifier
|
|---|
| 10 | } = require("../util/comparators");
|
|---|
| 11 | const {
|
|---|
| 12 | assignAscendingModuleIds,
|
|---|
| 13 | getUsedModuleIdsAndModules
|
|---|
| 14 | } = require("./IdHelpers");
|
|---|
| 15 |
|
|---|
| 16 | /** @typedef {import("../../declarations/plugins/ids/OccurrenceModuleIdsPlugin").OccurrenceModuleIdsPluginOptions} OccurrenceModuleIdsPluginOptions */
|
|---|
| 17 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 18 | /** @typedef {import("../Module")} Module */
|
|---|
| 19 |
|
|---|
| 20 | const PLUGIN_NAME = "OccurrenceModuleIdsPlugin";
|
|---|
| 21 |
|
|---|
| 22 | class OccurrenceModuleIdsPlugin {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of OccurrenceModuleIdsPlugin.
|
|---|
| 25 | * @param {OccurrenceModuleIdsPluginOptions=} options options object
|
|---|
| 26 | */
|
|---|
| 27 | constructor(options = {}) {
|
|---|
| 28 | /** @type {OccurrenceModuleIdsPluginOptions} */
|
|---|
| 29 | this.options = options;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 34 | * @param {Compiler} compiler the compiler instance
|
|---|
| 35 | * @returns {void}
|
|---|
| 36 | */
|
|---|
| 37 | apply(compiler) {
|
|---|
| 38 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 39 | compiler.validate(
|
|---|
| 40 | () =>
|
|---|
| 41 | require("../../schemas/plugins/ids/OccurrenceModuleIdsPlugin.json"),
|
|---|
| 42 | this.options,
|
|---|
| 43 | {
|
|---|
| 44 | name: "Occurrence Order Module Ids Plugin",
|
|---|
| 45 | baseDataPath: "options"
|
|---|
| 46 | },
|
|---|
| 47 | (options) =>
|
|---|
| 48 | require("../../schemas/plugins/ids/OccurrenceModuleIdsPlugin.check")(
|
|---|
| 49 | options
|
|---|
| 50 | )
|
|---|
| 51 | );
|
|---|
| 52 | });
|
|---|
| 53 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 54 | const moduleGraph = compilation.moduleGraph;
|
|---|
| 55 |
|
|---|
| 56 | compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => {
|
|---|
| 57 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 58 |
|
|---|
| 59 | const [usedIds, modulesInOccurrenceOrder] =
|
|---|
| 60 | getUsedModuleIdsAndModules(compilation);
|
|---|
| 61 |
|
|---|
| 62 | /** @type {Map<Module, number>} */
|
|---|
| 63 | const occursInInitialChunksMap = new Map();
|
|---|
| 64 | /** @type {Map<Module, number>} */
|
|---|
| 65 | const occursInAllChunksMap = new Map();
|
|---|
| 66 |
|
|---|
| 67 | /** @type {Map<Module, number>} */
|
|---|
| 68 | const initialChunkChunkMap = new Map();
|
|---|
| 69 | /** @type {Map<Module, number>} */
|
|---|
| 70 | const entryCountMap = new Map();
|
|---|
| 71 | for (const m of modulesInOccurrenceOrder) {
|
|---|
| 72 | let initial = 0;
|
|---|
| 73 | let entry = 0;
|
|---|
| 74 | for (const c of chunkGraph.getModuleChunksIterable(m)) {
|
|---|
| 75 | if (c.canBeInitial()) initial++;
|
|---|
| 76 | if (chunkGraph.isEntryModuleInChunk(m, c)) entry++;
|
|---|
| 77 | }
|
|---|
| 78 | initialChunkChunkMap.set(m, initial);
|
|---|
| 79 | entryCountMap.set(m, entry);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Count occurs in entry.
|
|---|
| 84 | * @param {Module} module module
|
|---|
| 85 | * @returns {number} count of occurs
|
|---|
| 86 | */
|
|---|
| 87 | const countOccursInEntry = (module) => {
|
|---|
| 88 | let sum = 0;
|
|---|
| 89 | for (const [
|
|---|
| 90 | originModule,
|
|---|
| 91 | connections
|
|---|
| 92 | ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
|
|---|
| 93 | if (!originModule) continue;
|
|---|
| 94 | if (!connections.some((c) => c.isTargetActive(undefined))) continue;
|
|---|
| 95 | sum += initialChunkChunkMap.get(originModule) || 0;
|
|---|
| 96 | }
|
|---|
| 97 | return sum;
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Returns count of occurs.
|
|---|
| 102 | * @param {Module} module module
|
|---|
| 103 | * @returns {number} count of occurs
|
|---|
| 104 | */
|
|---|
| 105 | const countOccurs = (module) => {
|
|---|
| 106 | let sum = 0;
|
|---|
| 107 | for (const [
|
|---|
| 108 | originModule,
|
|---|
| 109 | connections
|
|---|
| 110 | ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
|
|---|
| 111 | if (!originModule) continue;
|
|---|
| 112 | const chunkModules =
|
|---|
| 113 | chunkGraph.getNumberOfModuleChunks(originModule);
|
|---|
| 114 | for (const c of connections) {
|
|---|
| 115 | if (!c.isTargetActive(undefined)) continue;
|
|---|
| 116 | if (!c.dependency) continue;
|
|---|
| 117 | const factor = c.dependency.getNumberOfIdOccurrences();
|
|---|
| 118 | if (factor === 0) continue;
|
|---|
| 119 | sum += factor * chunkModules;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | return sum;
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 | if (this.options.prioritiseInitial) {
|
|---|
| 126 | for (const m of modulesInOccurrenceOrder) {
|
|---|
| 127 | const result =
|
|---|
| 128 | countOccursInEntry(m) +
|
|---|
| 129 | /** @type {number} */ (initialChunkChunkMap.get(m)) +
|
|---|
| 130 | /** @type {number} */ (entryCountMap.get(m));
|
|---|
| 131 | occursInInitialChunksMap.set(m, result);
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | for (const m of modulesInOccurrenceOrder) {
|
|---|
| 136 | const result =
|
|---|
| 137 | countOccurs(m) +
|
|---|
| 138 | chunkGraph.getNumberOfModuleChunks(m) +
|
|---|
| 139 | /** @type {number} */ (entryCountMap.get(m));
|
|---|
| 140 | occursInAllChunksMap.set(m, result);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | const naturalCompare = compareModulesByPreOrderIndexOrIdentifier(
|
|---|
| 144 | compilation.moduleGraph
|
|---|
| 145 | );
|
|---|
| 146 |
|
|---|
| 147 | modulesInOccurrenceOrder.sort((a, b) => {
|
|---|
| 148 | if (this.options.prioritiseInitial) {
|
|---|
| 149 | const aEntryOccurs =
|
|---|
| 150 | /** @type {number} */
|
|---|
| 151 | (occursInInitialChunksMap.get(a));
|
|---|
| 152 | const bEntryOccurs =
|
|---|
| 153 | /** @type {number} */
|
|---|
| 154 | (occursInInitialChunksMap.get(b));
|
|---|
| 155 | if (aEntryOccurs > bEntryOccurs) return -1;
|
|---|
| 156 | if (aEntryOccurs < bEntryOccurs) return 1;
|
|---|
| 157 | }
|
|---|
| 158 | const aOccurs = /** @type {number} */ (occursInAllChunksMap.get(a));
|
|---|
| 159 | const bOccurs = /** @type {number} */ (occursInAllChunksMap.get(b));
|
|---|
| 160 | if (aOccurs > bOccurs) return -1;
|
|---|
| 161 | if (aOccurs < bOccurs) return 1;
|
|---|
| 162 | return naturalCompare(a, b);
|
|---|
| 163 | });
|
|---|
| 164 |
|
|---|
| 165 | assignAscendingModuleIds(
|
|---|
| 166 | usedIds,
|
|---|
| 167 | modulesInOccurrenceOrder,
|
|---|
| 168 | compilation
|
|---|
| 169 | );
|
|---|
| 170 | });
|
|---|
| 171 | });
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | module.exports = OccurrenceModuleIdsPlugin;
|
|---|