| 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 { compareNumbers } = require("./util/comparators");
|
|---|
| 9 | const identifierUtils = require("./util/identifier");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("./Chunk")} Chunk */
|
|---|
| 12 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 13 | /** @typedef {import("./Module")} Module */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Defines the records chunks type used by this module.
|
|---|
| 17 | * @typedef {object} RecordsChunks
|
|---|
| 18 | * @property {Record<string, number>=} byName
|
|---|
| 19 | * @property {Record<string, number>=} bySource
|
|---|
| 20 | * @property {number[]=} usedIds
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Defines the records modules type used by this module.
|
|---|
| 25 | * @typedef {object} RecordsModules
|
|---|
| 26 | * @property {Record<string, number>=} byIdentifier
|
|---|
| 27 | * @property {number[]=} usedIds
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Defines the records type used by this module.
|
|---|
| 32 | * @typedef {object} Records
|
|---|
| 33 | * @property {RecordsChunks=} chunks
|
|---|
| 34 | * @property {RecordsModules=} modules
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Defines the record ids plugin options type used by this module.
|
|---|
| 39 | * @typedef {object} RecordIdsPluginOptions
|
|---|
| 40 | * @property {boolean=} portableIds true, when ids need to be portable
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | /** @typedef {Set<number>} UsedIds */
|
|---|
| 44 |
|
|---|
| 45 | const PLUGIN_NAME = "RecordIdsPlugin";
|
|---|
| 46 |
|
|---|
| 47 | class RecordIdsPlugin {
|
|---|
| 48 | /**
|
|---|
| 49 | * Creates an instance of RecordIdsPlugin.
|
|---|
| 50 | * @param {RecordIdsPluginOptions=} options object
|
|---|
| 51 | */
|
|---|
| 52 | constructor(options) {
|
|---|
| 53 | this.options = options || {};
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 58 | * @param {Compiler} compiler the Compiler
|
|---|
| 59 | * @returns {void}
|
|---|
| 60 | */
|
|---|
| 61 | apply(compiler) {
|
|---|
| 62 | const portableIds = this.options.portableIds;
|
|---|
| 63 |
|
|---|
| 64 | const makePathsRelative =
|
|---|
| 65 | identifierUtils.makePathsRelative.bindContextCache(
|
|---|
| 66 | compiler.context,
|
|---|
| 67 | compiler.root
|
|---|
| 68 | );
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Gets module identifier.
|
|---|
| 72 | * @param {Module} module the module
|
|---|
| 73 | * @returns {string} the (portable) identifier
|
|---|
| 74 | */
|
|---|
| 75 | const getModuleIdentifier = (module) => {
|
|---|
| 76 | if (portableIds) {
|
|---|
| 77 | return makePathsRelative(module.identifier());
|
|---|
| 78 | }
|
|---|
| 79 | return module.identifier();
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 83 | compilation.hooks.recordModules.tap(PLUGIN_NAME, (modules, records) => {
|
|---|
| 84 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 85 | if (!records.modules) records.modules = {};
|
|---|
| 86 | if (!records.modules.byIdentifier) records.modules.byIdentifier = {};
|
|---|
| 87 | /** @type {UsedIds} */
|
|---|
| 88 | const usedIds = new Set();
|
|---|
| 89 | for (const module of modules) {
|
|---|
| 90 | const moduleId = chunkGraph.getModuleId(module);
|
|---|
| 91 | if (typeof moduleId !== "number") continue;
|
|---|
| 92 | const identifier = getModuleIdentifier(module);
|
|---|
| 93 | records.modules.byIdentifier[identifier] = moduleId;
|
|---|
| 94 | usedIds.add(moduleId);
|
|---|
| 95 | }
|
|---|
| 96 | records.modules.usedIds = [...usedIds].sort(compareNumbers);
|
|---|
| 97 | });
|
|---|
| 98 | compilation.hooks.reviveModules.tap(PLUGIN_NAME, (modules, records) => {
|
|---|
| 99 | if (!records.modules) return;
|
|---|
| 100 | if (records.modules.byIdentifier) {
|
|---|
| 101 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 102 | /** @type {UsedIds} */
|
|---|
| 103 | const usedIds = new Set();
|
|---|
| 104 | for (const module of modules) {
|
|---|
| 105 | const moduleId = chunkGraph.getModuleId(module);
|
|---|
| 106 | if (moduleId !== null) continue;
|
|---|
| 107 | const identifier = getModuleIdentifier(module);
|
|---|
| 108 | const id = records.modules.byIdentifier[identifier];
|
|---|
| 109 | if (id === undefined) continue;
|
|---|
| 110 | if (usedIds.has(id)) continue;
|
|---|
| 111 | usedIds.add(id);
|
|---|
| 112 | chunkGraph.setModuleId(module, id);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 | if (Array.isArray(records.modules.usedIds)) {
|
|---|
| 116 | compilation.usedModuleIds = new Set(records.modules.usedIds);
|
|---|
| 117 | }
|
|---|
| 118 | });
|
|---|
| 119 |
|
|---|
| 120 | /** @typedef {string[]} ChunkSources */
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Gets chunk sources.
|
|---|
| 124 | * @param {Chunk} chunk the chunk
|
|---|
| 125 | * @returns {ChunkSources} sources of the chunk
|
|---|
| 126 | */
|
|---|
| 127 | const getChunkSources = (chunk) => {
|
|---|
| 128 | /** @type {ChunkSources} */
|
|---|
| 129 | const sources = [];
|
|---|
| 130 | for (const chunkGroup of chunk.groupsIterable) {
|
|---|
| 131 | const index = chunkGroup.chunks.indexOf(chunk);
|
|---|
| 132 | if (chunkGroup.name) {
|
|---|
| 133 | sources.push(`${index} ${chunkGroup.name}`);
|
|---|
| 134 | } else {
|
|---|
| 135 | for (const origin of chunkGroup.origins) {
|
|---|
| 136 | if (origin.module) {
|
|---|
| 137 | if (origin.request) {
|
|---|
| 138 | sources.push(
|
|---|
| 139 | `${index} ${getModuleIdentifier(origin.module)} ${
|
|---|
| 140 | origin.request
|
|---|
| 141 | }`
|
|---|
| 142 | );
|
|---|
| 143 | } else if (typeof origin.loc === "string") {
|
|---|
| 144 | sources.push(
|
|---|
| 145 | `${index} ${getModuleIdentifier(origin.module)} ${
|
|---|
| 146 | origin.loc
|
|---|
| 147 | }`
|
|---|
| 148 | );
|
|---|
| 149 | } else if (
|
|---|
| 150 | origin.loc &&
|
|---|
| 151 | typeof origin.loc === "object" &&
|
|---|
| 152 | "start" in origin.loc
|
|---|
| 153 | ) {
|
|---|
| 154 | sources.push(
|
|---|
| 155 | `${index} ${getModuleIdentifier(
|
|---|
| 156 | origin.module
|
|---|
| 157 | )} ${JSON.stringify(origin.loc.start)}`
|
|---|
| 158 | );
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | return sources;
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 | compilation.hooks.recordChunks.tap(PLUGIN_NAME, (chunks, records) => {
|
|---|
| 168 | if (!records.chunks) records.chunks = {};
|
|---|
| 169 | if (!records.chunks.byName) records.chunks.byName = {};
|
|---|
| 170 | if (!records.chunks.bySource) records.chunks.bySource = {};
|
|---|
| 171 | /** @type {UsedIds} */
|
|---|
| 172 | const usedIds = new Set();
|
|---|
| 173 | for (const chunk of chunks) {
|
|---|
| 174 | if (typeof chunk.id !== "number") continue;
|
|---|
| 175 | const name = chunk.name;
|
|---|
| 176 | if (name) records.chunks.byName[name] = chunk.id;
|
|---|
| 177 | const sources = getChunkSources(chunk);
|
|---|
| 178 | for (const source of sources) {
|
|---|
| 179 | records.chunks.bySource[source] = chunk.id;
|
|---|
| 180 | }
|
|---|
| 181 | usedIds.add(chunk.id);
|
|---|
| 182 | }
|
|---|
| 183 | records.chunks.usedIds = [...usedIds].sort(compareNumbers);
|
|---|
| 184 | });
|
|---|
| 185 | compilation.hooks.reviveChunks.tap(PLUGIN_NAME, (chunks, records) => {
|
|---|
| 186 | if (!records.chunks) return;
|
|---|
| 187 | /** @type {UsedIds} */
|
|---|
| 188 | const usedIds = new Set();
|
|---|
| 189 | if (records.chunks.byName) {
|
|---|
| 190 | for (const chunk of chunks) {
|
|---|
| 191 | if (chunk.id !== null) continue;
|
|---|
| 192 | if (!chunk.name) continue;
|
|---|
| 193 | const id = records.chunks.byName[chunk.name];
|
|---|
| 194 | if (id === undefined) continue;
|
|---|
| 195 | if (usedIds.has(id)) continue;
|
|---|
| 196 | usedIds.add(id);
|
|---|
| 197 | chunk.id = id;
|
|---|
| 198 | chunk.ids = [id];
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | if (records.chunks.bySource) {
|
|---|
| 202 | for (const chunk of chunks) {
|
|---|
| 203 | if (chunk.id !== null) continue;
|
|---|
| 204 | const sources = getChunkSources(chunk);
|
|---|
| 205 | for (const source of sources) {
|
|---|
| 206 | const id = records.chunks.bySource[source];
|
|---|
| 207 | if (id === undefined) continue;
|
|---|
| 208 | if (usedIds.has(id)) continue;
|
|---|
| 209 | usedIds.add(id);
|
|---|
| 210 | chunk.id = id;
|
|---|
| 211 | chunk.ids = [id];
|
|---|
| 212 | break;
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | if (Array.isArray(records.chunks.usedIds)) {
|
|---|
| 217 | compilation.usedChunkIds = new Set(records.chunks.usedIds);
|
|---|
| 218 | }
|
|---|
| 219 | });
|
|---|
| 220 | });
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | module.exports = RecordIdsPlugin;
|
|---|