@11ty/eleventy-plugin-webc 0.7.0 → 0.8.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.
@@ -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.0",
4
4
  "description": "WebC support for Eleventy adds for Single File Web Components",
5
5
  "main": "eleventyWebcPlugin.js",
6
6
  "scripts": {
@@ -38,8 +38,8 @@
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.18",
42
+ "@11ty/webc": "^0.8.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "ava": "^5.0.1"
@@ -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;
@@ -5,6 +5,7 @@ const CompileString = EleventyRenderPlugin.String;
5
5
 
6
6
  const CodeManager = require("./codeManager.js");
7
7
  const WebCIncremental = require("./incremental.js");
8
+ const BundleAssetsToContent = require("./bundleAssets.js");
8
9
 
9
10
  function relativePath(inputPath, newGlob) {
10
11
  // project root
@@ -44,18 +45,21 @@ module.exports = function(eleventyConfig, options = {}) {
44
45
  }
45
46
  });
46
47
 
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
-
48
+ // Expose bundled CSS code to other template languages
55
49
  if(options.filters.css) {
50
+ function getCss(pageUrl, bucket = "default") {
51
+ return cssManager.getForPage(pageUrl, bucket);
52
+ }
53
+
56
54
  eleventyConfig.addFilter(options.filters.css, (url, bucket) => getCss(url, bucket));
57
55
  }
56
+
57
+ // Expose bundled JS code to other template languages
58
58
  if(options.filters.js) {
59
+ function getJs(pageUrl, bucket = "default") {
60
+ return jsManager.getForPage(pageUrl, bucket);
61
+ }
62
+
59
63
  eleventyConfig.addFilter(options.filters.js, (url, bucket) => getJs(url, bucket));
60
64
  }
61
65
 
@@ -68,7 +72,7 @@ module.exports = function(eleventyConfig, options = {}) {
68
72
 
69
73
  isIncrementalMatch: function (incrementalFilePath) {
70
74
  // 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)) {
75
+ if(incremental.isFileUsingLayout(this.inputPath, incrementalFilePath)) {
72
76
  return true;
73
77
  }
74
78
 
@@ -113,11 +117,11 @@ module.exports = function(eleventyConfig, options = {}) {
113
117
  }
114
118
 
115
119
  // 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));
120
+ page.setHelper("getCss", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
121
+ page.setHelper("getCSS", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
118
122
 
119
- page.setHelper("getJs", (url, bucket) => getJs(url, bucket));
120
- page.setHelper("getJS", (url, bucket) => getJs(url, bucket));
123
+ page.setHelper("getJs", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
124
+ page.setHelper("getJS", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
121
125
 
122
126
  page.setTransform("11ty", async function(content) {
123
127
  let syntax = this["11ty:type"];
@@ -131,7 +135,9 @@ module.exports = function(eleventyConfig, options = {}) {
131
135
  });
132
136
 
133
137
  return async (data) => {
134
- let setupObject = { data };
138
+ let setupObject = {
139
+ data,
140
+ };
135
141
  if(data.webc?.components) {
136
142
  let WebC = incremental.webc;
137
143
  setupObject.components = WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
@@ -163,7 +169,14 @@ module.exports = function(eleventyConfig, options = {}) {
163
169
  }
164
170
  }
165
171
 
166
- return html;
172
+ // Always do a two pass render for assets to catch any CSS/JS that were compiled *in* the same template.
173
+ // https://github.com/11ty/eleventy-plugin-webc/issues/33
174
+ // This unlocks use of bundled asset code anywhere in the WebC component tree (not just Eleventy Layouts)
175
+ let bundler = new BundleAssetsToContent(html);
176
+ bundler.setAssetManager("css", cssManager);
177
+ bundler.setAssetManager("js", jsManager);
178
+
179
+ return bundler.replaceAll(data.page.url);
167
180
  };
168
181
  }
169
182
  });
@@ -12,7 +12,7 @@ class WebCIncremental {
12
12
  this.layouts = layouts;
13
13
  }
14
14
 
15
- isTemplateUsingLayout(templateFilePath, layoutFilePath) {
15
+ isFileUsingLayout(templateFilePath, layoutFilePath) {
16
16
  if(this.layouts && this.layouts[layoutFilePath] && this.layouts[layoutFilePath].includes(templateFilePath)) {
17
17
  return true;
18
18
  }
@@ -23,13 +23,7 @@ class WebCIncremental {
23
23
  let WebC = this.webc;
24
24
  let page = new WebC();
25
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
-
26
+ page.setBundlerMode(true);
33
27
  page.setContent(inputContent, inputPath);
34
28
 
35
29
  this.pages[inputPath] = page;