UNPKG

2.38 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 }
26
27 /**
28 * Adds a DependencyBlock to DependencyBlock relationship.
29 * This is used for when a Module has a AsyncDependencyBlock tie (for code-splitting)
30 *
31 * @param {AsyncDependenciesBlock} block block being added
32 * @returns {void}
33 */
34 addBlock(block) {
35 this.blocks.push(block);
36 block.parent = this;
37 }
38
39 /**
40 * @param {Dependency} dependency dependency being tied to block.
41 * This is an "edge" pointing to another "node" on module graph.
42 * @returns {void}
43 */
44 addDependency(dependency) {
45 this.dependencies.push(dependency);
46 }
47
48 /**
49 * @param {Dependency} dependency dependency being removed
50 * @returns {void}
51 */
52 removeDependency(dependency) {
53 const idx = this.dependencies.indexOf(dependency);
54 if (idx >= 0) {
55 this.dependencies.splice(idx, 1);
56 }
57 }
58
59 /**
60 * Removes all dependencies and blocks
61 * @returns {void}
62 */
63 clearDependenciesAndBlocks() {
64 this.dependencies.length = 0;
65 this.blocks.length = 0;
66 }
67
68 /**
69 * @param {Hash} hash the hash used to track dependencies
70 * @param {UpdateHashContext} context context
71 * @returns {void}
72 */
73 updateHash(hash, context) {
74 for (const dep of this.dependencies) {
75 dep.updateHash(hash, context);
76 }
77 for (const block of this.blocks) {
78 block.updateHash(hash, context);
79 }
80 }
81
82 serialize({ write }) {
83 write(this.dependencies);
84 write(this.blocks);
85 }
86
87 deserialize({ read }) {
88 this.dependencies = read();
89 this.blocks = read();
90 for (const block of this.blocks) {
91 block.parent = this;
92 }
93 }
94}
95
96makeSerializable(DependenciesBlock, "webpack/lib/DependenciesBlock");
97
98module.exports = DependenciesBlock;