Repository navigation
Expand file tree
/
Copy pathWatchTargets.js
More file actions
143 lines (114 loc) · 3.3 KB
/
Copy pathWatchTargets.js
File metadata and controls
143 lines (114 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { TemplatePath } from "@11ty/eleventy-utils";
import { DepGraph } from "dependency-graph";
import { mergeGraphs } from "@11ty/dependency-tree";
import JavaScriptDependencies from "./Util/JavaScriptDependencies.js";
import { resetImportCache } from "./Util/Require.js";
export default class WatchTargets {
#templateConfig;
constructor(templateConfig) {
this.targets = new Set();
this.dependencies = new Set();
this.newTargets = new Set();
this.graph = new DepGraph();
this.#templateConfig = templateConfig;
}
isJavaScriptDependency(path) {
return this.dependencies.has(path);
}
reset() {
this.newTargets = new Set();
}
uses(parent, dep) {
return this.getDependenciesOf(parent).includes(dep);
}
getDependenciesOf(parent) {
if (!this.graph.hasNode(parent)) {
return [];
}
return this.graph.dependenciesOf(parent);
}
getDependantsOf(child) {
if (!this.graph.hasNode(child)) {
return [];
}
return this.graph.dependantsOf(child);
}
addRaw(targets, isDependency) {
for (let target of targets) {
let path = TemplatePath.addLeadingDotSlash(target);
if (!this.targets.has(target)) {
this.newTargets.add(path);
}
this.targets.add(path);
if (isDependency) {
this.dependencies.add(path);
}
}
}
static toArray(targets) {
if (!targets) {
return [];
} else if (Array.isArray(targets)) {
return targets;
}
return [targets];
}
// add only a target
add(targets) {
this.addRaw(WatchTargets.toArray(targets));
}
static normalizeToGlobs(targets) {
return WatchTargets.toArray(targets).map((entry) =>
TemplatePath.convertToRecursiveGlobSync(entry),
);
}
// add only a target’s dependencies
async addDependencies(targets, filterCallback) {
if (this.#templateConfig && !this.#templateConfig.shouldSpiderJavaScriptDependencies()) {
return;
}
targets = WatchTargets.toArray(targets);
// https://github.com/11ty/eleventy/issues/3899
let graph = await JavaScriptDependencies.getGraph(targets);
// The filter only applies to new watch targets: dependants of a changed file (e.g. global data files) must be in the graph to reset their import cache
mergeGraphs(this.graph, graph);
// The graph includes the original targets, which we do not want for addRaw
let targetSet = new Set(targets);
this.addRaw(
graph
.overallOrder()
.filter((node) => !targetSet.has(node) && (!filterCallback || filterCallback(node))),
true,
);
}
setWriter(templateWriter) {
this.writer = templateWriter;
}
clearImportCacheFor(filePathArray) {
let paths = new Set();
for (const filePath of filePathArray) {
paths.add(filePath);
// Delete from require cache so that updates to the module are re-required
let importsTheChangedFile = this.getDependantsOf(filePath);
for (let dep of importsTheChangedFile) {
paths.add(dep);
}
let isImportedInTheChangedFile = this.getDependenciesOf(filePath);
for (let dep of isImportedInTheChangedFile) {
paths.add(dep);
}
// Use GlobalDependencyMap
let dependantsMapped = this.#templateConfig?.usesGraph.getDependantsFor(filePath) || [];
for (let dep of dependantsMapped) {
paths.add(dep);
}
}
resetImportCache(paths);
}
getNewTargetsSinceLastReset() {
return Array.from(this.newTargets);
}
getTargets() {
return Array.from(this.targets);
}
}