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