-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheleventyWebcTemplate.js
164 lines (132 loc) · 4.91 KB
/
eleventyWebcTemplate.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
const path = require("path");
const debug = require("debug")("Eleventy:WebC");
function relativePath(inputPath, newGlob) {
// project root
if(newGlob.startsWith("~/")) {
let rootRelativePath = "." + newGlob.slice(1);
return rootRelativePath;
}
let { dir } = path.parse(inputPath);
// globs must have forward slashes (even on Windows)
let templateRelativePath = path.join(dir, newGlob).split(path.sep).join("/");
return templateRelativePath;
}
function addContextToJavaScriptFunction(data, fn) {
let CONTEXT_KEYS = ["eleventy", "page"];
return function (...args) {
for (let key of CONTEXT_KEYS) {
if (data && data[key]) {
this[key] = data[key];
}
}
return fn.call(this, ...args);
};
}
module.exports = function(eleventyConfig, options = {}) {
// TODO remove this when WebC is moved out of plugin-land into core.
eleventyConfig.addTemplateFormats("webc");
let _WebC;
let globalComponentManager;
let componentsMap = false; // cache the glob search
let moduleScript;
let scopedHelpers = new Set(options.scopedHelpers);
eleventyConfig.on("eleventy.before", async () => {
// For ESM in CJS
let { WebC, ModuleScript, ComponentManager } = await import("@11ty/webc");
moduleScript = ModuleScript;
_WebC = WebC;
globalComponentManager = new ComponentManager();
if(options.components) {
componentsMap = WebC.getComponentsMap(options.components); // second argument is ignores here
}
});
let templateConfig;
eleventyConfig.on("eleventy.config", (cfg) => {
templateConfig = cfg;
});
eleventyConfig.addExtension("webc", {
outputFileExtension: "html",
compileOptions: {
permalink: function(contents, inputPath) {
if(contents && typeof contents === "string") {
return async (data) => {
try {
// Hard to know if this is JavaScript code or just a raw string value.
let evaluatedString = await moduleScript.evaluateScript(contents, {
...this,
...data,
}, `Check the permalink for ${inputPath}`, "eleventyWebcPermalink:" + inputPath);
return evaluatedString.returns;
} catch(e) {
debug("Error evaluating dynamic permalink, returning raw string contents instead: %o\n%O", contents, e);
return contents;
}
}
}
return contents;
}
},
compile: async function(inputContent, inputPath) {
let page = new _WebC();
page.setGlobalComponentManager(globalComponentManager);
page.setBundlerMode(true);
page.setContent(inputContent, inputPath);
if(componentsMap) {
page.defineComponents(componentsMap);
}
// Support both casings (I prefer getCss, but yeah)
page.setHelper("getCss", (url, bucket) => this.config.javascriptFunctions.getBundle("css", bucket, url), scopedHelpers.has("getCss"));
page.setHelper("getCSS", (url, bucket) => this.config.javascriptFunctions.getBundle("css", bucket, url), scopedHelpers.has("getCSS"));
page.setHelper("getJs", (url, bucket) => this.config.javascriptFunctions.getBundle("js", bucket, url), scopedHelpers.has("getJs"));
page.setHelper("getJS", (url, bucket) => this.config.javascriptFunctions.getBundle("js", bucket, url), scopedHelpers.has("getJS"));
page.setTransform("11ty", async function(content) {
let syntax = this["11ty:type"];
if(syntax) {
const { RenderPlugin } = await import("@11ty/eleventy");
const CompileString = RenderPlugin.String;
let fn = await CompileString(content, syntax, {
templateConfig
});
return fn(this);
}
return content;
});
// Render function
return async (data) => {
// Add Eleventy JavaScript Functions as WebC helpers
// Note that Universal Filters and Shortcodes populate into javascriptFunctions and will be present here
for(let helperName in this.config.javascriptFunctions) {
let helperFunction = addContextToJavaScriptFunction(data, this.config.javascriptFunctions[helperName]);
page.setHelper(helperName, helperFunction, scopedHelpers.has(helperName));
}
let setupObject = {
data,
};
if(data.webc?.components) {
setupObject.components = _WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
}
if(options.before && typeof options.before === "function") {
await options.before(page);
}
let { html, css, js, buckets, components } = await page.compile(setupObject);
// 2.0.0-canary.19+
this.addDependencies(inputPath, components);
// Add CSS to bundle
this.config.javascriptFunctions.css(css, "default", data.page.url);
if(buckets.css) {
for(let bucket in buckets.css) {
this.config.javascriptFunctions.css(buckets.css[bucket], bucket, data.page.url);
}
}
// Add JS to bundle
this.config.javascriptFunctions.js(js, "default", data.page.url);
if(buckets.js) {
for(let bucket in buckets.js) {
this.config.javascriptFunctions.js(buckets.js[bucket], bucket, data.page.url);
}
}
return html;
};
}
});
};