@11ty/eleventy-plugin-webc 0.3.1 → 0.5.0

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/.editorconfig ADDED
@@ -0,0 +1,15 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = tab
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ insert_final_newline = false
8
+ trim_trailing_whitespace = true
9
+ charset = utf-8
10
+
11
+ [*.js]
12
+ insert_final_newline = true
13
+
14
+ [test/*.js]
15
+ trim_trailing_whitespace = false
package/README.md CHANGED
@@ -4,261 +4,33 @@
4
4
 
5
5
  Adds support for [WebC, the single file web component format](https://github.com/11ty/webc), to Eleventy.
6
6
 
7
- ## Features
8
-
9
- * All of the [framework-independent WebC features](https://github.com/11ty/webc#features) out of the box.
10
- * First-class incremental support (for page templates, components, and Eleventy layouts): use with `--incremental`
11
- * Tired of importing components?
12
- * Configure no-import global web components
13
- * Configure no-import components in the data cascade to apply to a single template, a directory of templates, or a complex hierarchy of folders-o-templates.
14
- * Easiliy 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.
15
- * Components can roll up CSS/JS assets to arbitrary buckets for custom use!
16
- * For more complex templating needs, you _can_ render any existing Eleventy template syntax inside of WebC.
17
- * Works great with out of the box with [is-land](https://www.11ty.dev/docs/plugins/partial-hydration/)
7
+ * [This documentation has moved to 11ty.dev](https://www.11ty.dev/docs/languages/webc/).
8
+ * Watch the [crash course in Eleventy WebC on YouTube](https://www.youtube.com/watch?v=X-Bpjrkz-V8).
9
+ * Watch the [Interactive Components tutorial on YouTube](https://www.youtube.com/watch?v=p0wDUK0Z5Nw)
18
10
 
19
- ## ➡ [Documentation](https://www.11ty.dev/docs/languages/webc/)
11
+ [![npm Version](https://img.shields.io/npm/v/@11ty/eleventy-plugin-webc.svg?style=for-the-badge)](https://www.npmjs.com/package/@11ty/eleventy-plugin-webc)
20
12
 
21
- - Please star [Eleventy on GitHub](https://github.com/11ty/eleventy/)!
13
+ - Star [Eleventy on GitHub](https://github.com/11ty/eleventy/)!
22
14
  - Follow us on Twitter [@eleven_ty](https://twitter.com/eleven_ty)
23
15
  - Support [11ty on Open Collective](https://opencollective.com/11ty)
24
16
  - Subscribe to our [YouTube channel](https://11ty.dev/youtube)
25
- - [11ty on npm](https://www.npmjs.com/org/11ty)
26
- - [11ty on GitHub](https://github.com/11ty)
27
-
28
- [![npm Version](https://img.shields.io/npm/v/@11ty/eleventy-plugin-webc.svg?style=for-the-badge)](https://www.npmjs.com/package/@11ty/eleventy-plugin-webc)
29
-
30
- ## Installation
31
-
32
- You’ve found this early! It’s not yet on npm.
33
-
34
- <!--
35
- ```
36
- npm install @11ty/eleventy-plugin-webc
37
- ```
38
- -->
39
-
40
- ## Usage
41
-
42
- Enable the plugin in your Eleventy configuration file:
43
-
44
- ```js
45
- const pluginWebc = require("@11ty/eleventy-plugin-webc");
46
-
47
- module.exports = function(eleventyConfig) {
48
- eleventyConfig.addPlugin(pluginWebc);
49
- };
50
- ```
51
-
52
- ### Full options list
53
-
54
- ```js
55
- const pluginWebc = require("@11ty/eleventy-plugin-webc");
56
-
57
- module.exports = function(eleventyConfig) {
58
- eleventyConfig.addPlugin(pluginWebc, {
59
- // Glob to find no-import global components
60
- components: "_includes/components/**/*.webc", // defaults to `false`
61
- });
62
- };
63
- ```
64
-
65
- ## Examples
66
-
67
- Add a new `.webc` file to your Eleventy input directory and it will render as WebC to your output directory (`_site` by default).
68
-
69
- `my-page.webc`:
70
-
71
- ```html
72
- <!doctype html>
73
- <html lang="en">
74
- <head>
75
- <meta charset="utf-8">
76
- <title>WebC Example</title>
77
- </head>
78
- <body>
79
- WebC *is* HTML.
80
- </body>
81
- </html>
82
- ```
83
-
84
- ### Use Front Matter
85
-
86
- WebC in Eleventy works automatically with standard Eleventy conventions for [front matter](https://www.11ty.dev/docs/data-frontmatter/) (though front matter in Eleventy is _optional_).
87
-
88
- Notably you can use `yaml`, `json`, `js`, or [add your own data format](https://www.11ty.dev/docs/data-frontmatter-customize/) for front matter in Eleventy.
89
-
90
- `with-front-matter.webc` (using `_includes/my-layout.webc` as an [Eleventy layout](https://www.11ty.dev/docs/layouts/)):
91
-
92
- ```html
93
- ---
94
- layout: "my-layout.webc"
95
- ---
96
- <my-webc-component>WebC *is* HTML.</my-webc-component>
97
- ```
98
-
99
- `_includes/my-layout.webc`:
100
-
101
- ```html
102
- <!doctype html>
103
- <html lang="en">
104
- <head>
105
- <meta charset="utf-8">
106
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
107
- <meta name="description" content="">
108
- <title></title>
109
- </head>
110
- <body @html="this.content"></body>
111
- </html>
112
- ```
113
-
114
- ### Components
115
-
116
- There are a few ways to use web components here:
117
-
118
- 1. You can use `webc:import` inside of your components to import another component directly.
119
- 2. Use global no-import components specified in your config file
120
- 3. Specify a glob of no-import components at a directory or template level in the data cascade
121
-
122
- Notably, WebC components are not restricted to the same naming limitations as custom elements (they do not require a dash in the name). The [WebC documentation has all of the detail on how to use and configure WebC components](https://github.com/11ty/webc#html-imports-kidding-kinda).
123
-
124
- #### Global no-import Components
125
17
 
126
- Use the `components` property in the Full options list in your Eleventy configuration file to specify project-wide WebC component files available for use in any page.
18
+ ## [Documentation](https://www.11ty.dev/docs/languages/webc/)
127
19
 
128
- #### Specify no-import Components in the Data Cascade
20
+ This documentation has [moved to 11ty.dev](https://www.11ty.dev/docs/languages/webc/).
129
21
 
130
- You can also use and configure specific components as part of the data cascade as well (global to a folder or a specific template) by assigning a glob (or array of globs) to `webc.components`, like so:
131
-
132
- `my-directory/my-page.webc`:
133
-
134
- ```html
135
- ---
136
- layout: "my-layout.webc"
137
- webc:
138
- components: "./webc/*.webc"
139
- ---
140
- <my-webc-component>WebC *is* HTML.</my-webc-component>
141
- ```
142
-
143
- * Usage warning: By default these paths are relative to the template file. If you’re setting this in the data cascade to apply multiple child folders deep, it might be better to use the global no-import components option above OR to use `~/` as a prefix (e.g. `~/my-directory/webc/*.webc`) to alias to the project’s root directory.
144
-
145
- ### CSS and JS
146
-
147
- [Eleventy Layouts](https://www.11ty.dev/docs/layouts/) can bundle any specific page’s assets (CSS and JS used by components on the page). These are automatically rolled up when a component uses `<script>` or `<style>`. You can use this to easily implement component-driven Critical CSS.
148
-
149
- `_includes/webc/my-webc-component.webc`:
150
-
151
- ```html
152
- <style>/* This is component CSS */</style>
153
- <script>/* This is component JS */</script>
154
- ```
155
-
156
- `_includes/layout.webc`:
157
-
158
- ```html
159
- <!doctype html>
160
- <html lang="en">
161
- <head>
162
- <meta charset="utf-8">
163
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
164
- <meta name="description" content="">
165
- <title></title>
166
- <style @html="this.getCSS(this.page.url)"></style>
167
- <script @html="this.getJS(this.page.url)"></script>
168
- </head>
169
- <body @html="this.content"></body>
170
- </html>
171
- ```
172
-
173
- #### Asset bucketing
174
-
175
- Components can use the `webc:bucket` feature to output to any arbitrary bucket name for compartmentalization at the component level.
176
-
177
- `_includes/webc/my-webc-component.webc`:
178
-
179
- ```html
180
- <style>/* This CSS is put into the default bucket */</style>
181
- <script>/* This JS is put into the default bucket */</script>
182
- <style webc:bucket="defer">/* This CSS is put into the `defer` bucket */</style>
183
- <script webc:bucket="defer">/* This JS is put into the `defer` bucket */</script>
184
- ```
185
-
186
- ```html
187
- <!doctype html>
188
- <html lang="en">
189
- <head>
190
- <meta charset="utf-8">
191
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
192
- <meta name="description" content="">
193
- <title></title>
194
- <!-- Default bucket -->
195
- <style @html="this.getCSS(this.page.url)"></style>
196
- <script @html="this.getJS(this.page.url)"></script>
197
- </head>
198
- <body>
199
- <template webc:nokeep @html="this.content"></template>
200
-
201
- <!-- `defer` bucket -->
202
- <style @html="this.getCSS(this.page.url, 'defer')"></style>
203
- <script @html="this.getJS(this.page.url, 'defer')"></script>
204
- </body>
205
- </html>
206
- ```
207
-
208
- ### Use any Eleventy Template Syntax
209
-
210
- We’ve wired up [WebC’s Custom Transforms feature](https://github.com/11ty/webc#custom-transforms) `webc:type="11ty"` to the [Eleventy Render plugin](https://www.11ty.dev/docs/plugins/render/) to allow you to use existing Eleventy template syntax inside of WebC.
211
-
212
- Use the `11ty:type` attribute to specify a [valid template syntax](https://www.11ty.dev/docs/plugins/render/#rendertemplate).
213
-
214
- `my-page.webc`:
215
-
216
- ```
217
- ---
218
- frontmatterdata: "Hello from Front Matter"
219
- ---
220
- <template webc:type="11ty" 11ty:type="liquid,md">
221
- {% assign t = "Liquid in WebC" %}
222
- ## {{ t }}
223
-
224
- _{{ frontmatterdata }}_
225
- </template>
226
- ```
227
-
228
- You have full access to the data cascade here (see `frontmatterdata` in the example above).
229
-
230
- ### Use with `is-land`
231
-
232
- You can also use this out of the box with Eleventy’s [`is-land` component for web component hydration](https://www.11ty.dev/docs/plugins/partial-hydration/).
233
-
234
- At the component level, components can declare their own is-land loading conditions.
235
-
236
- _(Some features here require `is-land` 1.0.1 or newer)_
237
-
238
- `_includes/webc/my-webc-component.webc`:
239
-
240
- ```html
241
- <is-land on:visible>
242
- <style media="module/island" webc:keep>
243
- /* This is some is-land on-visible CSS */
244
- </style>
245
- <script type="module/island" webc:keep>
246
- console.log("This is some is-land on-visible JavaScript");
247
- </script>
248
-
249
- <link rel="stylesheet" media="style/island" href="some-arbitrary-css.css">
250
- <script type="module/island" src="some-arbitrary-js.js"></script>
251
- </is-land>
252
-
253
- <is-land on:interaction>
254
- <style media="module/island" webc:keep>
255
- /* This is some is-land on-interaction CSS */
256
- </style>
257
- <script type="module/island" webc:keep>
258
- console.log("This is some is-land on-interaction JavaScript");
259
- </script>
22
+ ## Features
260
23
 
