[79a0317] | 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 ChunkGraph = require("./ChunkGraph");
|
---|
| 9 | const Entrypoint = require("./Entrypoint");
|
---|
| 10 | const { intersect } = require("./util/SetHelpers");
|
---|
| 11 | const SortableSet = require("./util/SortableSet");
|
---|
| 12 | const StringXor = require("./util/StringXor");
|
---|
| 13 | const {
|
---|
| 14 | compareModulesByIdentifier,
|
---|
| 15 | compareChunkGroupsByIndex,
|
---|
| 16 | compareModulesById
|
---|
| 17 | } = require("./util/comparators");
|
---|
| 18 | const { createArrayToSetDeprecationSet } = require("./util/deprecation");
|
---|
| 19 | const { mergeRuntime } = require("./util/runtime");
|
---|
| 20 |
|
---|
| 21 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 22 | /** @typedef {import("./ChunkGraph").ChunkFilterPredicate} ChunkFilterPredicate */
|
---|
| 23 | /** @typedef {import("./ChunkGraph").ChunkSizeOptions} ChunkSizeOptions */
|
---|
| 24 | /** @typedef {import("./ChunkGraph").ModuleFilterPredicate} ModuleFilterPredicate */
|
---|
| 25 | /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
|
---|
| 26 | /** @typedef {import("./ChunkGroup")} ChunkGroup */
|
---|
| 27 | /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
|
---|
| 28 | /** @typedef {import("./Compilation")} Compilation */
|
---|
| 29 | /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
|
---|
| 30 | /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
|
---|
| 31 | /** @typedef {import("./Module")} Module */
|
---|
| 32 | /** @typedef {import("./ModuleGraph")} ModuleGraph */
|
---|
| 33 | /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
|
---|
| 34 | /** @typedef {import("./util/Hash")} Hash */
|
---|
| 35 | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
| 36 |
|
---|
| 37 | /** @typedef {number | string} ChunkId */
|
---|
| 38 |
|
---|
| 39 | const ChunkFilesSet = createArrayToSetDeprecationSet("chunk.files");
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @typedef {object} WithId an object who has an id property *
|
---|
| 43 | * @property {string | number} id the id of the object
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * @deprecated
|
---|
| 48 | * @typedef {object} ChunkMaps
|
---|
| 49 | * @property {Record<string|number, string>} hash
|
---|
| 50 | * @property {Record<string|number, Record<string, string>>} contentHash
|
---|
| 51 | * @property {Record<string|number, string>} name
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * @deprecated
|
---|
| 56 | * @typedef {object} ChunkModuleMaps
|
---|
| 57 | * @property {Record<string|number, (string|number)[]>} id
|
---|
| 58 | * @property {Record<string|number, string>} hash
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 | let debugId = 1000;
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * A Chunk is a unit of encapsulation for Modules.
|
---|
| 65 | * Chunks are "rendered" into bundles that get emitted when the build completes.
|
---|
| 66 | */
|
---|
| 67 | class Chunk {
|
---|
| 68 | /**
|
---|
| 69 | * @param {string=} name of chunk being created, is optional (for subclasses)
|
---|
| 70 | * @param {boolean} backCompat enable backward-compatibility
|
---|
| 71 | */
|
---|
| 72 | constructor(name, backCompat = true) {
|
---|
| 73 | /** @type {ChunkId | null} */
|
---|
| 74 | this.id = null;
|
---|
| 75 | /** @type {ChunkId[] | null} */
|
---|
| 76 | this.ids = null;
|
---|
| 77 | /** @type {number} */
|
---|
| 78 | this.debugId = debugId++;
|
---|
| 79 | /** @type {string | undefined} */
|
---|
| 80 | this.name = name;
|
---|
| 81 | /** @type {SortableSet<string>} */
|
---|
| 82 | this.idNameHints = new SortableSet();
|
---|
| 83 | /** @type {boolean} */
|
---|
| 84 | this.preventIntegration = false;
|
---|
| 85 | /** @type {TemplatePath | undefined} */
|
---|
| 86 | this.filenameTemplate = undefined;
|
---|
| 87 | /** @type {TemplatePath | undefined} */
|
---|
| 88 | this.cssFilenameTemplate = undefined;
|
---|
| 89 | /**
|
---|
| 90 | * @private
|
---|
| 91 | * @type {SortableSet<ChunkGroup>}
|
---|
| 92 | */
|
---|
| 93 | this._groups = new SortableSet(undefined, compareChunkGroupsByIndex);
|
---|
| 94 | /** @type {RuntimeSpec} */
|
---|
| 95 | this.runtime = undefined;
|
---|
| 96 | /** @type {Set<string>} */
|
---|
| 97 | this.files = backCompat ? new ChunkFilesSet() : new Set();
|
---|
| 98 | /** @type {Set<string>} */
|
---|
| 99 | this.auxiliaryFiles = new Set();
|
---|
| 100 | /** @type {boolean} */
|
---|
| 101 | this.rendered = false;
|
---|
| 102 | /** @type {string=} */
|
---|
| 103 | this.hash = undefined;
|
---|
| 104 | /** @type {Record<string, string>} */
|
---|
| 105 | this.contentHash = Object.create(null);
|
---|
| 106 | /** @type {string=} */
|
---|
| 107 | this.renderedHash = undefined;
|
---|
| 108 | /** @type {string=} */
|
---|
| 109 | this.chunkReason = undefined;
|
---|
| 110 | /** @type {boolean} */
|
---|
| 111 | this.extraAsync = false;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // TODO remove in webpack 6
|
---|
| 115 | // BACKWARD-COMPAT START
|
---|
| 116 | get entryModule() {
|
---|
| 117 | const entryModules = Array.from(
|
---|
| 118 | ChunkGraph.getChunkGraphForChunk(
|
---|
| 119 | this,
|
---|
| 120 | "Chunk.entryModule",
|
---|
| 121 | "DEP_WEBPACK_CHUNK_ENTRY_MODULE"
|
---|
| 122 | ).getChunkEntryModulesIterable(this)
|
---|
| 123 | );
|
---|
| 124 | if (entryModules.length === 0) {
|
---|
| 125 | return undefined;
|
---|
| 126 | } else if (entryModules.length === 1) {
|
---|
| 127 | return entryModules[0];
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | throw new Error(
|
---|
| 131 | "Module.entryModule: Multiple entry modules are not supported by the deprecated API (Use the new ChunkGroup API)"
|
---|
| 132 | );
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * @returns {boolean} true, if the chunk contains an entry module
|
---|
| 137 | */
|
---|
| 138 | hasEntryModule() {
|
---|
| 139 | return (
|
---|
| 140 | ChunkGraph.getChunkGraphForChunk(
|
---|
| 141 | this,
|
---|
| 142 | "Chunk.hasEntryModule",
|
---|
| 143 | "DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE"
|
---|
| 144 | ).getNumberOfEntryModules(this) > 0
|
---|
| 145 | );
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * @param {Module} module the module
|
---|
| 150 | * @returns {boolean} true, if the chunk could be added
|
---|
| 151 | */
|
---|
| 152 | addModule(module) {
|
---|
| 153 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 154 | this,
|
---|
| 155 | "Chunk.addModule",
|
---|
| 156 | "DEP_WEBPACK_CHUNK_ADD_MODULE"
|
---|
| 157 | );
|
---|
| 158 | if (chunkGraph.isModuleInChunk(module, this)) return false;
|
---|
| 159 | chunkGraph.connectChunkAndModule(this, module);
|
---|
| 160 | return true;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * @param {Module} module the module
|
---|
| 165 | * @returns {void}
|
---|
| 166 | */
|
---|
| 167 | removeModule(module) {
|
---|
| 168 | ChunkGraph.getChunkGraphForChunk(
|
---|
| 169 | this,
|
---|
| 170 | "Chunk.removeModule",
|
---|
| 171 | "DEP_WEBPACK_CHUNK_REMOVE_MODULE"
|
---|
| 172 | ).disconnectChunkAndModule(this, module);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * @returns {number} the number of module which are contained in this chunk
|
---|
| 177 | */
|
---|
| 178 | getNumberOfModules() {
|
---|
| 179 | return ChunkGraph.getChunkGraphForChunk(
|
---|
| 180 | this,
|
---|
| 181 | "Chunk.getNumberOfModules",
|
---|
| 182 | "DEP_WEBPACK_CHUNK_GET_NUMBER_OF_MODULES"
|
---|
| 183 | ).getNumberOfChunkModules(this);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | get modulesIterable() {
|
---|
| 187 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 188 | this,
|
---|
| 189 | "Chunk.modulesIterable",
|
---|
| 190 | "DEP_WEBPACK_CHUNK_MODULES_ITERABLE"
|
---|
| 191 | );
|
---|
| 192 | return chunkGraph.getOrderedChunkModulesIterable(
|
---|
| 193 | this,
|
---|
| 194 | compareModulesByIdentifier
|
---|
| 195 | );
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
| 199 | * @param {Chunk} otherChunk the chunk to compare with
|
---|
| 200 | * @returns {-1|0|1} the comparison result
|
---|
| 201 | */
|
---|
| 202 | compareTo(otherChunk) {
|
---|
| 203 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 204 | this,
|
---|
| 205 | "Chunk.compareTo",
|
---|
| 206 | "DEP_WEBPACK_CHUNK_COMPARE_TO"
|
---|
| 207 | );
|
---|
| 208 | return chunkGraph.compareChunks(this, otherChunk);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * @param {Module} module the module
|
---|
| 213 | * @returns {boolean} true, if the chunk contains the module
|
---|
| 214 | */
|
---|
| 215 | containsModule(module) {
|
---|
| 216 | return ChunkGraph.getChunkGraphForChunk(
|
---|
| 217 | this,
|
---|
| 218 | "Chunk.containsModule",
|
---|
| 219 | "DEP_WEBPACK_CHUNK_CONTAINS_MODULE"
|
---|
| 220 | ).isModuleInChunk(module, this);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | * @returns {Module[]} the modules for this chunk
|
---|
| 225 | */
|
---|
| 226 | getModules() {
|
---|
| 227 | return ChunkGraph.getChunkGraphForChunk(
|
---|
| 228 | this,
|
---|
| 229 | "Chunk.getModules",
|
---|
| 230 | "DEP_WEBPACK_CHUNK_GET_MODULES"
|
---|
| 231 | ).getChunkModules(this);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * @returns {void}
|
---|
| 236 | */
|
---|
| 237 | remove() {
|
---|
| 238 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 239 | this,
|
---|
| 240 | "Chunk.remove",
|
---|
| 241 | "DEP_WEBPACK_CHUNK_REMOVE"
|
---|
| 242 | );
|
---|
| 243 | chunkGraph.disconnectChunk(this);
|
---|
| 244 | this.disconnectFromGroups();
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /**
|
---|
| 248 | * @param {Module} module the module
|
---|
| 249 | * @param {Chunk} otherChunk the target chunk
|
---|
| 250 | * @returns {void}
|
---|
| 251 | */
|
---|
| 252 | moveModule(module, otherChunk) {
|
---|
| 253 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 254 | this,
|
---|
| 255 | "Chunk.moveModule",
|
---|
| 256 | "DEP_WEBPACK_CHUNK_MOVE_MODULE"
|
---|
| 257 | );
|
---|
| 258 | chunkGraph.disconnectChunkAndModule(this, module);
|
---|
| 259 | chunkGraph.connectChunkAndModule(otherChunk, module);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /**
|
---|
| 263 | * @param {Chunk} otherChunk the other chunk
|
---|
| 264 | * @returns {boolean} true, if the specified chunk has been integrated
|
---|
| 265 | */
|
---|
| 266 | integrate(otherChunk) {
|
---|
| 267 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 268 | this,
|
---|
| 269 | "Chunk.integrate",
|
---|
| 270 | "DEP_WEBPACK_CHUNK_INTEGRATE"
|
---|
| 271 | );
|
---|
| 272 | if (chunkGraph.canChunksBeIntegrated(this, otherChunk)) {
|
---|
| 273 | chunkGraph.integrateChunks(this, otherChunk);
|
---|
| 274 | return true;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | return false;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * @param {Chunk} otherChunk the other chunk
|
---|
| 282 | * @returns {boolean} true, if chunks could be integrated
|
---|
| 283 | */
|
---|
| 284 | canBeIntegrated(otherChunk) {
|
---|
| 285 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 286 | this,
|
---|
| 287 | "Chunk.canBeIntegrated",
|
---|
| 288 | "DEP_WEBPACK_CHUNK_CAN_BE_INTEGRATED"
|
---|
| 289 | );
|
---|
| 290 | return chunkGraph.canChunksBeIntegrated(this, otherChunk);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /**
|
---|
| 294 | * @returns {boolean} true, if this chunk contains no module
|
---|
| 295 | */
|
---|
| 296 | isEmpty() {
|
---|
| 297 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 298 | this,
|
---|
| 299 | "Chunk.isEmpty",
|
---|
| 300 | "DEP_WEBPACK_CHUNK_IS_EMPTY"
|
---|
| 301 | );
|
---|
| 302 | return chunkGraph.getNumberOfChunkModules(this) === 0;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | /**
|
---|
| 306 | * @returns {number} total size of all modules in this chunk
|
---|
| 307 | */
|
---|
| 308 | modulesSize() {
|
---|
| 309 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 310 | this,
|
---|
| 311 | "Chunk.modulesSize",
|
---|
| 312 | "DEP_WEBPACK_CHUNK_MODULES_SIZE"
|
---|
| 313 | );
|
---|
| 314 | return chunkGraph.getChunkModulesSize(this);
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
| 318 | * @param {ChunkSizeOptions} options options object
|
---|
| 319 | * @returns {number} total size of this chunk
|
---|
| 320 | */
|
---|
| 321 | size(options = {}) {
|
---|
| 322 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 323 | this,
|
---|
| 324 | "Chunk.size",
|
---|
| 325 | "DEP_WEBPACK_CHUNK_SIZE"
|
---|
| 326 | );
|
---|
| 327 | return chunkGraph.getChunkSize(this, options);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /**
|
---|
| 331 | * @param {Chunk} otherChunk the other chunk
|
---|
| 332 | * @param {ChunkSizeOptions} options options object
|
---|
| 333 | * @returns {number} total size of the chunk or false if the chunk can't be integrated
|
---|
| 334 | */
|
---|
| 335 | integratedSize(otherChunk, options) {
|
---|
| 336 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 337 | this,
|
---|
| 338 | "Chunk.integratedSize",
|
---|
| 339 | "DEP_WEBPACK_CHUNK_INTEGRATED_SIZE"
|
---|
| 340 | );
|
---|
| 341 | return chunkGraph.getIntegratedChunksSize(this, otherChunk, options);
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /**
|
---|
| 345 | * @param {ModuleFilterPredicate} filterFn function used to filter modules
|
---|
| 346 | * @returns {ChunkModuleMaps} module map information
|
---|
| 347 | */
|
---|
| 348 | getChunkModuleMaps(filterFn) {
|
---|
| 349 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 350 | this,
|
---|
| 351 | "Chunk.getChunkModuleMaps",
|
---|
| 352 | "DEP_WEBPACK_CHUNK_GET_CHUNK_MODULE_MAPS"
|
---|
| 353 | );
|
---|
| 354 | /** @type {Record<string|number, (string|number)[]>} */
|
---|
| 355 | const chunkModuleIdMap = Object.create(null);
|
---|
| 356 | /** @type {Record<string|number, string>} */
|
---|
| 357 | const chunkModuleHashMap = Object.create(null);
|
---|
| 358 |
|
---|
| 359 | for (const asyncChunk of this.getAllAsyncChunks()) {
|
---|
| 360 | /** @type {ChunkId[] | undefined} */
|
---|
| 361 | let array;
|
---|
| 362 | for (const module of chunkGraph.getOrderedChunkModulesIterable(
|
---|
| 363 | asyncChunk,
|
---|
| 364 | compareModulesById(chunkGraph)
|
---|
| 365 | )) {
|
---|
| 366 | if (filterFn(module)) {
|
---|
| 367 | if (array === undefined) {
|
---|
| 368 | array = [];
|
---|
| 369 | chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array;
|
---|
| 370 | }
|
---|
| 371 | const moduleId =
|
---|
| 372 | /** @type {ModuleId} */
|
---|
| 373 | (chunkGraph.getModuleId(module));
|
---|
| 374 | array.push(moduleId);
|
---|
| 375 | chunkModuleHashMap[moduleId] = chunkGraph.getRenderedModuleHash(
|
---|
| 376 | module,
|
---|
| 377 | undefined
|
---|
| 378 | );
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | return {
|
---|
| 384 | id: chunkModuleIdMap,
|
---|
| 385 | hash: chunkModuleHashMap
|
---|
| 386 | };
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /**
|
---|
| 390 | * @param {ModuleFilterPredicate} filterFn predicate function used to filter modules
|
---|
| 391 | * @param {ChunkFilterPredicate=} filterChunkFn predicate function used to filter chunks
|
---|
| 392 | * @returns {boolean} return true if module exists in graph
|
---|
| 393 | */
|
---|
| 394 | hasModuleInGraph(filterFn, filterChunkFn) {
|
---|
| 395 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
---|
| 396 | this,
|
---|
| 397 | "Chunk.hasModuleInGraph",
|
---|
| 398 | "DEP_WEBPACK_CHUNK_HAS_MODULE_IN_GRAPH"
|
---|
| 399 | );
|
---|
| 400 | return chunkGraph.hasModuleInGraph(this, filterFn, filterChunkFn);
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | /**
|
---|
| 404 | * @deprecated
|
---|
| 405 | * @param {boolean} realHash whether the full hash or the rendered hash is to be used
|
---|
| 406 | * @returns {ChunkMaps} the chunk map information
|
---|
| 407 | */
|
---|
| 408 | getChunkMaps(realHash) {
|
---|
| 409 | /** @type {Record<string|number, string>} */
|
---|
| 410 | const chunkHashMap = Object.create(null);
|
---|
| 411 | /** @type {Record<string|number, Record<string, string>>} */
|
---|
| 412 | const chunkContentHashMap = Object.create(null);
|
---|
| 413 | /** @type {Record<string|number, string>} */
|
---|
| 414 | const chunkNameMap = Object.create(null);
|
---|
| 415 |
|
---|
| 416 | for (const chunk of this.getAllAsyncChunks()) {
|
---|
| 417 | const id = /** @type {ChunkId} */ (chunk.id);
|
---|
| 418 | chunkHashMap[id] =
|
---|
| 419 | /** @type {string} */
|
---|
| 420 | (realHash ? chunk.hash : chunk.renderedHash);
|
---|
| 421 | for (const key of Object.keys(chunk.contentHash)) {
|
---|
| 422 | if (!chunkContentHashMap[key]) {
|
---|
| 423 | chunkContentHashMap[key] = Object.create(null);
|
---|
| 424 | }
|
---|
| 425 | chunkContentHashMap[key][id] = chunk.contentHash[key];
|
---|
| 426 | }
|
---|
| 427 | if (chunk.name) {
|
---|
| 428 | chunkNameMap[id] = chunk.name;
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | return {
|
---|
| 433 | hash: chunkHashMap,
|
---|
| 434 | contentHash: chunkContentHashMap,
|
---|
| 435 | name: chunkNameMap
|
---|
| 436 | };
|
---|
| 437 | }
|
---|
| 438 | // BACKWARD-COMPAT END
|
---|
| 439 |
|
---|
| 440 | /**
|
---|
| 441 | * @returns {boolean} whether or not the Chunk will have a runtime
|
---|
| 442 | */
|
---|
| 443 | hasRuntime() {
|
---|
| 444 | for (const chunkGroup of this._groups) {
|
---|
| 445 | if (
|
---|
| 446 | chunkGroup instanceof Entrypoint &&
|
---|
| 447 | chunkGroup.getRuntimeChunk() === this
|
---|
| 448 | ) {
|
---|
| 449 | return true;
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 | return false;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /**
|
---|
| 456 | * @returns {boolean} whether or not this chunk can be an initial chunk
|
---|
| 457 | */
|
---|
| 458 | canBeInitial() {
|
---|
| 459 | for (const chunkGroup of this._groups) {
|
---|
| 460 | if (chunkGroup.isInitial()) return true;
|
---|
| 461 | }
|
---|
| 462 | return false;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | /**
|
---|
| 466 | * @returns {boolean} whether this chunk can only be an initial chunk
|
---|
| 467 | */
|
---|
| 468 | isOnlyInitial() {
|
---|
| 469 | if (this._groups.size <= 0) return false;
|
---|
| 470 | for (const chunkGroup of this._groups) {
|
---|
| 471 | if (!chunkGroup.isInitial()) return false;
|
---|
| 472 | }
|
---|
| 473 | return true;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /**
|
---|
| 477 | * @returns {EntryOptions | undefined} the entry options for this chunk
|
---|
| 478 | */
|
---|
| 479 | getEntryOptions() {
|
---|
| 480 | for (const chunkGroup of this._groups) {
|
---|
| 481 | if (chunkGroup instanceof Entrypoint) {
|
---|
| 482 | return chunkGroup.options;
|
---|
| 483 | }
|
---|
| 484 | }
|
---|
| 485 | return undefined;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | /**
|
---|
| 489 | * @param {ChunkGroup} chunkGroup the chunkGroup the chunk is being added
|
---|
| 490 | * @returns {void}
|
---|
| 491 | */
|
---|
| 492 | addGroup(chunkGroup) {
|
---|
| 493 | this._groups.add(chunkGroup);
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | /**
|
---|
| 497 | * @param {ChunkGroup} chunkGroup the chunkGroup the chunk is being removed from
|
---|
| 498 | * @returns {void}
|
---|
| 499 | */
|
---|
| 500 | removeGroup(chunkGroup) {
|
---|
| 501 | this._groups.delete(chunkGroup);
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | /**
|
---|
| 505 | * @param {ChunkGroup} chunkGroup the chunkGroup to check
|
---|
| 506 | * @returns {boolean} returns true if chunk has chunkGroup reference and exists in chunkGroup
|
---|
| 507 | */
|
---|
| 508 | isInGroup(chunkGroup) {
|
---|
| 509 | return this._groups.has(chunkGroup);
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /**
|
---|
| 513 | * @returns {number} the amount of groups that the said chunk is in
|
---|
| 514 | */
|
---|
| 515 | getNumberOfGroups() {
|
---|
| 516 | return this._groups.size;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /**
|
---|
| 520 | * @returns {SortableSet<ChunkGroup>} the chunkGroups that the said chunk is referenced in
|
---|
| 521 | */
|
---|
| 522 | get groupsIterable() {
|
---|
| 523 | this._groups.sort();
|
---|
| 524 | return this._groups;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | /**
|
---|
| 528 | * @returns {void}
|
---|
| 529 | */
|
---|
| 530 | disconnectFromGroups() {
|
---|
| 531 | for (const chunkGroup of this._groups) {
|
---|
| 532 | chunkGroup.removeChunk(this);
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | /**
|
---|
| 537 | * @param {Chunk} newChunk the new chunk that will be split out of
|
---|
| 538 | * @returns {void}
|
---|
| 539 | */
|
---|
| 540 | split(newChunk) {
|
---|
| 541 | for (const chunkGroup of this._groups) {
|
---|
| 542 | chunkGroup.insertChunk(newChunk, this);
|
---|
| 543 | newChunk.addGroup(chunkGroup);
|
---|
| 544 | }
|
---|
| 545 | for (const idHint of this.idNameHints) {
|
---|
| 546 | newChunk.idNameHints.add(idHint);
|
---|
| 547 | }
|
---|
| 548 | newChunk.runtime = mergeRuntime(newChunk.runtime, this.runtime);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | /**
|
---|
| 552 | * @param {Hash} hash hash (will be modified)
|
---|
| 553 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 554 | * @returns {void}
|
---|
| 555 | */
|
---|
| 556 | updateHash(hash, chunkGraph) {
|
---|
| 557 | hash.update(
|
---|
| 558 | `${this.id} ${this.ids ? this.ids.join() : ""} ${this.name || ""} `
|
---|
| 559 | );
|
---|
| 560 | const xor = new StringXor();
|
---|
| 561 | for (const m of chunkGraph.getChunkModulesIterable(this)) {
|
---|
| 562 | xor.add(chunkGraph.getModuleHash(m, this.runtime));
|
---|
| 563 | }
|
---|
| 564 | xor.updateHash(hash);
|
---|
| 565 | const entryModules =
|
---|
| 566 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(this);
|
---|
| 567 | for (const [m, chunkGroup] of entryModules) {
|
---|
| 568 | hash.update(
|
---|
| 569 | `entry${chunkGraph.getModuleId(m)}${
|
---|
| 570 | /** @type {ChunkGroup} */ (chunkGroup).id
|
---|
| 571 | }`
|
---|
| 572 | );
|
---|
| 573 | }
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | /**
|
---|
| 577 | * @returns {Set<Chunk>} a set of all the async chunks
|
---|
| 578 | */
|
---|
| 579 | getAllAsyncChunks() {
|
---|
| 580 | const queue = new Set();
|
---|
| 581 | const chunks = new Set();
|
---|
| 582 |
|
---|
| 583 | const initialChunks = intersect(
|
---|
| 584 | Array.from(this.groupsIterable, g => new Set(g.chunks))
|
---|
| 585 | );
|
---|
| 586 |
|
---|
| 587 | const initialQueue = new Set(this.groupsIterable);
|
---|
| 588 |
|
---|
| 589 | for (const chunkGroup of initialQueue) {
|
---|
| 590 | for (const child of chunkGroup.childrenIterable) {
|
---|
| 591 | if (child instanceof Entrypoint) {
|
---|
| 592 | initialQueue.add(child);
|
---|
| 593 | } else {
|
---|
| 594 | queue.add(child);
|
---|
| 595 | }
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | for (const chunkGroup of queue) {
|
---|
| 600 | for (const chunk of chunkGroup.chunks) {
|
---|
| 601 | if (!initialChunks.has(chunk)) {
|
---|
| 602 | chunks.add(chunk);
|
---|
| 603 | }
|
---|
| 604 | }
|
---|
| 605 | for (const child of chunkGroup.childrenIterable) {
|
---|
| 606 | queue.add(child);
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | return chunks;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | /**
|
---|
| 614 | * @returns {Set<Chunk>} a set of all the initial chunks (including itself)
|
---|
| 615 | */
|
---|
| 616 | getAllInitialChunks() {
|
---|
| 617 | const chunks = new Set();
|
---|
| 618 | const queue = new Set(this.groupsIterable);
|
---|
| 619 | for (const group of queue) {
|
---|
| 620 | if (group.isInitial()) {
|
---|
| 621 | for (const c of group.chunks) chunks.add(c);
|
---|
| 622 | for (const g of group.childrenIterable) queue.add(g);
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 | return chunks;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /**
|
---|
| 629 | * @returns {Set<Chunk>} a set of all the referenced chunks (including itself)
|
---|
| 630 | */
|
---|
| 631 | getAllReferencedChunks() {
|
---|
| 632 | const queue = new Set(this.groupsIterable);
|
---|
| 633 | const chunks = new Set();
|
---|
| 634 |
|
---|
| 635 | for (const chunkGroup of queue) {
|
---|
| 636 | for (const chunk of chunkGroup.chunks) {
|
---|
| 637 | chunks.add(chunk);
|
---|
| 638 | }
|
---|
| 639 | for (const child of chunkGroup.childrenIterable) {
|
---|
| 640 | queue.add(child);
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | return chunks;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | /**
|
---|
| 648 | * @returns {Set<Entrypoint>} a set of all the referenced entrypoints
|
---|
| 649 | */
|
---|
| 650 | getAllReferencedAsyncEntrypoints() {
|
---|
| 651 | const queue = new Set(this.groupsIterable);
|
---|
| 652 | const entrypoints = new Set();
|
---|
| 653 |
|
---|
| 654 | for (const chunkGroup of queue) {
|
---|
| 655 | for (const entrypoint of chunkGroup.asyncEntrypointsIterable) {
|
---|
| 656 | entrypoints.add(entrypoint);
|
---|
| 657 | }
|
---|
| 658 | for (const child of chunkGroup.childrenIterable) {
|
---|
| 659 | queue.add(child);
|
---|
| 660 | }
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | return entrypoints;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | /**
|
---|
| 667 | * @returns {boolean} true, if the chunk references async chunks
|
---|
| 668 | */
|
---|
| 669 | hasAsyncChunks() {
|
---|
| 670 | const queue = new Set();
|
---|
| 671 |
|
---|
| 672 | const initialChunks = intersect(
|
---|
| 673 | Array.from(this.groupsIterable, g => new Set(g.chunks))
|
---|
| 674 | );
|
---|
| 675 |
|
---|
| 676 | for (const chunkGroup of this.groupsIterable) {
|
---|
| 677 | for (const child of chunkGroup.childrenIterable) {
|
---|
| 678 | queue.add(child);
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | for (const chunkGroup of queue) {
|
---|
| 683 | for (const chunk of chunkGroup.chunks) {
|
---|
| 684 | if (!initialChunks.has(chunk)) {
|
---|
| 685 | return true;
|
---|
| 686 | }
|
---|
| 687 | }
|
---|
| 688 | for (const child of chunkGroup.childrenIterable) {
|
---|
| 689 | queue.add(child);
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | return false;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | /**
|
---|
| 697 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 698 | * @param {ChunkFilterPredicate=} filterFn function used to filter chunks
|
---|
| 699 | * @returns {Record<string, (string | number)[]>} a record object of names to lists of child ids(?)
|
---|
| 700 | */
|
---|
| 701 | getChildIdsByOrders(chunkGraph, filterFn) {
|
---|
| 702 | /** @type {Map<string, {order: number, group: ChunkGroup}[]>} */
|
---|
| 703 | const lists = new Map();
|
---|
| 704 | for (const group of this.groupsIterable) {
|
---|
| 705 | if (group.chunks[group.chunks.length - 1] === this) {
|
---|
| 706 | for (const childGroup of group.childrenIterable) {
|
---|
| 707 | for (const key of Object.keys(childGroup.options)) {
|
---|
| 708 | if (key.endsWith("Order")) {
|
---|
| 709 | const name = key.slice(0, key.length - "Order".length);
|
---|
| 710 | let list = lists.get(name);
|
---|
| 711 | if (list === undefined) {
|
---|
| 712 | list = [];
|
---|
| 713 | lists.set(name, list);
|
---|
| 714 | }
|
---|
| 715 | list.push({
|
---|
| 716 | order:
|
---|
| 717 | /** @type {number} */
|
---|
| 718 | (
|
---|
| 719 | childGroup.options[
|
---|
| 720 | /** @type {keyof ChunkGroupOptions} */ (key)
|
---|
| 721 | ]
|
---|
| 722 | ),
|
---|
| 723 | group: childGroup
|
---|
| 724 | });
|
---|
| 725 | }
|
---|
| 726 | }
|
---|
| 727 | }
|
---|
| 728 | }
|
---|
| 729 | }
|
---|
| 730 | /** @type {Record<string, (string | number)[]>} */
|
---|
| 731 | const result = Object.create(null);
|
---|
| 732 | for (const [name, list] of lists) {
|
---|
| 733 | list.sort((a, b) => {
|
---|
| 734 | const cmp = b.order - a.order;
|
---|
| 735 | if (cmp !== 0) return cmp;
|
---|
| 736 | return a.group.compareTo(chunkGraph, b.group);
|
---|
| 737 | });
|
---|
| 738 | /** @type {Set<string | number>} */
|
---|
| 739 | const chunkIdSet = new Set();
|
---|
| 740 | for (const item of list) {
|
---|
| 741 | for (const chunk of item.group.chunks) {
|
---|
| 742 | if (filterFn && !filterFn(chunk, chunkGraph)) continue;
|
---|
| 743 | chunkIdSet.add(/** @type {ChunkId} */ (chunk.id));
|
---|
| 744 | }
|
---|
| 745 | }
|
---|
| 746 | if (chunkIdSet.size > 0) {
|
---|
| 747 | result[name] = Array.from(chunkIdSet);
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 | return result;
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | /**
|
---|
| 754 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 755 | * @param {string} type option name
|
---|
| 756 | * @returns {{ onChunks: Chunk[], chunks: Set<Chunk> }[] | undefined} referenced chunks for a specific type
|
---|
| 757 | */
|
---|
| 758 | getChildrenOfTypeInOrder(chunkGraph, type) {
|
---|
| 759 | const list = [];
|
---|
| 760 | for (const group of this.groupsIterable) {
|
---|
| 761 | for (const childGroup of group.childrenIterable) {
|
---|
| 762 | const order =
|
---|
| 763 | childGroup.options[/** @type {keyof ChunkGroupOptions} */ (type)];
|
---|
| 764 | if (order === undefined) continue;
|
---|
| 765 | list.push({
|
---|
| 766 | order,
|
---|
| 767 | group,
|
---|
| 768 | childGroup
|
---|
| 769 | });
|
---|
| 770 | }
|
---|
| 771 | }
|
---|
| 772 | if (list.length === 0) return;
|
---|
| 773 | list.sort((a, b) => {
|
---|
| 774 | const cmp =
|
---|
| 775 | /** @type {number} */ (b.order) - /** @type {number} */ (a.order);
|
---|
| 776 | if (cmp !== 0) return cmp;
|
---|
| 777 | return a.group.compareTo(chunkGraph, b.group);
|
---|
| 778 | });
|
---|
| 779 | const result = [];
|
---|
| 780 | let lastEntry;
|
---|
| 781 | for (const { group, childGroup } of list) {
|
---|
| 782 | if (lastEntry && lastEntry.onChunks === group.chunks) {
|
---|
| 783 | for (const chunk of childGroup.chunks) {
|
---|
| 784 | lastEntry.chunks.add(chunk);
|
---|
| 785 | }
|
---|
| 786 | } else {
|
---|
| 787 | result.push(
|
---|
| 788 | (lastEntry = {
|
---|
| 789 | onChunks: group.chunks,
|
---|
| 790 | chunks: new Set(childGroup.chunks)
|
---|
| 791 | })
|
---|
| 792 | );
|
---|
| 793 | }
|
---|
| 794 | }
|
---|
| 795 | return result;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | /**
|
---|
| 799 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 800 | * @param {boolean=} includeDirectChildren include direct children (by default only children of async children are included)
|
---|
| 801 | * @param {ChunkFilterPredicate=} filterFn function used to filter chunks
|
---|
| 802 | * @returns {Record<string|number, Record<string, (string | number)[]>>} a record object of names to lists of child ids(?) by chunk id
|
---|
| 803 | */
|
---|
| 804 | getChildIdsByOrdersMap(chunkGraph, includeDirectChildren, filterFn) {
|
---|
| 805 | /** @type {Record<string|number, Record<string, (string | number)[]>>} */
|
---|
| 806 | const chunkMaps = Object.create(null);
|
---|
| 807 |
|
---|
| 808 | /**
|
---|
| 809 | * @param {Chunk} chunk a chunk
|
---|
| 810 | * @returns {void}
|
---|
| 811 | */
|
---|
| 812 | const addChildIdsByOrdersToMap = chunk => {
|
---|
| 813 | const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
|
---|
| 814 | for (const key of Object.keys(data)) {
|
---|
| 815 | let chunkMap = chunkMaps[key];
|
---|
| 816 | if (chunkMap === undefined) {
|
---|
| 817 | chunkMaps[key] = chunkMap = Object.create(null);
|
---|
| 818 | }
|
---|
| 819 | chunkMap[/** @type {ChunkId} */ (chunk.id)] = data[key];
|
---|
| 820 | }
|
---|
| 821 | };
|
---|
| 822 |
|
---|
| 823 | if (includeDirectChildren) {
|
---|
| 824 | /** @type {Set<Chunk>} */
|
---|
| 825 | const chunks = new Set();
|
---|
| 826 | for (const chunkGroup of this.groupsIterable) {
|
---|
| 827 | for (const chunk of chunkGroup.chunks) {
|
---|
| 828 | chunks.add(chunk);
|
---|
| 829 | }
|
---|
| 830 | }
|
---|
| 831 | for (const chunk of chunks) {
|
---|
| 832 | addChildIdsByOrdersToMap(chunk);
|
---|
| 833 | }
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 | for (const chunk of this.getAllAsyncChunks()) {
|
---|
| 837 | addChildIdsByOrdersToMap(chunk);
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | return chunkMaps;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | /**
|
---|
| 844 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 845 | * @param {string} type option name
|
---|
| 846 | * @param {boolean=} includeDirectChildren include direct children (by default only children of async children are included)
|
---|
| 847 | * @param {ChunkFilterPredicate=} filterFn function used to filter chunks
|
---|
| 848 | * @returns {boolean} true when the child is of type order, otherwise false
|
---|
| 849 | */
|
---|
| 850 | hasChildByOrder(chunkGraph, type, includeDirectChildren, filterFn) {
|
---|
| 851 | if (includeDirectChildren) {
|
---|
| 852 | /** @type {Set<Chunk>} */
|
---|
| 853 | const chunks = new Set();
|
---|
| 854 | for (const chunkGroup of this.groupsIterable) {
|
---|
| 855 | for (const chunk of chunkGroup.chunks) {
|
---|
| 856 | chunks.add(chunk);
|
---|
| 857 | }
|
---|
| 858 | }
|
---|
| 859 | for (const chunk of chunks) {
|
---|
| 860 | const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
|
---|
| 861 | if (data[type] !== undefined) return true;
|
---|
| 862 | }
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | for (const chunk of this.getAllAsyncChunks()) {
|
---|
| 866 | const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
|
---|
| 867 | if (data[type] !== undefined) return true;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | return false;
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | module.exports = Chunk;
|
---|