-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
69 lines (58 loc) · 2.04 KB
/
.eleventy.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
const path = require('node:path')
const {
Edge,
default: defaultEdgeInstance
} = require('edge.js')
const { Supercharged: EdgeSuperCharged } = require('edge-supercharged')
function initDefaultEdgeInstance(edge, eleventyUserConfig) {
const cwd = process.cwd()
const { dir } = eleventyUserConfig
edge.mount(path.join(cwd, dir.input))
edge.mount('includes', path.join(cwd, dir.input, dir.includes))
edge.mount('layouts', path.join(cwd, dir.input, dir.layouts))
const supercharged = new EdgeSuperCharged()
edge.use(supercharged.wire, {
recurring: process.env.NODE_ENV === 'development'
})
}
function createExtension(userOptions = {}) {
let edgeInstance = null
return {
outputFileExtension: 'html',
init: async function () {
if (typeof userOptions.createEdgeInstance === 'function') {
edgeInstance = await userOptions.createEdgeInstance(this.config)
return
} else {
await initDefaultEdgeInstance(defaultEdgeInstance, this.config)
if (typeof userOptions.extendEdgeInstance === 'function') {
await (userOptions.extendEdgeInstance(defaultEdgeInstance, this.config))
}
edgeInstance = defaultEdgeInstance
}
},
compile: async function (inputContent, inputPath) {
const hasSkipRenderCondition = typeof userOptions.skipRenderCondition === 'function'
if (hasSkipRenderCondition) {
const shouldSkip = await userOptions.skipRenderCondition.apply(this, [
inputContent,
inputPath,
this.config
])
if (shouldSkip) {
// just return `undefined` when this fix will be available
// https://github.com/11ty/eleventy/pull/2358
return () => {}
}
}
return async function (data) {
const content = await edgeInstance.renderRaw(inputContent, data)
return content
}
}
}
}
module.exports = function(eleventyConfig, userOptions = {}) {
eleventyConfig.addTemplateFormats('edge')
eleventyConfig.addExtension('edge', createExtension(userOptions))
}