261
- <link rel="stylesheet" media="style/island" href="some-arbitrary-css.css">
262
- <script type="module/island" src="some-arbitrary-js.js"></script>
263
- </is-land>
264
- ```
24
+ * Brings first-class **components** to Eleventy.
25
+ * Expand any HTML element (including custom elements) to HTML with defined conventions from web standards.
26
+ * This means that Web Components created with WebC are compatible with server-side rendering (without duplicating author-written markup)
27
+ * WebC components are [Progressive Enhancement friendly](https://www.youtube.com/watch?v=p0wDUK0Z5Nw).
28
+ * Get first-class **incremental builds** (for page templates, components, and Eleventy layouts) when [used with `--incremental`](https://www.11ty.dev/docs/usage/#incremental-for-partial-incremental-builds)
29
+ * Streaming friendly (stream on the Edge 👀)
30
+ * Easily scope component CSS (or use your own scoping utility).
31
+ * Tired of importing components? Use global or per-page no-import components.
32
+ * Shadow DOM friendly (works with or without Shadow DOM)
33
+ * All configuration extensions/hooks into WebC are async-friendly out of the box.
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
+ * For more complex templating needs, render any existing Eleventy template syntax (Liquid, markdown, Nunjucks, etc.) inside of WebC.
36
+ * Works great with [is-land](https://www.11ty.dev/docs/plugins/partial-hydration/) for web component hydration.
@@ -1,27 +1,6 @@
1
1
  const pkg = require("./package.json");
2
- const path = require("path");
3
- const { EleventyRenderPlugin } = require("@11ty/eleventy");
4
- const EleventyRenderManager = EleventyRenderPlugin.RenderManager;
5
-
6
- const CodeManager = require("./src/codeManager.js");
7
- const WebCIncremental = require("./src/incremental.js");
8
-
9
- function relativePath(inputPath, newGlob) {
10
- // project root
11
- if(newGlob.startsWith("~/")) {
12
- let rootRelativePath = "." + newGlob.slice(1);
13
- return rootRelativePath;
14
- }
15
-
16
- let { dir } = path.parse(inputPath);
17
- // globs must have forward slashes (even on Windows)
18
- let templateRelativePath = path.join(dir, newGlob).split(path.sep).join("/");
19
- return templateRelativePath;
20
- }
21
-
22
- /* TODO
23
- * Use JavaScript functions or universal shortcodes as built-in components?
24
- */
2
+ const templatePlugin = require("./src/eleventyWebcTemplate.js");
3
+ const transformPlugin = require("./src/eleventyWebcTransform.js");
25
4
 
26
5
  module.exports = function(eleventyConfig, options = {}) {
27
6
  try {
@@ -30,126 +9,28 @@ module.exports = function(eleventyConfig, options = {}) {
30
9
  console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
31
10
  }
32
11
 
12
+ let filters = Object.assign({
13
+ css: "webcGetCss",
14
+ js: "webcGetJs",
15
+ }, options.filters);
16
+
33
17
  options = Object.assign({
34
18
  components: false, // glob for no-import global components
19
+ useTransform: false, // global transform
20
+ transformData: {}, // extra global data for transforms specifically
35
21
  }, options);
36
22
 
23
+ options.filters = filters;
24
+
37
25
  if(options.components) {
38
26
  eleventyConfig.addWatchTarget(options.components);
39
- }
40
27
 
41
- eleventyConfig.addTemplateFormats("webc");
42
-
43
- let renderManager = new EleventyRenderManager();
44
- let cssManager = new CodeManager();
45
- let jsManager = new CodeManager();
46
-
47
- eleventyConfig.on("eleventy.before", () => {
48
- cssManager.reset();
49
- jsManager.reset();
50
- });
51
-
52
- function getCss(pageUrl, bucket = "default") {
53
- return cssManager.getForPage(pageUrl, bucket);
54
- }
55
-
56
- function getJs(pageUrl, bucket = "default") {
57
- return jsManager.getForPage(pageUrl, bucket);
28
+ // TODO add to eleventy ignores (if not in _includes folder)
58
29
  }
59
30
 
60
- // TODO
61
- // eleventyConfig.addFilter("webcGetCSS", (url, bucket) => getCss(url, bucket));
62
- // eleventyConfig.addFilter("webcGetJS", (url, bucket) => getJs(url, bucket));
63
-
64
- let incremental = new WebCIncremental();
65
-
66
- eleventyConfig.on("eleventy.layouts", layouts => {
67
- incremental.setLayouts(layouts);
68
- });
69
-
70
- eleventyConfig.on("eleventy.before", () => {
71
- incremental.setComponents(options.components);
72
- });
73
-
74
- eleventyConfig.addExtension("webc", {
75
- outputFileExtension: "html",
31
+ templatePlugin(eleventyConfig, options);
76
32
 
77
- init: async function() {
78
- // For ESM in CJS
79
- let e = await import("@11ty/webc");
80
- incremental.setWebC(e.WebC);
81
-
82
- if(incremental.needsComponents()) {
83
- incremental.setComponents(options.components);
84
- }
85
- },
86
-
87
- isIncrementalMatch: function (incrementalFilePath) {
88
- // Eleventy layouts don’t appear directly in the WebC component graph, so we use the `eleventy.layouts` map here
89
- if(incremental.isTemplateUsingLayout(this.inputPath, incrementalFilePath)) {
90
- return true;
91
- }
92
-
93
- let {page, setup} = incremental.get(this.inputPath);
94
- if(page && setup) {
95
- if(page.getComponents(setup).includes(incrementalFilePath)) {
96
- return true;
97
- }
98
- }
99
-
100
- return false;
101
- },
102
-
103
- compile: async function(inputContent, inputPath) {
104
- let page = incremental.add(inputContent, inputPath);
105
-
106
- page.setHelper("getCSS", function(url, bucket) {
107
- return getCss(url, bucket);
108
- });
109
-
110
- page.setHelper("getJS", function(url, bucket) {
111
- return getJs(url, bucket);
112
- });
113
-
114
- page.setTransform("11ty", async function(content) {
115
- let syntax = this["11ty:type"];
116
- if(syntax) {
117
- let fn = await renderManager.compile(content, syntax);
118
- return renderManager.render(fn, this, {});
119
- }
120
- return content;
121
- });
122
-
123
- return async (data) => {
124
- let setupObject = { data };
125
- if(data.webc?.components) {
126
- setupObject.components = incremental.getComponentMap(relativePath(data.page.inputPath, data.webc.components));
127
- }
128
-
129
- let setup = await page.setup(setupObject);
130
- incremental.addSetup(inputPath, setup);
131
-
132
- let { ast, serializer } = setup;
133
- let { html, css, js, buckets } = await serializer.compile(ast);
134
-
135
- cssManager.addToPage(data.page.url, css, "default");
136
-
137
- if(buckets.css) {
138
- for(let bucket in buckets.css) {
139
- cssManager.addToPage(data.page.url, buckets.css[bucket], bucket);
140
- }
141
- }
142
-
143
- jsManager.addToPage(data.page.url, js, "default");
144
-
145
- if(buckets.js) {
146
- for(let bucket in buckets.js) {
147
- jsManager.addToPage(data.page.url, buckets.js[bucket], bucket);
148
- }
149
- }
150
-
151
- return html;
152
- };
153
- }
154
- });
155
- }
33
+ if(options.useTransform) {
34
+ transformPlugin(eleventyConfig, options);
35
+ }
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@11ty/eleventy-plugin-webc",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "WebC support for Eleventy adds for Single File Web Components",
5
5
  "main": "eleventyWebcPlugin.js",
6
6
  "scripts": {
@@ -13,9 +13,9 @@
13
13
  "engines": {
14
14
  "node": ">=14.18"
15
15
  },
16
- "11ty": {
17
- "compatibility": ">=2.0.0-canary.16"
18
- },
16
+ "11ty": {
17
+ "compatibility": ">=2.0.0-canary.16"
18
+ },
19
19
  "funding": {
20
20
  "type": "opencollective",
21
21
  "url": "https://opencollective.com/11ty"
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@11ty/eleventy": "^2.0.0-canary.16",
42
- "@11ty/webc": "^0.3.1"
42
+ "@11ty/webc": "^0.5.2"
43
43
  },
44
44
  "devDependencies": {
45
45
  "ava": "^4.3.3"
@@ -0,0 +1,140 @@
1
+ const path = require("path");
2
+
3
+ const { EleventyRenderPlugin } = require("@11ty/eleventy");
4
+ const EleventyRenderManager = EleventyRenderPlugin.RenderManager;
5
+
6
+ const CodeManager = require("./codeManager.js");
7
+ const WebCIncremental = require("./incremental.js");
8
+
9
+ function relativePath(inputPath, newGlob) {
10
+ // project root
11
+ if(newGlob.startsWith("~/")) {
12
+ let rootRelativePath = "." + newGlob.slice(1);
13
+ return rootRelativePath;
14
+ }
15
+
16
+ let { dir } = path.parse(inputPath);
17
+ // globs must have forward slashes (even on Windows)
18
+ let templateRelativePath = path.join(dir, newGlob).split(path.sep).join("/");
19
+ return templateRelativePath;
20
+ }
21
+
22
+ module.exports = function(eleventyConfig, options = {}) {
23
+ eleventyConfig.addTemplateFormats("webc");
24
+
25
+ let renderManager = new EleventyRenderManager();
26
+ let cssManager = new CodeManager();
27
+ let jsManager = new CodeManager();
28
+ let incremental = new WebCIncremental();
29
+
30
+ eleventyConfig.on("eleventy.before", () => {
31
+ cssManager.reset();
32
+ jsManager.reset();
33
+ incremental.setComponents(options.components);
34
+ });
35
+
36
+ function getCss(pageUrl, bucket = "default") {
37
+ return cssManager.getForPage(pageUrl, bucket);
38
+ }
39
+
40
+ function getJs(pageUrl, bucket = "default") {
41
+ return jsManager.getForPage(pageUrl, bucket);
42
+ }
43
+
44
+ if(options.filters.css) {
45
+ eleventyConfig.addFilter(options.filters.css, (url, bucket) => getCss(url, bucket));
46
+ }
47
+ if(options.filters.js) {
48
+ eleventyConfig.addFilter(options.filters.js, (url, bucket) => getJs(url, bucket));
49
+ }
50
+
51
+ eleventyConfig.on("eleventy.layouts", layouts => {
52
+ incremental.setLayouts(layouts);
53
+ });
54
+
55
+ eleventyConfig.addExtension("webc", {
56
+ outputFileExtension: "html",
57
+
58
+ init: async function() {
59
+ // For ESM in CJS
60
+ let { WebC } = await import("@11ty/webc");
61
+ incremental.setWebC(WebC);
62
+
63
+ if(incremental.needsComponents()) {
64
+ incremental.setComponents(options.components);
65
+ }
66
+ },
67
+
68
+ isIncrementalMatch: function (incrementalFilePath) {
69
+ // Eleventy layouts don’t appear directly in the WebC component graph, so we use the `eleventy.layouts` map here
70
+ if(incremental.isTemplateUsingLayout(this.inputPath, incrementalFilePath)) {
71
+ return true;
72
+ }
73
+
74
+ let {page, setup} = incremental.get(this.inputPath);
75
+ if(page && setup) {
76
+ if(page.getComponents(setup).includes(incrementalFilePath)) {
77
+ return true;
78
+ }
79
+ }
80
+
81
+ return false;
82
+ },
83
+
84
+ compile: async function(inputContent, inputPath) {
85
+ let page = incremental.add(inputContent, inputPath);
86
+
87
+ // Add Eleventy JavaScript Functions as WebC helpers (Universal Filters also populate into these)
88
+ for(let helperName in this.config.javascriptFunctions) {
89
+ page.setHelper(helperName, this.config.javascriptFunctions[helperName]);
90
+ }
91
+
92
+ // Support both casings (I prefer getCss, but yeah)
93
+ page.setHelper("getCss", (url, bucket) => getCss(url, bucket));
94
+ page.setHelper("getCSS", (url, bucket) => getCss(url, bucket));
95
+
96
+ page.setHelper("getJs", (url, bucket) => getJs(url, bucket));
97
+ page.setHelper("getJS", (url, bucket) => getJs(url, bucket));
98
+
99
+ page.setTransform("11ty", async function(content) {
100
+ let syntax = this["11ty:type"];
101
+ if(syntax) {
102
+ let fn = await renderManager.compile(content, syntax);
103
+ return renderManager.render(fn, this, {});
104
+ }
105
+ return content;
106
+ });
107
+
108
+ return async (data) => {
109
+ let setupObject = { data };
110
+ if(data.webc?.components) {
111
+ setupObject.components = incremental.getComponentMap(relativePath(data.page.inputPath, data.webc.components));
112
+ }
113
+
114
+ let setup = await page.setup(setupObject);
115
+ incremental.addSetup(inputPath, setup);
116
+
117
+ let { ast, serializer } = setup;
118
+ let { html, css, js, buckets } = await serializer.compile(ast);
119
+
120
+ cssManager.addToPage(data.page.url, css, "default");
121
+
122
+ if(buckets.css) {
123
+ for(let bucket in buckets.css) {
124
+ cssManager.addToPage(data.page.url, buckets.css[bucket], bucket);
125
+ }
126
+ }
127
+
128
+ jsManager.addToPage(data.page.url, js, "default");
129
+
130
+ if(buckets.js) {
131
+ for(let bucket in buckets.js) {
132
+ jsManager.addToPage(data.page.url, buckets.js[bucket], bucket);
133
+ }
134
+ }
135
+
136
+ return html;
137
+ };
138
+ }
139
+ });
140
+ };
@@ -0,0 +1,40 @@
1
+ module.exports = function(eleventyConfig, options = {}) {
2
+ let componentsMap = false; // cache the glob search
3
+
4
+ eleventyConfig.on("eleventy.before", () => {
5
+ componentsMap = false;
6
+ });
7
+
8
+ eleventyConfig.addTransform("@11ty/eleventy-plugin-webc", async function(content) {
9
+ // Skip non-.html output
10
+ // Skip .webc input
11
+ if(this.inputPath.endsWith(".webc") || !(this.outputPath || "").endsWith(".html")) {
12
+ return content;
13
+ }
14
+
15
+ // TODO prevent double processing with the Render plugin with WebC
16
+ // https://www.11ty.dev/docs/plugins/render/
17
+ try {
18
+ const { WebC } = await import("@11ty/webc");
19
+ let page = new WebC();
20
+
21
+ if(componentsMap === false && options.components) {
22
+ componentsMap = WebC.getComponentsMap(options.components); // "./_includes/webc/*.webc"
23
+ }
24
+
25
+ page.defineComponents(componentsMap);
26
+ page.setContent(content, this.outputPath);
27
+
28
+ let { html } = await page.compile({
29
+ // global data
30
+ data: options.transformData
31
+ });
32
+ return html;
33
+ } catch(e) {
34
+ console.error( `[11ty/eleventy-plugin-webc] Error transforming ${this.inputPath}`, e );
35
+ throw e;
36
+ }
37
+
38
+ return content;
39
+ });
40
+ }
@@ -20,12 +20,12 @@ class WebCIncremental {
20
20
  this.globalComponentsMap = map;
21
21
  }
22
22
  }
23
-
23
+
24
24
  getComponentMap(glob) {
25
25
  if(!this.webc) {
26
26
  return false;
27
27
  }
28
-
28
+
29
29
  let WebC = this.webc;
30
30
  let components = WebC.getComponentsMap(glob);
31
31
  // console.log( glob, Object.keys(components).length );
@@ -46,6 +46,16 @@ class WebCIncremental {
46
46
  add(inputContent, inputPath) {
47
47
  let WebC = this.webc;
48
48
  let page = new WebC();
49
+
50
+ // Explicitly disable bundler mode for layouts
51
+ if(this.layouts && this.layouts[inputPath]) {
52
+ page.setBundlerMode(false);
53
+ page.setReprocessingMode(false);
54
+ } else {
55
+ page.setBundlerMode(true);
56
+ page.setReprocessingMode(true);
57
+ }
58
+
49
59
  page.setContent(inputContent, inputPath);
50
60
 
51
61
  if(this.globalComponentsMap) {
@@ -78,4 +88,4 @@ class WebCIncremental {
78
88
  }
79
89
  }
80
90
 
81
- module.exports = WebCIncremental;
91
+ module.exports = WebCIncremental;
@@ -1,17 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <meta name="description" content="">
7
- <title></title>
8
- <style @html="this.getCSS(this.page.url)"></style>
9
- <script @html="this.getJS(this.page.url)"></script>
10
- </head>
11
- <body>
12
- <template webc:nokeep @html="this.content"></template>
13
-
14
- <style @html="this.getCSS(this.page.url, 'defer')"></style>
15
- <script @html="this.getJS(this.page.url, 'defer')"></script>
16
- </body>
17
- </html>
@@ -1,12 +0,0 @@
1
- const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");
2
-
3
- module.exports = function (eleventyConfig) {
4
- eleventyConfig.addPlugin(EleventyWebcPlugin);
5
-
6
- return {
7
- dir: {
8
- includes: "_includes",
9
- layouts: "_layouts",
10
- }
11
- }
12
- }
@@ -1,9 +0,0 @@
1
- ---
2
- frontmatterdata: "HELLO FROM FRONT MATTER"
3
- layout: layout.webc
4
- ---
5
- <say-hello></say-hello>
6
- <say-hello></say-hello>
7
- WHO IS THIS
8
- hi
9
- <span @html="this.frontmatterdata"></span>
@@ -1 +0,0 @@
1
- This is a component.
@@ -1,17 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <meta name="description" content="">
7
- <title></title>
8
- <style @html="this.getCSS(this.page.url)"></style>
9
- <script @html="this.getJS(this.page.url)"></script>
10
- </head>
11
- <body>
12
- <template webc:nokeep @html="this.content"></template>
13
-
14
- <style @html="this.getCSS(this.page.url, 'defer')"></style>
15
- <script @html="this.getJS(this.page.url, 'defer')"></script>
16
- </body>
17
- </html>
@@ -1,15 +0,0 @@
1
- const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");
2
-
3
- module.exports = function (eleventyConfig) {
4
- eleventyConfig.addPlugin(EleventyWebcPlugin, {
5
- components: "test/sample-2/_includes/**/*.webc"
6
- });
7
- // eleventyConfig.addPlugin(EleventyWebcPlugin);
8
-
9
- return {
10
- dir: {
11
- includes: "_includes",
12
- layouts: "_layouts",
13
- }
14
- }
15
- }
@@ -1,9 +0,0 @@
1
- ---
2
- frontmatterdata: "HELLO FROM FRONT MATTER"
3
- layout: layout.webc
4
- ---
5
- <component></component>
6
- <component></component>
7
- WHO IS THIS
8
- hi
9
- <span @html="this.frontmatterdata"></span>
@@ -1,17 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <meta name="description" content="">
7
- <title></title>
8
- <style @html="this.getCSS(this.page.url)"></style>
9
- <script @html="this.getJS(this.page.url)"></script>
10
- </head>
11
- <body>
12
- <template webc:nokeep @html="this.content"></template>
13
-
14
- <style @html="this.getCSS(this.page.url, 'defer')"></style>
15
- <script @html="this.getJS(this.page.url, 'defer')"></script>
16
- </body>
17
- </html>
@@ -1,12 +0,0 @@
1
- const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");
2
-
3
- module.exports = function (eleventyConfig) {
4
- eleventyConfig.addPlugin(EleventyWebcPlugin);
5
-
6
- return {
7
- dir: {
8
- includes: "_includes",
9
- layouts: "_layouts",
10
- }
11
- }
12
- }
@@ -1,11 +0,0 @@
1
- ---
2
- frontmatterdata: "HELLO FROM FRONT MATTER"
3
- layout: layout.webc
4
- webc:
5
- components: "~/test/sample-page-global-components/_includes/page1-components/*.webc"
6
- ---
7
- <say-hello></say-hello>
8
- <say-hello></say-hello>
9
- WHO IS THIS
10
- hi
11
- <span @html="this.frontmatterdata"></span>
@@ -1,17 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <meta name="description" content="">
7
- <title></title>
8
- <style @html="this.getCSS(this.page.url)"></style>
9
- <script @html="this.getJS(this.page.url)"></script>
10
- </head>
11
- <body>
12
- <template webc:nokeep @html="this.content"></template>
13
-
14
- <style @html="this.getCSS(this.page.url, 'defer')"></style>
15
- <script @html="this.getJS(this.page.url, 'defer')"></script>
16
- </body>
17
- </html>
@@ -1,12 +0,0 @@
1
- const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");
2
-
3
- module.exports = function (eleventyConfig) {
4
- eleventyConfig.addPlugin(EleventyWebcPlugin);
5
-
6
- return {
7
- dir: {
8
- includes: "_includes",
9
- layouts: "_layouts",
10
- }
11
- }
12
- }
@@ -1,11 +0,0 @@
1
- ---
2
- frontmatterdata: "HELLO FROM FRONT MATTER"
3
- layout: layout.webc
4
- webc:
5
- components: "./_includes/page1-components/*.webc"
6
- ---
7
- <say-hello></say-hello>
8
- <say-hello></say-hello>
9
- WHO IS THIS
10
- hi
11
- <span @html="this.frontmatterdata"></span>
package/test/test.js DELETED
@@ -1,131 +0,0 @@
1
- const test = require("ava");
2
- const Eleventy = require("@11ty/eleventy");
3
-
4
- test("Sample page", async t => {
5
- let elev = new Eleventy("./test/sample-1/page.webc", "./test/sample-1/_site", {
6
- configPath: "./test/sample-1/eleventy.config.js"
7
- });
8
-
9
- let results = await elev.toJSON();
10
- let [result] = results;
11
-
12
- t.is(result.content.trim(), `<!doctype html>
13
- <html lang="en">
14
- <head>
15
- <meta charset="utf-8">
16
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
17
- <meta name="description" content>
18
- <title></title>
19
- <style></style>
20
- <script></script>
21
- </head>
22
- <body>
23
- <say-hello></say-hello>
24
- <say-hello></say-hello>
25
- WHO IS THIS
26
- hi
27
- <span>HELLO FROM FRONT MATTER</span>
28
-
29
- <style></style>
30
- <script></script>
31
-
32
- </body>
33
- </html>`);
34
- });
35
-
36
- test("Sample page with global component", async t => {
37
- let elev = new Eleventy("./test/sample-2/page.webc", "./test/sample-2/_site", {
38
- configPath: "./test/sample-2/eleventy.config.js"
39
- });
40
-
41
- let results = await elev.toJSON();
42
- let [result] = results;
43
-
44
- t.is(result.content.trim(), `<!doctype html>
45
- <html lang="en">
46
- <head>
47
- <meta charset="utf-8">
48
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
49
- <meta name="description" content>
50
- <title></title>
51
- <style></style>
52
- <script></script>
53
- </head>
54
- <body>
55
- This is a component.
56
- This is a component.
57
- WHO IS THIS
58
- hi
59
- <span>HELLO FROM FRONT MATTER</span>
60
-
61
- <style></style>
62
- <script></script>
63
-
64
- </body>
65
- </html>`);
66
- });
67
-
68
- test("Page with front matter no-import components", async t => {
69
- let elev = new Eleventy("./test/sample-page-global-components/page.webc", "./test/sample-page-global-components/_site", {
70
- configPath: "./test/sample-page-global-components/eleventy.config.js"
71
- });
72
-
73
- let results = await elev.toJSON();
74
- let [result] = results;
75
-
76
- t.is(result.content.trim(), `<!doctype html>
77
- <html lang="en">
78
- <head>
79
- <meta charset="utf-8">
80
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
81
- <meta name="description" content>
82
- <title></title>
83
- <style></style>
84
- <script></script>
85
- </head>
86
- <body>
87
- HELLO
88
- HELLO
89
- WHO IS THIS
90
- hi
91
- <span>HELLO FROM FRONT MATTER</span>
92
-
93
- <style></style>
94
- <script></script>
95
-
96
- </body>
97
- </html>`);
98
- });
99
-
100
-
101
- test("Page with front matter no-import components (relative to input path)", async t => {
102
- let elev = new Eleventy("./test/sample-page-global-components-relative-to-inputpath/page.webc", "./test/sample-page-global-components-relative-to-inputpath/_site", {
103
- configPath: "./test/sample-page-global-components-relative-to-inputpath/eleventy.config.js"
104
- });
105
-
106
- let results = await elev.toJSON();
107
- let [result] = results;
108
-
109
- t.is(result.content.trim(), `<!doctype html>
110
- <html lang="en">
111
- <head>
112
- <meta charset="utf-8">
113
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
114
- <meta name="description" content>
115
- <title></title>
116
- <style></style>
117
- <script></script>
118
- </head>
119
- <body>
120
- HELLO
121
- HELLO
122
- WHO IS THIS
123
- hi
124
- <span>HELLO FROM FRONT MATTER</span>
125
-
126
- <style></style>
127
- <script></script>
128
-
129
- </body>
130
- </html>`);
131
- });