-
-
Notifications
You must be signed in to change notification settings - Fork 475
/
Custom.js
232 lines (195 loc) · 6.14 KB
/
Custom.js
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
const TemplateEngine = require("./TemplateEngine");
const getJavaScriptData = require("../Util/GetJavaScriptData");
class CustomEngine extends TemplateEngine {
constructor(name, dirs, config) {
super(name, dirs, config);
this.entry = this.getExtensionMapEntry();
this.needsInit =
"init" in this.entry && typeof this.entry.init === "function";
this._defaultEngine = undefined;
// Enable cacheability for this template
if (this.entry.compileOptions && "cache" in this.entry.compileOptions) {
this.cacheable = this.entry.compileOptions.cache;
} else if (this.needsToReadFileContents()) {
this.cacheable = true;
}
}
getExtensionMapEntry() {
if ("extensionMap" in this.config) {
for (let entry of this.config.extensionMap) {
if (entry.key.toLowerCase() === this.name.toLowerCase()) {
return entry;
}
}
}
throw Error(
`Could not find a custom extension for ${this.name}. Did you add it to your config file?`
);
}
setDefaultEngine(defaultEngine) {
this._defaultEngine = defaultEngine;
}
needsToReadFileContents() {
if ("read" in this.entry) {
return this.entry.read;
}
return true;
}
// If we init from multiple places, wait for the first init to finish before continuing on.
async _runningInit() {
if (this.needsInit) {
if (!this._initing) {
this._initBench = this.benchmarks.aggregate.get(
`Engine (${this.name}) Init`
);
this._initBench.before();
this._initing = this.entry.init.bind({
config: this.config,
bench: this.benchmarks.aggregate,
})();
}
await this._initing;
this.needsInit = false;
if (this._initBench) {
this._initBench.after();
this._initBench = undefined;
}
}
}
async getExtraDataFromFile(inputPath) {
if (!("getData" in this.entry) || this.entry.getData === false) {
return;
}
await this._runningInit();
if (typeof this.entry.getData === "function") {
let dataBench = this.benchmarks.aggregate.get(
`Engine (${this.name}) Get Data From File (Function)`
);
dataBench.before();
let data = this.entry.getData(inputPath);
dataBench.after();
return data;
}
// if getData is not false or a function then `getInstanceFromInputPath` must exist
if (!("getInstanceFromInputPath" in this.entry)) {
return Promise.reject(
new Error(
`getInstanceFromInputPath callback missing from ${this.name} template engine plugin.`
)
);
}
let keys = new Set();
if (this.entry.getData === true) {
keys.add("data");
} else if (Array.isArray(this.entry.getData)) {
for (let key of this.entry.getData) {
keys.add(key);
}
}
let dataBench = this.benchmarks.aggregate.get(
`Engine (${this.name}) Get Data From File`
);
dataBench.before();
let inst = await this.entry.getInstanceFromInputPath(inputPath);
// override keys set at the plugin level in the individual template
if (inst.eleventyDataKey) {
keys = new Set(inst.eleventyDataKey);
}
let mixins;
if (this.config) {
// Object.assign usage: see TemplateRenderCustomTest.js: `JavaScript functions should not be mutable but not *that* mutable`
mixins = Object.assign({}, this.config.javascriptFunctions);
}
let promises = [];
for (let key of keys) {
promises.push(
getJavaScriptData(inst, inputPath, key, {
mixins,
isObjectRequired: key === "data",
})
);
}
let results = await Promise.all(promises);
let data = {};
for (let result of results) {
Object.assign(data, result);
}
dataBench.after();
return data;
}
async compile(str, inputPath, ...args) {
await this._runningInit();
let defaultRenderer;
if (this._defaultEngine) {
defaultRenderer = async (data) => {
const render = await this._defaultEngine.compile(
str,
inputPath,
...args
);
return render(data);
};
}
// Fall back to default compiler if the user does not provide their own
if (!this.entry.compile && defaultRenderer) {
return defaultRenderer;
}
// TODO generalize this (look at JavaScript.js)
let fn = this.entry.compile.bind({
config: this.config,
defaultRenderer, // bind defaultRenderer to compile function
})(str, inputPath);
// Support `undefined` to skip compile/render
if (fn) {
// Bind defaultRenderer to render function
if ("then" in fn && typeof fn.then === "function") {
// Promise, wait to bind
return fn.then((fn) => fn.bind({ defaultRenderer }));
} else if ("bind" in fn && typeof fn.bind === "function") {
return fn.bind({ defaultRenderer });
}
}
return fn;
}
get defaultTemplateFileExtension() {
return this.entry.outputFileExtension;
}
getCompileCacheKey(str, inputPath) {
if (
this.entry.compileOptions &&
"getCacheKey" in this.entry.compileOptions
) {
if (typeof this.entry.compileOptions.getCacheKey !== "function") {
throw new Error(
`\`compileOptions.getCacheKey\` must be a function in addExtension for the ${this.name} type`
);
}
return this.entry.compileOptions.getCacheKey(str, inputPath);
}
return super.getCompileCacheKey(str, inputPath);
}
permalinkNeedsCompilation(str) {
if (this.entry.compileOptions && "permalink" in this.entry.compileOptions) {
let p = this.entry.compileOptions.permalink;
if (p === "raw") {
return false;
}
// permalink: false is aliased to permalink: () => false
if (p === false) {
return () => false;
}
return this.entry.compileOptions.permalink;
}
return true;
}
static shouldSpiderJavaScriptDependencies(entry) {
if (
entry.compileOptions &&
"spiderJavaScriptDependencies" in entry.compileOptions
) {
return entry.compileOptions.spiderJavaScriptDependencies;
}
return false;
}
}
module.exports = CustomEngine;