UNPKG

3.35 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4 */
5"use strict";
6
7const DependenciesBlockVariable = require("./DependenciesBlockVariable");
8
9/** @typedef {import("./ChunkGroup")} ChunkGroup */
10/** @typedef {import("./Dependency")} Dependency */
11/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
12/** @typedef {import("./DependenciesBlockVariable")} DependenciesBlockVariable */
13/** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
14/** @typedef {import("./util/createHash").Hash} Hash */
15
16class DependenciesBlock {
17 constructor() {
18 /** @type {Dependency[]} */
19 this.dependencies = [];
20 /** @type {AsyncDependenciesBlock[]} */
21 this.blocks = [];
22 /** @type {DependenciesBlockVariable[]} */
23 this.variables = [];
24 }
25
26 /**
27 * Adds a DependencyBlock to DependencyBlock relationship.
28 * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
29 *
30 * @param {AsyncDependenciesBlock} block block being added
31 * @returns {void}
32 */
33 addBlock(block) {
34 this.blocks.push(block);
35 block.parent = this;
36 }
37
38 /**
39 * @param {string} name name of dependency
40 * @param {string} expression expression string for variable
41 * @param {Dependency[]} dependencies dependency instances tied to variable
42 * @returns {void}
43 */
44 addVariable(name, expression, dependencies) {
45 for (let v of this.variables) {
46 if (v.name === name && v.expression === expression) {
47 return;
48 }
49 }
50 this.variables.push(
51 new DependenciesBlockVariable(name, expression, dependencies)
52 );
53 }
54
55 /**
56 * @param {Dependency} dependency dependency being tied to block.
57 * This is an "edge" pointing to another "node" on module graph.
58 * @returns {void}
59 */
60 addDependency(dependency) {
61 this.dependencies.push(dependency);
62 }
63
64 /**
65 * @param {Dependency} dependency dependency being removed
66 * @returns {void}
67 */
68 removeDependency(dependency) {
69 const idx = this.dependencies.indexOf(dependency);
70 if (idx >= 0) {
71 this.dependencies.splice(idx, 1);
72 }
73 }
74
75 /**
76 * @param {Hash} hash the hash used to track dependencies
77 * @returns {void}
78 */
79 updateHash(hash) {
80 for (const dep of this.dependencies) dep.updateHash(hash);
81 for (const block of this.blocks) block.updateHash(hash);
82 for (const variable of this.variables) variable.updateHash(hash);
83 }
84
85 disconnect() {
86 for (const dep of this.dependencies) dep.disconnect();
87 for (const block of this.blocks) block.disconnect();
88 for (const variable of this.variables) variable.disconnect();
89 }
90
91 unseal() {
92 for (const block of this.blocks) block.unseal();
93 }
94
95 /**
96 * @param {DependencyFilterFunction} filter filter function for dependencies, gets passed all dependency ties from current instance
97 * @returns {boolean} returns boolean for filter
98 */
99 hasDependencies(filter) {
100 if (filter) {
101 for (const dep of this.dependencies) {
102 if (filter(dep)) return true;
103 }
104 } else {
105 if (this.dependencies.length > 0) {
106 return true;
107 }
108 }
109
110 for (const block of this.blocks) {
111 if (block.hasDependencies(filter)) return true;
112 }
113 for (const variable of this.variables) {
114 if (variable.hasDependencies(filter)) return true;
115 }
116 return false;
117 }
118
119 sortItems() {
120 for (const block of this.blocks) block.sortItems();
121 }
122}
123
124module.exports = DependenciesBlock;