UNPKG

2.59 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const 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("./util/Hash")} Hash */
16
17/** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
18
19class DependenciesBlock {
20 constructor() {
21 /** @type {Dependency[]} */
22 this.dependencies = [];
23 /** @type {AsyncDependenciesBlock[]} */
24 this.blocks = [];
25 /** @type {DependenciesBlock} */
26 this.parent = undefined;
27 }
28
29 getRootBlock() {
30 /** @type {DependenciesBlock} */
31 let current = this;
32 while (current.parent) current = current.parent;
33 return current;
34 }
35
36 /**
37 * Adds a DependencyBlock to DependencyBlock relationship.
38 * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
39 *
40 * @param {AsyncDependenciesBlock} block block being added
41 * @returns {void}
42 */
43 addBlock(block) {
44 this.blocks.push(block);
45 block.parent = this;
46 }
47
48 /**
49 * @param {Dependency} dependency dependency being tied to block.
50 * This is an "edge" pointing to another "node" on module graph.
51 * @returns {void}
52 */
53 addDependency(dependency) {
54 this.dependencies.push(dependency);
55 }
56
57 /**
58 * @param {Dependency} dependency dependency being removed
59 * @returns {void}
60 */
61 removeDependency(dependency) {
62 const idx = this.dependencies.indexOf(dependency);
63 if (idx >= 0) {
64 this.dependencies.splice(idx, 1);
65 }
66 }
67
68 /**
69 * Removes all dependencies and blocks
70 * @returns {void}
71 */
72 clearDependenciesAndBlocks() {
73 this.dependencies.length = 0;
74 this.blocks.length = 0;
75 }
76
77 /**
78 * @param {Hash} hash the hash used to track dependencies
79 * @param {UpdateHashContext} context context
80 * @returns {void}
81 */
82 updateHash(hash, context) {
83 for (const dep of this.dependencies) {
84 dep.updateHash(hash, context);
85 }
86 for (const block of this.blocks) {
87 block.updateHash(hash, context);
88 }
89 }
90
91 serialize({ write }) {
92 write(this.dependencies);
93 write(this.blocks);
94 }
95
96 deserialize({ read }) {
97 this.dependencies = read();
98 this.blocks = read();
99 for (const block of this.blocks) {
100 block.parent = this;
101 }
102 }
103}
104
105makeSerializable(DependenciesBlock, "webpack/lib/DependenciesBlock");
106
107module.exports = DependenciesBlock;