summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/GlobalDependencyMap.js
blob: 7e169dafcec47661021b6fa3e86ceed44aea84e2 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import debugUtil from "debug";
import { TemplatePath } from "@11ty/eleventy-utils";

import JavaScriptDependencies from "./Util/JavaScriptDependencies.js";
import PathNormalizer from "./Util/PathNormalizer.js";
import { TemplateDepGraph } from "./Util/TemplateDepGraph.js";

const debug = debugUtil("Eleventy:Dependencies");

class GlobalDependencyMap {
	// dependency-graph requires these keys to be alphabetic strings
	static LAYOUT_KEY = "layout";
	static COLLECTION_PREFIX = "__collection:"; // must match TemplateDepGraph key

	#map;
	#templateConfig;
	#cachedUserConfigurationCollectionApiNames;

	static isCollection(entry) {
		return entry.startsWith(this.COLLECTION_PREFIX);
	}

	static getTagName(entry) {
		if (this.isCollection(entry)) {
			return entry.slice(this.COLLECTION_PREFIX.length);
		}
	}

	static getCollectionKeyForEntry(entry) {
		return `${GlobalDependencyMap.COLLECTION_PREFIX}${entry}`;
	}

	reset() {
		this.#map = undefined;
	}

	setIsEsm(isEsm) {
		this.isEsm = isEsm;
	}

