@11ty/eleventy-plugin-webc 0.12.0-beta.7 → 0.12.0-beta.8
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/README.md +104 -0
- package/eleventyWebcPlugin.js +7 -4
- package/package.json +29 -34
- package/src/dynamicScript.js +3 -4
- package/src/eleventyWebcTemplate.js +16 -16
- package/src/eleventyWebcTransform.js +3 -4
package/README.md
CHANGED
|
@@ -34,3 +34,107 @@ This documentation has [moved to 11ty.dev](https://www.11ty.dev/docs/languages/w
|
|
|
34
34
|
* Bundler mode: Easily roll up the CSS and JS in-use by WebC components on a page for page-specific bundles. Dirt-simple critical CSS/JS to only load the code you need.
|
|
35
35
|
* For more complex templating needs, render any existing Eleventy template syntax (Liquid, markdown, Nunjucks, etc.) inside of WebC.
|
|
36
36
|
* Works great with [is-land](https://www.11ty.dev/docs/plugins/partial-hydration/) for web component hydration.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npm install @11ty/eleventy-plugin-webc
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
* `0.12.x` requires Eleventy `3.0.0` or newer and Node.js 22.18 or newer
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Add to Configuration File
|
|
49
|
+
|
|
50
|
+
Usually `eleventy.config.js`:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import eleventyWebc from "@11ty/eleventy-plugin-webc";
|
|
54
|
+
|
|
55
|
+
export default function(eleventyConfig) {
|
|
56
|
+
eleventyConfig.addPlugin(eleventyWebc);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### Output component CSS and JS
|
|
61
|
+
|
|
62
|
+
In a WebC layout:
|
|
63
|
+
|
|
64
|
+
```html
|
|
65
|
+
<head>
|
|
66
|
+
<style @raw="getBundle('css')" webc:keep></style>
|
|
67
|
+
<script type="module" @raw="getBundle('js')" webc:keep></script>
|
|
68
|
+
</head>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
In a Nunjucks or Liquid layout:
|
|
72
|
+
|
|
73
|
+
```html
|
|
74
|
+
<head>
|
|
75
|
+
<style>{% getBundle "css" %}</style>
|
|
76
|
+
<script type="module">{% getBundle "js" %}</script>
|
|
77
|
+
</head>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Customize with Options
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import eleventyWebc from "@11ty/eleventy-plugin-webc";
|
|
84
|
+
|
|
85
|
+
export default function(eleventyConfig) {
|
|
86
|
+
eleventyConfig.addPlugin(eleventyWebc, {
|
|
87
|
+
// Glob (or array of globs) of no-import global components, `npm:` prefix for packages
|
|
88
|
+
components: "_components/**/*.webc",
|
|
89
|
+
|
|
90
|
+
// Helpers only available under `webc.helpers` (e.g. `webc.helpers.css()`) instead of top-level
|
|
91
|
+
scopedHelpers: ["css", "js", "html"],
|
|
92
|
+
|
|
93
|
+
// Process WebC in all non-WebC `.html` output
|
|
94
|
+
useTransform: false,
|
|
95
|
+
|
|
96
|
+
// Extra global data for `useTransform`
|
|
97
|
+
transformData: {},
|
|
98
|
+
|
|
99
|
+
// Passed to the Bundle plugin, see https://www.11ty.dev/docs/plugins/bundle/
|
|
100
|
+
bundlePluginOptions: {
|
|
101
|
+
hoistDuplicateBundlesFor: ["css", "js"],
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
// Customize the WebC instance before each render (may be async)
|
|
105
|
+
before(webc) {},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Example
|
|
111
|
+
|
|
112
|
+
`_components/my-greeting.webc`:
|
|
113
|
+
|
|
114
|
+
```html
|
|
115
|
+
<p>Hello, <span @text="name"></span>!</p>
|
|
116
|
+
|
|
117
|
+
<style webc:scoped>
|
|
118
|
+
:host { color: rebeccapurple; }
|
|
119
|
+
</style>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`index.webc`:
|
|
123
|
+
|
|
124
|
+
```html
|
|
125
|
+
---
|
|
126
|
+
layout: base.webc
|
|
127
|
+
title: Possums
|
|
128
|
+
---
|
|
129
|
+
<h1 @text="title"></h1>
|
|
130
|
+
<my-greeting name="Possum"></my-greeting>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
To use components for only one page, add `webc.components` to its front matter (relative to the template, or the project root with `~/`):
|
|
134
|
+
|
|
135
|
+
```yaml
|
|
136
|
+
---
|
|
137
|
+
webc:
|
|
138
|
+
components: ./_page-components/*.webc
|
|
139
|
+
---
|
|
140
|
+
```
|
package/eleventyWebcPlugin.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
const templatePlugin = require("./src/eleventyWebcTemplate.js");
|
|
3
|
-
const transformPlugin = require("./src/eleventyWebcTransform.js");
|
|
1
|
+
import fs from "node:fs";
|
|
4
2
|
|
|
5
|
-
|
|
3
|
+
import templatePlugin from "./src/eleventyWebcTemplate.js";
|
|
4
|
+
import transformPlugin from "./src/eleventyWebcTransform.js";
|
|
5
|
+
|
|
6
|
+
const pkg = JSON.parse(fs.readFileSync(new URL("./package.json", import.meta.url), "utf8"));
|
|
7
|
+
|
|
8
|
+
export default function(eleventyConfig, options = {}) {
|
|
6
9
|
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
|
|
7
10
|
|
|
8
11
|
// Error for removed filters.
|
package/package.json
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@11ty/eleventy-plugin-webc",
|
|
3
|
-
"version": "0.12.0-beta.
|
|
3
|
+
"version": "0.12.0-beta.8",
|
|
4
4
|
"description": "WebC support for Eleventy adds for Single File Web Components",
|
|
5
5
|
"main": "eleventyWebcPlugin.js",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./eleventyWebcPlugin.js"
|
|
8
8
|
},
|
|
9
|
-
"
|
|
10
|
-
"access": "public"
|
|
11
|
-
},
|
|
12
|
-
"license": "MIT",
|
|
9
|
+
"type": "module",
|
|
13
10
|
"engines": {
|
|
14
|
-
"node": ">=
|
|
11
|
+
"node": ">=22.18"
|
|
15
12
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
18
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"eleventyWebcPlugin.js",
|
|
18
|
+
"src/"
|
|
19
|
+
],
|
|
19
20
|
"funding": {
|
|
20
21
|
"type": "opencollective",
|
|
21
22
|
"url": "https://opencollective.com/11ty"
|
|
@@ -26,33 +27,27 @@
|
|
|
26
27
|
"web-components",
|
|
27
28
|
"custom-elements"
|
|
28
29
|
],
|
|
29
|
-
"author":
|
|
30
|
+
"author": {
|
|
31
|
+
"name": "Zach Leatherman",
|
|
32
|
+
"url": "https://zachleat.com/"
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
30
35
|
"repository": {
|
|
31
36
|
"type": "git",
|
|
32
|
-
"url": "git://github.com/11ty/
|
|
33
|
-
|
|
34
|
-
"bugs": "https://github.com/11ty/eleventy-plugin-webc/issues",
|
|
35
|
-
"homepage": "https://github.com/11ty/eleventy-plugin-webc",
|
|
36
|
-
"ava": {
|
|
37
|
-
"failFast": true,
|
|
38
|
-
"files": [
|
|
39
|
-
"./test/test*.js",
|
|
40
|
-
"./test/test*.mjs"
|
|
41
|
-
],
|
|
42
|
-
"watchMode": {
|
|
43
|
-
"ignoreChanges": [
|
|
44
|
-
"**/_site/**",
|
|
45
|
-
".cache"
|
|
46
|
-
]
|
|
47
|
-
}
|
|
37
|
+
"url": "git+https://github.com/11ty/plugin-template-languages.git",
|
|
38
|
+
"directory": "webc"
|
|
48
39
|
},
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
40
|
+
"bugs": "https://github.com/11ty/plugin-template-languages/issues",
|
|
41
|
+
"homepage": "https://www.11ty.dev/docs/languages/webc/",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"test": "echo \"Error: run tests from root directory\" && exit 1"
|
|
53
44
|
},
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
|
|
45
|
+
"11ty": {
|
|
46
|
+
"compatibility": ">=3.0.0"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@11ty/webc": "^0.12.0-beta.5",
|
|
50
|
+
"obug": "^3.0.0",
|
|
51
|
+
"import-module-string": "^3.0.0"
|
|
57
52
|
}
|
|
58
53
|
}
|
package/src/dynamicScript.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { parseCode, walkCode, importFromString } from "import-module-string";
|
|
2
|
+
|
|
1
3
|
// TODO move this upstream into import-module-string
|
|
2
4
|
// Code cribbed from WebC’s attributeSerializer.js
|
|
3
5
|
async function evaluateInlineCode(codeString, options = {}) {
|
|
4
6
|
let { filePath, context, data } = Object.assign({ data: {} }, options);
|
|
5
7
|
|
|
6
|
-
let { parseCode, walkCode, importFromString } = await import("import-module-string");
|
|
7
8
|
let varsInUse = [];
|
|
8
9
|
try {
|
|
9
10
|
let {used} = walkCode(parseCode(codeString));
|
|
@@ -45,6 +46,4 @@ async function evaluateInlineCode(codeString, options = {}) {
|
|
|
45
46
|
return evaluated;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
evaluateInlineCode
|
|
50
|
-
};
|
|
49
|
+
export { evaluateInlineCode };
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { createDebug } from "obug";
|
|
3
|
+
import { WebC, ComponentManager } from "@11ty/webc";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
import { evaluateInlineCode } from "./dynamicScript.js";
|
|
6
|
+
|
|
7
|
+
const debug = createDebug("Eleventy:WebC");
|
|
5
8
|
|
|
6
9
|
function relativePath(inputPath, newGlob) {
|
|
7
10
|
// project root
|
|
@@ -19,29 +22,26 @@ function relativePath(inputPath, newGlob) {
|
|
|
19
22
|
function addContextToJavaScriptFunction(data, fn) {
|
|
20
23
|
let CONTEXT_KEYS = ["eleventy", "page"];
|
|
21
24
|
return function (...args) {
|
|
25
|
+
let context = Object.create(this ?? null);
|
|
22
26
|
for (let key of CONTEXT_KEYS) {
|
|
23
27
|
if (data && data[key]) {
|
|
24
|
-
|
|
28
|
+
context[key] = data[key];
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
return fn.call(
|
|
32
|
+
return fn.call(context, ...args);
|
|
29
33
|
};
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
export default function(eleventyConfig, options = {}) {
|
|
33
37
|
// TODO remove this when WebC is moved out of plugin-land into core.
|
|
34
38
|
eleventyConfig.addTemplateFormats("webc");
|
|
35
39
|
|
|
36
|
-
let _WebC;
|
|
37
40
|
let globalComponentManager;
|
|
38
41
|
let componentsMap = false; // cache the glob search
|
|
39
42
|
let scopedHelpers = new Set(options.scopedHelpers);
|
|
40
43
|
|
|
41
|
-
eleventyConfig.on("eleventy.before",
|
|
42
|
-
// Temporary workaround for ESM in CJS
|
|
43
|
-
let { WebC, ComponentManager } = await import("@11ty/webc");
|
|
44
|
-
_WebC = WebC;
|
|
44
|
+
eleventyConfig.on("eleventy.before", () => {
|
|
45
45
|
globalComponentManager = new ComponentManager();
|
|
46
46
|
|
|
47
47
|
if(options.components) {
|
|
@@ -50,9 +50,9 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
let templateConfig;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
eleventyConfig.on("eleventy.config", (cfg) => {
|
|
54
|
+
templateConfig = cfg;
|
|
55
|
+
});
|
|
56
56
|
|
|
57
57
|
eleventyConfig.addExtension("webc", {
|
|
58
58
|
outputFileExtension: "html",
|
|
@@ -83,7 +83,7 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
83
83
|
},
|
|
84
84
|
|
|
85
85
|
compile: async function(inputContent, inputPath) {
|
|
86
|
-
let page = new
|
|
86
|
+
let page = new WebC();
|
|
87
87
|
|
|
88
88
|
page.setGlobalComponentManager(globalComponentManager);
|
|
89
89
|
page.setBundlerMode(true);
|
|
@@ -129,7 +129,7 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
129
129
|
};
|
|
130
130
|
|
|
131
131
|
if(data.webc?.components) {
|
|
132
|
-
setupObject.components =
|
|
132
|
+
setupObject.components = WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
if(options.before && typeof options.before === "function") {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { WebC } from "@11ty/webc";
|
|
2
|
+
|
|
3
|
+
export default function(eleventyConfig, options = {}) {
|
|
2
4
|
let componentsMap = false; // cache the glob search
|
|
3
5
|
|
|
4
6
|
eleventyConfig.on("eleventy.before", () => {
|
|
@@ -17,7 +19,6 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
17
19
|
// TODO prevent double processing with the Render plugin with WebC
|
|
18
20
|
// https://www.11ty.dev/docs/plugins/render/
|
|
19
21
|
try {
|
|
20
|
-
const { WebC } = await import("@11ty/webc");
|
|
21
22
|
let page = new WebC();
|
|
22
23
|
|
|
23
24
|
if(componentsMap === false && options.components) {
|
|
@@ -41,7 +42,5 @@ module.exports = function(eleventyConfig, options = {}) {
|
|
|
41
42
|
console.error( `[11ty/eleventy-plugin-webc] Error transforming ${this.inputPath}`, e );
|
|
42
43
|
throw e;
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
-
return content;
|
|
46
45
|
});
|
|
47
46
|
}
|