1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const { RawSource, ReplaceSource } = require("webpack-sources");
|
8
|
|
9
|
/** @typedef {import("./Dependency")} Dependency */
|
10
|
/** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate */
|
11
|
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
12
|
/** @typedef {import("./util/createHash").Hash} Hash */
|
13
|
/** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
|
14
|
/** @typedef {Map<Function, DependencyTemplate>} DependencyTemplates */
|
15
|
|
16
|
class DependenciesBlockVariable {
|
17
|
/**
|
18
|
* Creates an instance of DependenciesBlockVariable.
|
19
|
* @param {string} name name of DependenciesBlockVariable
|
20
|
* @param {string} expression expression string
|
21
|
* @param {Dependency[]=} dependencies dependencies tied to this varaiable
|
22
|
*/
|
23
|
constructor(name, expression, dependencies) {
|
24
|
this.name = name;
|
25
|
this.expression = expression;
|
26
|
this.dependencies = dependencies || [];
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* @param {Hash} hash hash for instance to update
|
31
|
* @returns {void}
|
32
|
*/
|
33
|
updateHash(hash) {
|
34
|
hash.update(this.name);
|
35
|
hash.update(this.expression);
|
36
|
for (const d of this.dependencies) {
|
37
|
d.updateHash(hash);
|
38
|
}
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* @param {DependencyTemplates} dependencyTemplates Dependency constructors and templates Map.
|
43
|
* @param {RuntimeTemplate} runtimeTemplate runtimeTemplate to generate expression souce
|
44
|
* @returns {ReplaceSource} returns constructed source for expression via templates
|
45
|
*/
|
46
|
expressionSource(dependencyTemplates, runtimeTemplate) {
|
47
|
const source = new ReplaceSource(new RawSource(this.expression));
|
48
|
for (const dep of this.dependencies) {
|
49
|
const template = dependencyTemplates.get(dep.constructor);
|
50
|
if (!template) {
|
51
|
throw new Error(`No template for dependency: ${dep.constructor.name}`);
|
52
|
}
|
53
|
template.apply(dep, source, runtimeTemplate, dependencyTemplates);
|
54
|
}
|
55
|
return source;
|
56
|
}
|
57
|
|
58
|
disconnect() {
|
59
|
for (const d of this.dependencies) {
|
60
|
d.disconnect();
|
61
|
}
|
62
|
}
|
63
|
|
64
|
hasDependencies(filter) {
|
65
|
if (filter) {
|
66
|
return this.dependencies.some(filter);
|
67
|
}
|
68
|
return this.dependencies.length > 0;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
module.exports = DependenciesBlockVariable;
|