	setTemplateConfig(templateConfig) {
		this.#templateConfig = templateConfig;

		// These have leading dot slashes, but so do the paths from Eleventy
		this.#templateConfig.userConfig.events.once("eleventy.layouts", async (layouts) => {
			await this.addLayoutsToMap(layouts);
		});
	}

	get userConfigurationCollectionApiNames() {
		if (this.#cachedUserConfigurationCollectionApiNames) {
			return this.#cachedUserConfigurationCollectionApiNames;
		}
		return Object.keys(this.#templateConfig.userConfig.getCollections()) || [];
	}

	initializeUserConfigurationApiCollections() {
		this.addCollectionApiNames(this.userConfigurationCollectionApiNames);
	}

	// For Testing
	setCollectionApiNames(names = []) {
		this.#cachedUserConfigurationCollectionApiNames = names;
	}

	addCollectionApiNames(names = []) {
		if (!names || names.length === 0) {
			return;
		}

		for (let collectionName of names) {
			this.map.addConfigCollectionName(collectionName);
		}
	}

	filterOutLayouts(nodes = []) {
		return nodes.filter((node) => {
			if (GlobalDependencyMap.isLayoutNode(this.map, node)) {
				return false;
			}
			return true;
		});
	}

	filterOutCollections(nodes = []) {
		return nodes.filter((node) => !node.startsWith(GlobalDependencyMap.COLLECTION_PREFIX));
	}

	static removeLayoutNodes(graph, nodeList) {
		return nodeList.filter((node) => {
			if (this.isLayoutNode(graph, node)) {
				return false;
			}
			return true;
		});
	}

	removeLayoutNodes(normalizedLayouts) {
		let nodes = this.map.overallOrder();
		for (let node of nodes) {
			if (!GlobalDependencyMap.isLayoutNode(this.map, node)) {
				continue;
			}

			// previous layout is not in the new layout map (no templates are using it)
			if (!normalizedLayouts[node]) {
				this.map.removeNode(node);
			}
			// important: if the layout map changed to have different templates (but was not removed)
			// this is already handled by `resetNode` called via TemplateMap
		}
	}

	// Eleventy Layouts don’t show up in the dependency graph, so we handle those separately
	async addLayoutsToMap(layouts) {
		let normalizedLayouts = this.normalizeLayoutsObject(layouts);

		// Clear out any previous layout relationships to make way for the new ones
		this.removeLayoutNodes(normalizedLayouts);

		for (let layout in normalizedLayouts) {
			// We add this pre-emptively to add the `layout` data
			if (!this.map.hasNode(layout)) {
				this.map.addNode(layout, {
					type: GlobalDependencyMap.LAYOUT_KEY,
				});
			} else {
				this.map.setNodeData(layout, {
					type: GlobalDependencyMap.LAYOUT_KEY,
				});
			}

			// Potential improvement: only add the first template in the chain for a template and manage any upstream layouts by their own relationships
			for (let pageTemplate of normalizedLayouts[layout]) {
				this.addDependency(pageTemplate, [layout]);
			}

			if (this.#templateConfig?.shouldSpiderJavaScriptDependencies()) {
				let deps = await JavaScriptDependencies.getDependencies([layout], this.isEsm);
				this.addDependency(layout, deps);
			}
		}
	}

	get map() {
		if (!this.#map) {
			// this.#map = new DepGraph({ circular: true });
			this.#map = new TemplateDepGraph();
		}

		return this.#map;
	}

	set map(graph) {
		this.#map = graph;
	}

	normalizeNode(node) {
		if (!node) {
			return;
		}

		// TODO tests for this
		// Fix URL objects passed in (sass does this)
		if (typeof node !== "string" && "toString" in node) {
			node = node.toString();
		}

		if (typeof node !== "string") {
			throw new Error("`addDependencies` files must be strings. Received:" + node);
		}

		return PathNormalizer.fullNormalization(node);
	}

	normalizeLayoutsObject(layouts) {
		let o = {};
		for (let rawLayout in layouts) {
			let layout = this.normalizeNode(rawLayout);
			o[layout] = layouts[rawLayout].map((entry) => this.normalizeNode(entry));
		}
		return o;
	}

	getDependantsFor(node) {
		if (!node) {
			return [];
		}

		node = this.normalizeNode(node);

		if (!this.map.hasNode(node)) {
			return [];
		}

		// Direct dependants and dependencies, both publish and consume from collections
		return this.map.directDependantsOf(node);
	}

	hasNode(node) {
		return this.map.hasNode(this.normalizeNode(node));
	}

	findCollectionsRemovedFrom(node, collectionNames) {
		if (!this.hasNode(node)) {
			return new Set();
		}

		let prevDeps = this.getDependantsFor(node)
			.map((entry) => GlobalDependencyMap.getTagName(entry))
			.filter(Boolean);

		let prevDepsSet = new Set(prevDeps);
		let deleted = new Set();
		for (let dep of prevDepsSet) {
			if (!collectionNames.has(dep)) {
				deleted.add(dep);
			}
		}

		return deleted;
	}

	resetNode(node) {
		node = this.normalizeNode(node);

		if (!this.map.hasNode(node)) {
			return;
		}

		// We don’t want to remove relationships that consume this, controlled by the upstream content
		// for (let dep of this.map.directDependantsOf(node)) {
		//   this.map.removeDependency(dep, node);
		// }

		for (let dep of this.map.directDependenciesOf(node)) {
			this.map.removeDependency(node, dep);
		}
	}

	getTemplatesThatConsumeCollections(collectionNames) {
		let templates = new Set();
		for (let name of collectionNames) {
			let collectionKey = GlobalDependencyMap.getCollectionKeyForEntry(name);
			if (!this.map.hasNode(collectionKey)) {
				continue;
			}
			for (let node of this.map.dependantsOf(collectionKey)) {
				if (!node.startsWith(GlobalDependencyMap.COLLECTION_PREFIX)) {
					if (!GlobalDependencyMap.isLayoutNode(this.map, node)) {
						templates.add(node);
					}
				}
			}
		}
		return templates;
	}

	static isLayoutNode(graph, node) {
		if (!graph.hasNode(node)) {
			return false;
		}
		return graph.getNodeData(node)?.type === GlobalDependencyMap.LAYOUT_KEY;
	}

	getLayoutsUsedBy(node) {
		node = this.normalizeNode(node);

		if (!this.map.hasNode(node)) {
			return [];
		}

		let layouts = [];

		// include self, if layout
		if (GlobalDependencyMap.isLayoutNode(this.map, node)) {
			layouts.push(node);
		}

		this.map.dependantsOf(node).forEach((node) => {
			// we only want layouts
			if (GlobalDependencyMap.isLayoutNode(this.map, node)) {
				return layouts.push(node);
			}
		});

		return layouts;
	}

	// In order
	// Does not include original templatePaths (unless *they* are second-order relevant)
	getTemplatesRelevantToTemplateList(templatePaths) {
		let overallOrder = this.map.overallOrder();
		overallOrder = this.filterOutLayouts(overallOrder);
		overallOrder = this.filterOutCollections(overallOrder);

		let relevantLookup = {};
		for (let inputPath of templatePaths) {
			inputPath = TemplatePath.stripLeadingDotSlash(inputPath);

			let deps = this.getDependencies(inputPath, false);

			if (Array.isArray(deps)) {
				let paths = this.filterOutCollections(deps);
				for (let node of paths) {
					relevantLookup[node] = true;
				}
			}
		}

		return overallOrder.filter((node) => {
			if (relevantLookup[node]) {
				return true;
			}
			return false;
		});
	}

	// Layouts are not relevant to compile cache and can be ignored
	getDependencies(node, includeLayouts = true) {
		node = this.normalizeNode(node);

		// `false` means the Node was unknown
		if (!this.map.hasNode(node)) {
			return false;
		}

		if (includeLayouts) {
			return this.map.dependenciesOf(node).filter(Boolean);
		}

		return GlobalDependencyMap.removeLayoutNodes(this.map, this.map.dependenciesOf(node));
	}

	#addNode(name) {
		if (this.map.hasNode(name)) {
			return;
		}

		this.map.addNode(name);
	}

	// node arguments are already normalized
	#addDependency(from, toArray = []) {
		this.#addNode(from); // only if not already added

		if (!Array.isArray(toArray)) {
			throw new Error("Second argument to `addDependency` must be an Array.");
		}

		// debug("%o depends on %o", from, toArray);
		for (let to of toArray) {
			this.#addNode(to); // only if not already added
			if (from !== to) {
				this.map.addDependency(from, to);
			}
		}
	}

	addDependency(from, toArray = []) {
		this.#addDependency(
			this.normalizeNode(from),
			toArray.map((to) => this.normalizeNode(to)),
		);
	}

	addNewNodeRelationships(from, consumes = [], publishes = []) {
		consumes = consumes.filter(Boolean);
		publishes = publishes.filter(Boolean);

		debug("%o consumes %o and publishes to %o", from, consumes, publishes);
		from = this.normalizeNode(from);

		this.map.addTemplate(from, consumes, publishes);
	}

	// Layouts are not relevant to compile cache and can be ignored
	hasDependency(from, to, includeLayouts) {
		to = this.normalizeNode(to);

		let deps = this.getDependencies(from, includeLayouts); // normalizes `from`

		if (!deps) {
			return false;
		}

		return deps.includes(to);
	}

	// Layouts are not relevant to compile cache and can be ignored
	isFileRelevantTo(fullTemplateInputPath, comparisonFile, includeLayouts) {
		fullTemplateInputPath = this.normalizeNode(fullTemplateInputPath);
		comparisonFile = this.normalizeNode(comparisonFile);

		// No watch/serve changed file
		if (!comparisonFile) {
			return false;
		}

		// The file that changed is the relevant file
		if (fullTemplateInputPath === comparisonFile) {
			return true;
		}

		// The file that changed is a dependency of the template
		// comparisonFile is used by fullTemplateInputPath
		if (this.hasDependency(fullTemplateInputPath, comparisonFile, includeLayouts)) {
			return true;
		}

		return false;
	}

	isFileUsedBy(parent, child, includeLayouts) {
		if (this.hasDependency(parent, child, includeLayouts)) {
			// child is used by parent
			return true;
		}
		return false;
	}

	getTemplateOrder() {
		let order = [];
		for (let entry of this.map.overallOrder()) {
			order.push(entry);
		}

		return order;
	}

	stringify() {
		return JSON.stringify(this.map, function replacer(key, value) {
			// Serialize internal Map objects.
			if (value instanceof Map) {
				let obj = {};
				for (let [k, v] of value) {
					obj[k] = v;
				}
				return obj;
			}

			return value;
		});
	}

	restore(persisted) {
		let obj = JSON.parse(persisted);
		let graph = new TemplateDepGraph();

		// https://github.com/jriecken/dependency-graph/issues/44
		// Restore top level serialized Map objects (in stringify above)
		for (let key in obj) {
			let map = graph[key];
			for (let k in obj[key]) {
				let v = obj[key][k];
				map.set(k, v);
			}
		}
		this.map = graph;
	}
}

export default GlobalDependencyMap;