@11ty/eleventy-plugin-webc 0.12.0-beta.3 → 0.12.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -4
- package/src/dynamicScript.js +50 -0
- package/src/eleventyWebcTemplate.js +17 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@11ty/eleventy-plugin-webc",
|
|
3
|
-
"version": "0.12.0-beta.
|
|
3
|
+
"version": "0.12.0-beta.5",
|
|
4
4
|
"description": "WebC support for Eleventy adds for Single File Web Components",
|
|
5
5
|
"main": "eleventyWebcPlugin.js",
|
|
6
6
|
"scripts": {
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"url": "git://github.com/11ty/eleventy-plugin-webc.git"
|
|
33
33
|
},
|
|
34
34
|
"bugs": "https://github.com/11ty/eleventy-plugin-webc/issues",
|
|
35
|
+
"homepage": "https://github.com/11ty/eleventy-plugin-webc",
|
|
35
36
|
"ava": {
|
|
36
37
|
"failFast": true,
|
|
37
38
|
"files": [
|
|
@@ -46,10 +47,12 @@
|
|
|
46
47
|
}
|
|
47
48
|
},
|
|
48
49
|
"dependencies": {
|
|
49
|
-
"@11ty/
|
|
50
|
+
"@11ty/eleventy-plugin-bundle": "^3.0.7",
|
|
51
|
+
"@11ty/webc": "0.12.0-beta.1",
|
|
52
|
+
"import-module-string": "^2.0.3"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
|
-
"@11ty/eleventy": "^3.
|
|
53
|
-
"ava": "^6.
|
|
55
|
+
"@11ty/eleventy": "^3.1.2",
|
|
56
|
+
"ava": "^6.4.1"
|
|
54
57
|
}
|
|
55
58
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// TODO move this upstream into import-module-string
|
|
2
|
+
// Code cribbed from WebC’s attributeSerializer.js
|
|
3
|
+
async function evaluateInlineCode(codeString, options = {}) {
|
|
4
|
+
let { filePath, context, data } = Object.assign({ data: {} }, options);
|
|
5
|
+
|
|
6
|
+
let { parseCode, walkCode, importFromString } = await import("import-module-string");
|
|
7
|
+
let varsInUse = [];
|
|
8
|
+
try {
|
|
9
|
+
let {used} = walkCode(parseCode(codeString));
|
|
10
|
+
varsInUse = used;
|
|
11
|
+
} catch(e) {
|
|
12
|
+
let errorString = `Error parsing inline code: \`${codeString}\``;
|
|
13
|
+
|
|
14
|
+
// Issue #45: very defensive error message here. We only throw this error when an error is thrown during compilation.
|
|
15
|
+
if(e.message.startsWith("Unexpected token ") && codeString.match(/\bclass\b/) && !codeString.match(/\bclass\b\s*\{/)) {
|
|
16
|
+
throw new Error(`${errorString ? `${errorString} ` : ""}\`class\` is a reserved word in JavaScript. Change \`class\` to \`this.class\` instead!`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
throw new Error(`${errorString}\nOriginal error message: ${e.message}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let argString = "";
|
|
23
|
+
if(!Array.isArray(varsInUse)) {
|
|
24
|
+
varsInUse = Array.from(varsInUse)
|
|
25
|
+
}
|
|
26
|
+
if(varsInUse.length > 0) {
|
|
27
|
+
argString = `{ ${varsInUse.join(", ")} }`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let code = `export default function(${argString}) { return ${codeString} };`;
|
|
31
|
+
|
|
32
|
+
let evaluated = await importFromString(code, {
|
|
33
|
+
filePath,
|
|
34
|
+
implicitExports: false,
|
|
35
|
+
}).then(mod => {
|
|
36
|
+
let fn = mod.default;
|
|
37
|
+
if(context) {
|
|
38
|
+
return fn.call(context, data);
|
|
39
|
+
}
|
|
40
|
+
return fn(data);
|
|
41
|
+
}).catch(e => {
|
|
42
|
+
throw new Error(`Error evaluating inline code: \`${codeString}\``, { cause: e });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return evaluated;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = {
|
|
49
|
+
evaluateInlineCode
|
|
50
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
const path = require("path");
|
|
1
|
+
const path = require("node:path");
|
|
2
2
|
const debug = require("debug")("Eleventy:WebC");
|
|
3
3
|
|
|
4
|
+
const { evaluateInlineCode } = require("./dynamicScript.js");
|
|
5
|
+
|
|
4
6
|
function relativePath(inputPath, newGlob) {
|
|
5
7
|
// project root
|
|
6
8
|
if(newGlob.startsWith("~/")) {
|
|
@@ -34,13 +36,11 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
34
36
|
let _WebC;
|
|
35
37
|
let globalComponentManager;
|
|
36
38
|
let componentsMap = false; // cache the glob search
|
|
37
|
-
let moduleScript;
|
|
38
39
|
let scopedHelpers = new Set(options.scopedHelpers);
|
|
39
40
|
|
|
40
41
|
eleventyConfig.on("eleventy.before", async () => {
|
|
41
|
-
//
|
|
42
|
-
let { WebC,
|
|
43
|
-
moduleScript = ModuleScript;
|
|
42
|
+
// Temporary workaround for ESM in CJS
|
|
43
|
+
let { WebC, ComponentManager } = await import("@11ty/webc");
|
|
44
44
|
_WebC = WebC;
|
|
45
45
|
globalComponentManager = new ComponentManager();
|
|
46
46
|
|
|
@@ -61,18 +61,20 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
61
61
|
permalink: function(contents, inputPath) {
|
|
62
62
|
if(contents && typeof contents === "string") {
|
|
63
63
|
return async (data) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
64
|
+
let combinedContextData = {
|
|
65
|
+
...this,
|
|
66
|
+
...data,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Hard to know if this is JavaScript code or just a raw string value.
|
|
70
|
+
return evaluateInlineCode(contents, {
|
|
71
|
+
filePath: inputPath,
|
|
72
|
+
context: combinedContextData,
|
|
73
|
+
data: combinedContextData,
|
|
74
|
+
}).catch(e => {
|
|
73
75
|
debug("Error evaluating dynamic permalink, returning raw string contents instead: %o\n%O", contents, e);
|
|
74
76
|
return contents;
|
|
75
|
-
}
|
|
77
|
+
});
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
80
|
|