@11ty/eleventy-plugin-webc 0.7.0 → 0.8.1

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.
@@ -27,6 +27,9 @@ module.exports = function(eleventyConfig, options = {}) {
27
27
 
28
28
  // Opt-out of Eleventy to process components
29
29
  // Note that Eleventy’s default ignores already have _includes/**
30
+
31
+ // This will cause component files outside of _includes to not be watched: https://github.com/11ty/eleventy-plugin-webc/issues/29
32
+ // Fixed in @11ty/eleventy@2.0.0-canary.18: https://github.com/11ty/eleventy/issues/893
30
33
  eleventyConfig.ignores.add(options.components);
31
34
  }
32
35
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@11ty/eleventy-plugin-webc",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "description": "WebC support for Eleventy adds for Single File Web Components",
5
5
  "main": "eleventyWebcPlugin.js",
6
6
  "scripts": {
@@ -14,7 +14,7 @@
14
14
  "node": ">=14.18"
15
15
  },
16
16
  "11ty": {
17
- "compatibility": ">=2.0.0-canary.17"
17
+ "compatibility": ">=2.0.0-canary.19"
18
18
  },
19
19
  "funding": {
20
20
  "type": "opencollective",
@@ -38,10 +38,10 @@
38
38
  ]
39
39
  },
40
40
  "dependencies": {
41
- "@11ty/eleventy": "^2.0.0-canary.17",
42
- "@11ty/webc": "^0.7.1"
41
+ "@11ty/eleventy": "^2.0.0-canary.19",
42
+ "@11ty/webc": "^0.8.0"
43
43
  },
44
44
  "devDependencies": {
45
- "ava": "^5.0.1"
45
+ "ava": "^5.1.0"
46
46
  }
47
47
  }
@@ -0,0 +1,50 @@
1
+ class BundleAssetsToContent {
2
+ static SPLIT_REGEX = /(\/\*__WebC:[^:]*:[^:]*:WebC__\*\/)/;
3
+ static SEPARATOR = ":";
4
+
5
+ constructor(content) {
6
+ this.content = content;
7
+ this.managers = {};
8
+ }
9
+
10
+ static getAssetKey(name, bucket) {
11
+ return `/*__WebC:${name}:${bucket || "default"}:WebC__*/`
12
+ }
13
+
14
+ setAssetManager(name, assetManager) {
15
+ this.managers[name] = assetManager;
16
+ }
17
+
18
+ normalizeMatch(match) {
19
+ if(match.startsWith("/*__WebC:")) {
20
+ let [prefix, name, bucket, suffix] = match.split(BundleAssetsToContent.SEPARATOR);
21
+ return { name, bucket };
22
+ }
23
+ return match;
24
+ }
25
+
26
+ findAll() {
27
+ let matches = this.content.split(BundleAssetsToContent.SPLIT_REGEX);
28
+ let ret = [];
29
+ for(let match of matches) {
30
+ ret.push(this.normalizeMatch(match));
31
+ }
32
+ return ret;
33
+ }
34
+
35
+ replaceAll(url) {
36
+ let matches = this.findAll();
37
+ return matches.map(match => {
38
+ if(typeof match === "string") {
39
+ return match;
40
+ }
41
+ let {name, bucket} = match;
42
+ if(!this.managers[name]) {
43
+ throw new Error(`No asset manager found for ${name}. Known keys: ${Object.keys(this.managers)}`);
44
+ }
45
+ return this.managers[name].getForPage(url, bucket);
46
+ }).join("");
47
+ }
48
+ }
49
+
50
+ module.exports = BundleAssetsToContent;
@@ -4,7 +4,7 @@ const { EleventyRenderPlugin } = require("@11ty/eleventy");
4
4
  const CompileString = EleventyRenderPlugin.String;
5
5
 
6
6
  const CodeManager = require("./codeManager.js");
7
- const WebCIncremental = require("./incremental.js");
7
+ const BundleAssetsToContent = require("./bundleAssets.js");
8
8
 
