summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Engines/TemplateEngine.js
blob: 234aa4e747eb59897f95d848026efacfe19f2cc4 (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
import debugUtil from "debug";
import EleventyBaseError from "../Errors/EleventyBaseError.js";

class TemplateEngineConfigError extends EleventyBaseError {}

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

const AMENDED_INSTANCES = new Set();

export default class TemplateEngine {
	#extensionMap;
	#engineManager;
	#benchmarks;

	constructor(name, eleventyConfig) {
		this.name = name;

		this.engineLib = null;

		if (!eleventyConfig) {
			throw new TemplateEngineConfigError("Missing `eleventyConfig` argument.");
		}
		this.eleventyConfig = eleventyConfig;
	}

	get cacheable() {
		return false;
	}

	get dirs() {
		return this.eleventyConfig.directories;
	}

	get inputDir() {
		return this.dirs.input;
	}

	get includesDir() {
		return this.dirs.includes;
	}

	get config() {
		if (this.eleventyConfig.constructor.name !== "TemplateConfig") {
			throw new Error("Expecting a TemplateConfig instance.");
		}

		return this.eleventyConfig.getConfig();
	}

	get benchmarks() {
		if (!this.#benchmarks) {
			this.#benchmarks = {
				aggregate: this.config.benchmarkManager.get("Aggregate"),
			};
		}
		return this.#benchmarks;
	}

	get engineManager() {
		return this.#engineManager;
	}

	set engineManager(manager) {
		this.#engineManager = manager;
	}

	get extensionMap() {
		if (!this.#extensionMap) {
			throw new Error("Internal error: missing `extensionMap` in TemplateEngine.");
		}
		return this.#extensionMap;
	}

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

	get extensions() {
		if (!this._extensions) {
			this._extensions = this.extensionMap.getExtensionsFromKey(this.name);
		}
		return this._extensions;
	}

	get extensionEntries() {
		if (!this._extensionEntries) {
			this._extensionEntries = this.extensionMap.getExtensionEntriesFromKey(this.name);
		}
		return this._extensionEntries;
	}

	getName() {
		return this.name;
	}

	// Backwards compat
	getIncludesDir() {
		return this.includesDir;
	}

	/**
	 * @protected
	 */
	setEngineLib(engineLib, isOverrideViaSetLibrary = false) {
		this.engineLib = engineLib;

		// Run engine amendments (via issue #2438)
		// Issue #3816: this isn’t ideal but there is no other way to reset a markdown instance if it was also overridden by addLibrary
		if (AMENDED_INSTANCES.has(engineLib)) {
			return;
		}

		if (isOverrideViaSetLibrary) {
			AMENDED_INSTANCES.add(engineLib);
		}
		debug(
			"Running amendLibrary for %o (number of amendments: %o)",
			this.name,
			this.config.libraryAmendments[this.name]?.length,
		);

		for (let amendment of this.config.libraryAmendments[this.name] || []) {
			// TODO it’d be nice if this were async friendly
			amendment(engineLib);
		}
	}

	getEngineLib() {
		return this.engineLib;
	}

	async _testRender(str, data) {
		// @ts-ignore
		let fn = await this.compile(str);
		return fn(data);
	}

	useJavaScriptImport() {
		return false;
	}

	// JavaScript files defer to the module loader rather than read the files to strings
	needsToReadFileContents() {
		return true;
	}

	getExtraDataFromFile() {
		return {};
	}

	getCompileCacheKey(str, inputPath) {
		// Changing to use inputPath and contents, using only file contents (`str`) caused issues when two
		// different files had identical content (2.0.0-canary.16)

		// Caches are now segmented based on inputPath so using inputPath here is superfluous (2.0.0-canary.19)
		// But we do want a non-falsy value here even if `str` is an empty string.
		return {
			useCache: true,
			key: inputPath + str,
		};
	}

	get defaultTemplateFileExtension() {
		return "html";
	}

	// Whether or not to wrap in Eleventy layouts
	useLayouts() {
		return true;
	}

	/** @returns {boolean|undefined} */
	permalinkNeedsCompilation(str) {
		return this.needsCompilation();
	}

	// whether or not compile is needed or can we return the plaintext?
	needsCompilation(str) {
		return true;
	}

	/**
	 * Make sure compile is implemented downstream.
	 * @abstract
	 * @return {Promise}
	 */
	async compile() {
		throw new Error("compile() must be implemented by engine");
	}

	// See https://v3.11ty.dev/docs/watch-serve/#watch-javascript-dependencies
	static shouldSpiderJavaScriptDependencies() {
		return false;
	}

	hasDependencies(inputPath) {
		if (this.config.uses.getDependencies(inputPath) === false) {
			return false;
		}
		return true;
	}

	isFileRelevantTo(inputPath, comparisonFile) {
		return this.config.uses.isFileRelevantTo(inputPath, comparisonFile);
	}
}