[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 makeSerializable = require("./util/makeSerializable");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
---|
| 11 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
---|
| 12 | /** @typedef {import("./ChunkGroup")} ChunkGroup */
|
---|
| 13 | /** @typedef {import("./Dependency")} Dependency */
|
---|
| 14 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 15 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 16 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 17 | /** @typedef {import("./util/Hash")} Hash */
|
---|
| 18 |
|
---|
| 19 | /** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * DependenciesBlock is the base class for all Module classes in webpack. It describes a
|
---|
| 23 | * "block" of dependencies which are pointers to other DependenciesBlock instances. For example
|
---|
| 24 | * when a Module has a CommonJs require statement, the DependencyBlock for the CommonJs module
|
---|
| 25 | * would be added as a dependency to the Module. DependenciesBlock is inherited by two types of classes:
|
---|
| 26 | * Module subclasses and AsyncDependenciesBlock subclasses. The only difference between the two is that
|
---|
| 27 | * AsyncDependenciesBlock subclasses are used for code-splitting (async boundary) and Module subclasses are not.
|
---|
| 28 | */
|
---|
| 29 | class DependenciesBlock {
|
---|
| 30 | constructor() {
|
---|
| 31 | /** @type {Dependency[]} */
|
---|
| 32 | this.dependencies = [];
|
---|
| 33 | /** @type {AsyncDependenciesBlock[]} */
|
---|
| 34 | this.blocks = [];
|
---|
| 35 | /** @type {DependenciesBlock | undefined} */
|
---|
| 36 | this.parent = undefined;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | getRootBlock() {
|
---|
| 40 | /** @type {DependenciesBlock} */
|
---|
| 41 | let current = this;
|
---|
| 42 | while (current.parent) current = current.parent;
|
---|
| 43 | return current;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Adds a DependencyBlock to DependencyBlock relationship.
|
---|
| 48 | * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
|
---|
| 49 | * @param {AsyncDependenciesBlock} block block being added
|
---|
| 50 | * @returns {void}
|
---|
| 51 | */
|
---|
| 52 | addBlock(block) {
|
---|
| 53 | this.blocks.push(block);
|
---|
| 54 | block.parent = this;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * @param {Dependency} dependency dependency being tied to block.
|
---|
| 59 | * This is an "edge" pointing to another "node" on module graph.
|
---|
| 60 | * @returns {void}
|
---|
| 61 | */
|
---|
| 62 | addDependency(dependency) {
|
---|
| 63 | this.dependencies.push(dependency);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * @param {Dependency} dependency dependency being removed
|
---|
| 68 | * @returns {void}
|
---|
| 69 | */
|
---|
| 70 | removeDependency(dependency) {
|
---|
| 71 | const idx = this.dependencies.indexOf(dependency);
|
---|
| 72 | if (idx >= 0) {
|
---|
| 73 | this.dependencies.splice(idx, 1);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Removes all dependencies and blocks
|
---|
| 79 | * @returns {void}
|
---|
| 80 | */
|
---|
| 81 | clearDependenciesAndBlocks() {
|
---|
| 82 | this.dependencies.length = 0;
|
---|
| 83 | this.blocks.length = 0;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * @param {Hash} hash the hash used to track dependencies
|
---|
| 88 | * @param {UpdateHashContext} context context
|
---|
| 89 | * @returns {void}
|
---|
| 90 | */
|
---|
| 91 | updateHash(hash, context) {
|
---|
| 92 | for (const dep of this.dependencies) {
|
---|
| 93 | dep.updateHash(hash, context);
|
---|
| 94 | }
|
---|
| 95 | for (const block of this.blocks) {
|
---|
| 96 | block.updateHash(hash, context);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * @param {ObjectSerializerContext} context context
|
---|
| 102 | */
|
---|
| 103 | serialize({ write }) {
|
---|
| 104 | write(this.dependencies);
|
---|
| 105 | write(this.blocks);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * @param {ObjectDeserializerContext} context context
|
---|
| 110 | */
|
---|
| 111 | deserialize({ read }) {
|
---|
| 112 | this.dependencies = read();
|
---|
| 113 | this.blocks = read();
|
---|
| 114 | for (const block of this.blocks) {
|
---|
| 115 | block.parent = this;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | makeSerializable(DependenciesBlock, "webpack/lib/DependenciesBlock");
|
---|
| 121 |
|
---|
| 122 | module.exports = DependenciesBlock;
|
---|