9
9
  function relativePath(inputPath, newGlob) {
10
10
  // project root
@@ -24,9 +24,9 @@ module.exports = function(eleventyConfig, options = {}) {
24
24
 
25
25
  let cssManager = new CodeManager();
26
26
  let jsManager = new CodeManager();
27
- let incremental = new WebCIncremental();
27
+
28
+ let _WebC;
28
29
  let componentsMap = false; // cache the glob search
29
- let componentsMapKey;
30
30
  let moduleScript;
31
31
 
32
32
  eleventyConfig.on("eleventy.before", async () => {
@@ -36,60 +36,35 @@ module.exports = function(eleventyConfig, options = {}) {
36
36
  // For ESM in CJS
37
37
  let { WebC, ModuleScript } = await import("@11ty/webc");
38
38
  moduleScript = ModuleScript;
39
- incremental.setWebC(WebC);
39
+ _WebC = WebC;
40
40
 
41
41
  if(options.components) {
42
42
  componentsMap = WebC.getComponentsMap(options.components);
43
- componentsMapKey = JSON.stringify(componentsMap);
44
43
  }
45
44
  });
46
45
 
47
- function getCss(pageUrl, bucket = "default") {
48
- return cssManager.getForPage(pageUrl, bucket);
49
- }
50
-
51
- function getJs(pageUrl, bucket = "default") {
52
- return jsManager.getForPage(pageUrl, bucket);
53
- }
54
-
46
+ // Expose bundled CSS code to other template languages
55
47
  if(options.filters.css) {
48
+ function getCss(pageUrl, bucket = "default") {
49
+ return cssManager.getForPage(pageUrl, bucket);
50
+ }
51
+
56
52
  eleventyConfig.addFilter(options.filters.css, (url, bucket) => getCss(url, bucket));
57
53
  }
54
+
55
+ // Expose bundled JS code to other template languages
58
56
  if(options.filters.js) {
57
+ function getJs(pageUrl, bucket = "default") {
58
+ return jsManager.getForPage(pageUrl, bucket);
59
+ }
60
+
59
61
  eleventyConfig.addFilter(options.filters.js, (url, bucket) => getJs(url, bucket));
60
62
  }
61
63
 
62
- eleventyConfig.on("eleventy.layouts", layouts => {
63
- incremental.setLayouts(layouts);
64
- });
65
-
66
64
  eleventyConfig.addExtension("webc", {
67
65
  outputFileExtension: "html",
68
66
 
69
- isIncrementalMatch: function (incrementalFilePath) {
70
- // Eleventy layouts don’t appear directly in the WebC component graph, so we use the `eleventy.layouts` map here
71
- if(incremental.isTemplateUsingLayout(this.inputPath, incrementalFilePath)) {
72
- return true;
73
- }
74
-
75
- let {page, setup} = incremental.get(this.inputPath);
76
-
77
- if(page && setup) {
78
- let components = page.getComponents(setup);
79
- if(components.includes(incrementalFilePath)) {
80
- return true;
81
- }
82
- }
83
-
84
- return false;
85
- },
86
-
87
67
  compileOptions: {
88
- cache: true,
89
- getCacheKey: function(contents, inputPath) {
90
- // if global components change, recompile!
91
- return contents + inputPath + componentsMapKey;
92
- },
93
68
  permalink: function(contents, inputPath) {
94
69
  return (data) => {
95
70
  return moduleScript.evaluateScript("permalink", contents, {
@@ -101,7 +76,10 @@ module.exports = function(eleventyConfig, options = {}) {
101
76
  },
102
77
 
103
78
  compile: async function(inputContent, inputPath) {
104
- let page = incremental.add(inputContent, inputPath);
79
+ let page = new _WebC();
80
+
81
+ page.setBundlerMode(true);
82
+ page.setContent(inputContent, inputPath);
105
83
 
106
84
  if(componentsMap) {
107
85
  page.defineComponents(componentsMap);
@@ -113,11 +91,11 @@ module.exports = function(eleventyConfig, options = {}) {
113
91
  }
114
92
 
115
93
  // Support both casings (I prefer getCss, but yeah)
116
- page.setHelper("getCss", (url, bucket) => getCss(url, bucket));
117
- page.setHelper("getCSS", (url, bucket) => getCss(url, bucket));
94
+ page.setHelper("getCss", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
95
+ page.setHelper("getCSS", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
118
96
 
119
- page.setHelper("getJs", (url, bucket) => getJs(url, bucket));
120
- page.setHelper("getJS", (url, bucket) => getJs(url, bucket));
97
+ page.setHelper("getJs", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
98
+ page.setHelper("getJS", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
121
99
 
122
100
  page.setTransform("11ty", async function(content) {
123
101
  let syntax = this["11ty:type"];
@@ -131,21 +109,22 @@ module.exports = function(eleventyConfig, options = {}) {
131
109
  });
132
110
 
133
111
  return async (data) => {
134
- let setupObject = { data };
112
+ let setupObject = {
113
+ data,
114
+ };
115
+
135
116
  if(data.webc?.components) {
136
- let WebC = incremental.webc;
137
- setupObject.components = WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
117
+ setupObject.components = _WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
138
118
  }
139
119
 
140
120
  if(options.before && typeof options.before === "function") {
141
121
  await options.before(page);
142
122
  }
143
123
 
144
- let setup = await page.setup(setupObject);
145
- incremental.addSetup(inputPath, setup);
124
+ let { html, css, js, buckets, components } = await page.compile(setupObject);
146
125
 
147
- let { ast, serializer } = setup;
148
- let { html, css, js, buckets } = await serializer.compile(ast);
126
+ // 2.0.0-canary.19+
127
+ this.addDependencies(inputPath, components);
149
128
 
150
129
  cssManager.addToPage(data.page.url, css, "default");
151
130
 
@@ -163,7 +142,14 @@ module.exports = function(eleventyConfig, options = {}) {
163
142
  }
164
143
  }
165
144
 
166
- return html;
145
+ // Always do a two pass render for assets to catch any CSS/JS that were compiled *in* the same template.
146
+ // https://github.com/11ty/eleventy-plugin-webc/issues/33
147
+ // This unlocks use of bundled asset code anywhere in the WebC component tree (not just Eleventy Layouts)
148
+ let bundler = new BundleAssetsToContent(html);
149
+ bundler.setAssetManager("css", cssManager);
150
+ bundler.setAssetManager("js", jsManager);
151
+
152
+ return bundler.replaceAll(data.page.url);
167
153
  };
168
154
  }
169
155
  });
@@ -1,60 +0,0 @@
1
- class WebCIncremental {
2
- constructor() {
3
- this.pages = {};
4
- this.setups = {};
5
- }
6
-
7
- setWebC(cls) {
8
- this.webc = cls;
9
- }
10
-
11
- setLayouts(layouts) {
12
- this.layouts = layouts;
13
- }
14
-
15
- isTemplateUsingLayout(templateFilePath, layoutFilePath) {
16
- if(this.layouts && this.layouts[layoutFilePath] && this.layouts[layoutFilePath].includes(templateFilePath)) {
17
- return true;
18
- }
19
- return false;
20
- }
21
-
22
- add(inputContent, inputPath) {
23
- let WebC = this.webc;
24
- let page = new WebC();
25
-
26
- // Explicitly disable bundler mode for layouts
27
- if(this.layouts && this.layouts[inputPath]) {
28
- page.setBundlerMode(false);
29
- } else {
30
- page.setBundlerMode(true);
31
- }
32
-
33
- page.setContent(inputContent, inputPath);
34
-
35
- this.pages[inputPath] = page;
36
-
37
- return page;
38
- }
39
-
40
- addSetup(inputPath, setup) {
41
- this.setups[inputPath] = setup;
42
- }
43
-
44
- get(inputPath) {
45
- let setup = this.setups[inputPath];
46
- let components = setup.serializer.components;
47
-
48
- if(setup && components) {
49
- setup.serializer.restorePreparsedComponents(components);
50
- }
51
-
52
- return {
53
- page: this.pages[inputPath],
54
- setup,
55
- components,
56
- }
57
- }
58
- }
59
-
60
- module.exports = WebCIncremental;