summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Data/TemplateDataInitialGlobalData.js
blob: 7e2a7ee2e974417293cda6687563ef261dd73ac6 (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
import lodash from "@11ty/lodash-custom";

import EleventyBaseError from "../Errors/EleventyBaseError.js";

const { set: lodashSet } = lodash;

class TemplateDataConfigError extends EleventyBaseError {}

class TemplateDataInitialGlobalData {
	constructor(templateConfig) {
		if (!templateConfig || templateConfig.constructor.name !== "TemplateConfig") {
			throw new TemplateDataConfigError("Missing or invalid `templateConfig` (via Render plugin).");
		}
		this.templateConfig = templateConfig;
		this.config = this.templateConfig.getConfig();
	}

	async getData() {
		let globalData = {};

		// via eleventyConfig.addGlobalData
		if (this.config.globalData) {
			let keys = Object.keys(this.config.globalData);
			for (let key of keys) {
				let returnValue = this.config.globalData[key];

				// This section is problematic when used with eleventyComputed #3389
				if (typeof returnValue === "function") {
					returnValue = await returnValue();
				}

				lodashSet(globalData, key, returnValue);
			}
		}

		return globalData;
	}
}

export default TemplateDataInitialGlobalData;