@11ty/eleventy-plugin-webc 0.8.1 → 0.9.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.
@@ -1,3 +1,4 @@
1
+ const eleventyBundlePlugin = require("@11ty/eleventy-plugin-bundle");
1
2
  const pkg = require("./package.json");
2
3
  const templatePlugin = require("./src/eleventyWebcTemplate.js");
3
4
  const transformPlugin = require("./src/eleventyWebcTransform.js");
@@ -9,6 +10,7 @@ module.exports = function(eleventyConfig, options = {}) {
9
10
  console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
10
11
  }
11
12
 
13
+ // Deprecated: this lives in @11ty/eleventy-plugin-bundle now
12
14
  let filters = Object.assign({
13
15
  css: "webcGetCss",
14
16
  js: "webcGetJs",
@@ -20,6 +22,7 @@ module.exports = function(eleventyConfig, options = {}) {
20
22
  transformData: {}, // extra global data for transforms specifically
21
23
  }, options);
22
24
 
25
+ // Deprecated: this lives in @11ty/eleventy-plugin-bundle now
23
26
  options.filters = filters;
24
27
 
25
28
  if(options.components) {
@@ -33,6 +36,13 @@ module.exports = function(eleventyConfig, options = {}) {
33
36
  eleventyConfig.ignores.add(options.components);
34
37
  }
35
38
 
39
+ // TODO Remove this when @11ty/eleventy-plugin-bundle is moved to core.
40
+ // If the bundle plugin has not been added, we add it here:
41
+ let bundlePlugin = eleventyConfig.plugins.find(entry => entry.plugin.eleventyPackage === "@11ty/eleventy-plugin-bundle");
42
+ if(!bundlePlugin) {
43
+ eleventyConfig.addPlugin(eleventyBundlePlugin);
44
+ }
45
+
36
46
  templatePlugin(eleventyConfig, options);
37
47
 
38
48
  if(options.useTransform) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@11ty/eleventy-plugin-webc",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "WebC support for Eleventy adds for Single File Web Components",
5
5
  "main": "eleventyWebcPlugin.js",
6
6
  "scripts": {
@@ -34,14 +34,20 @@
34
34
  "ava": {
35
35
  "failFast": true,
36
36
  "files": [
37
- "./test/*.js"
37
+ "./test/test*.js"
38
+ ],
39
+ "ignoredByWatcher": [
40
+ "**/_site/**",
41
+ ".cache"
38
42
  ]
39
43
  },
40
44
  "dependencies": {
41
- "@11ty/eleventy": "^2.0.0-canary.19",
42
- "@11ty/webc": "^0.8.0"
45
+ "@11ty/eleventy": "^2.0.0",
46
+ "@11ty/eleventy-plugin-bundle": "^1.0.1",
47
+ "@11ty/webc": "^0.9.0"
43
48
  },
44
49
  "devDependencies": {
45
- "ava": "^5.1.0"
50
+ "ava": "^5.1.0",
51
+ "node-retrieve-globals": "^2.0.4"
46
52
  }
47
53
  }
@@ -1,11 +1,10 @@
1
1
  const path = require("path");
2
+ const debug = require("debug")("Eleventy:WebC");
3
+
2
4
 
3
5
  const { EleventyRenderPlugin } = require("@11ty/eleventy");
4
6
  const CompileString = EleventyRenderPlugin.String;
5
7
 
6
- const CodeManager = require("./codeManager.js");
7
- const BundleAssetsToContent = require("./bundleAssets.js");
8
-
9
8
  function relativePath(inputPath, newGlob) {
10
9
  // project root
11
10
  if(newGlob.startsWith("~/")) {
@@ -20,19 +19,14 @@ function relativePath(inputPath, newGlob) {
20
19
  }
21
20
 
22
21
  module.exports = function(eleventyConfig, options = {}) {
22
+ // TODO remove this when WebC is moved out of plugin-land into core.
23
23
  eleventyConfig.addTemplateFormats("webc");
24
24
 
25
- let cssManager = new CodeManager();
26
- let jsManager = new CodeManager();
27
-
28
25
  let _WebC;
29
26
  let componentsMap = false; // cache the glob search
30
27
  let moduleScript;
31
28
 
32
29
  eleventyConfig.on("eleventy.before", async () => {
33
- cssManager.reset();
34
- jsManager.reset();
35
-
36
30
  // For ESM in CJS
37
31
  let { WebC, ModuleScript } = await import("@11ty/webc");
38
32
  moduleScript = ModuleScript;
@@ -43,22 +37,18 @@ module.exports = function(eleventyConfig, options = {}) {
43
37
  }
44
38
  });
45
39
 
46
- // Expose bundled CSS code to other template languages
40
+ // Deprecated (backwards compat only): this lives in @11ty/eleventy-plugin-bundle now
47
41
  if(options.filters.css) {
48
- function getCss(pageUrl, bucket = "default") {
49
- return cssManager.getForPage(pageUrl, bucket);
50
- }
51
-
52
- eleventyConfig.addFilter(options.filters.css, (url, bucket) => getCss(url, bucket));
42
+ eleventyConfig.addFilter(options.filters.css, (url, bucket) => {
43
+ return eleventyConfig.javascriptFunctions.getBundle("css", bucket);
44
+ });
53
45
  }
54
46
 
55
- // Expose bundled JS code to other template languages
47
+ // Deprecated (backwards compat only): this lives in @11ty/eleventy-plugin-bundle now
56
48
  if(options.filters.js) {
57
- function getJs(pageUrl, bucket = "default") {
58
- return jsManager.getForPage(pageUrl, bucket);
59
- }
60
-
61
- eleventyConfig.addFilter(options.filters.js, (url, bucket) => getJs(url, bucket));
49
+ eleventyConfig.addFilter(options.filters.js, (url, bucket) => {
50
+ return eleventyConfig.javascriptFunctions.getBundle("js", bucket)
51
+ });
62
52
  }
63
53
 
64
54
  eleventyConfig.addExtension("webc", {
@@ -66,12 +56,23 @@ module.exports = function(eleventyConfig, options = {}) {
66
56
 
67
57
  compileOptions: {
68
58
  permalink: function(contents, inputPath) {
69
- return (data) => {
70
- return moduleScript.evaluateScript("permalink", contents, {
71
- ...this,
72
- ...data,
73
- });
59
+ if(contents && typeof contents === "string") {
60
+ return async (data) => {
61
+ try {
62
+ // Hard to know if this is JavaScript code or just a raw string value.
63
+ let evaluatedString = await moduleScript.evaluateScript(contents, {
64
+ ...this,
65
+ ...data,
66
+ }, `Check the permalink for ${inputPath}`);
67
+ return evaluatedString;
68
+ } catch(e) {
69
+ debug("Error evaluating dynamic permalink, returning raw string contents instead: %o\n%O", contents, e);
70
+ return contents;
71
+ }
72
+ }
74
73
  }
74
+
75
+ return contents;
75
76
  }
76
77
  },
77
78
 
@@ -85,17 +86,18 @@ module.exports = function(eleventyConfig, options = {}) {
85
86
  page.defineComponents(componentsMap);
86
87
  }
87
88
 
88
- // Add Eleventy JavaScript Functions as WebC helpers (Universal Filters also populate into these)
89
+ // Add Eleventy JavaScript Functions as WebC helpers
90
+ // Note that Universal Filters and Shortcodes populate into javascriptFunctions and will be present here
89
91
  for(let helperName in this.config.javascriptFunctions) {
90
92
  page.setHelper(helperName, this.config.javascriptFunctions[helperName]);
91
93
  }
92
94
 
93
95
  // Support both casings (I prefer getCss, but yeah)
94
- page.setHelper("getCss", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
95
- page.setHelper("getCSS", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
96
+ page.setHelper("getCss", (url, bucket) => this.config.javascriptFunctions.getBundle("css", bucket));
97
+ page.setHelper("getCSS", (url, bucket) => this.config.javascriptFunctions.getBundle("css", bucket));
96
98
 
97
- page.setHelper("getJs", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
98
- page.setHelper("getJS", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
99
+ page.setHelper("getJs", (url, bucket) => this.config.javascriptFunctions.getBundle("js", bucket));
100
+ page.setHelper("getJS", (url, bucket) => this.config.javascriptFunctions.getBundle("js", bucket));
99
101
 
100
102
  page.setTransform("11ty", async function(content) {
101
103
  let syntax = this["11ty:type"];
@@ -126,30 +128,25 @@ module.exports = function(eleventyConfig, options = {}) {
126
128
  // 2.0.0-canary.19+
127
129
  this.addDependencies(inputPath, components);
128
130
 
129
- cssManager.addToPage(data.page.url, css, "default");
131
+ // Add CSS to bundle
132
+ this.config.javascriptFunctions.css(css, "default", data.page.url);
130
133
 
131
134
  if(buckets.css) {
132
135
  for(let bucket in buckets.css) {
133
- cssManager.addToPage(data.page.url, buckets.css[bucket], bucket);
136
+ this.config.javascriptFunctions.css(css, buckets.css[bucket], data.page.url);
134
137
  }
135
138
  }
136
139
 
137
- jsManager.addToPage(data.page.url, js, "default");
140
+ // Add JS to bundle
141
+ this.config.javascriptFunctions.js(js, "default", data.page.url);
138
142
 
139
143
  if(buckets.js) {
140
144
  for(let bucket in buckets.js) {
141
- jsManager.addToPage(data.page.url, buckets.js[bucket], bucket);
145
+ this.config.javascriptFunctions.js(js, buckets.js[bucket], data.page.url);
142
146
  }
143
147
  }
144
148
 
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);
149
+ return html;
153
150
  };
154
151
  }
155
152
  });
@@ -22,6 +22,7 @@ module.exports = function(eleventyConfig, options = {}) {
22
22
  componentsMap = WebC.getComponentsMap(options.components); // "./_includes/webc/*.webc"
23
23
  }
24
24
 
25
+ page.setBundlerMode(false);
25
26
  page.defineComponents(componentsMap);
26
27
  page.setContent(content, this.outputPath);
27
28
 
@@ -1,50 +0,0 @@
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;
@@ -1,33 +0,0 @@
1
- class Manager {
2
- constructor() {
3
- this.reset();
4
- }
5
-
6
- reset() {
7
- this.pages = {};
8
- }
9
-
10
- addToPage(pageUrl, code = [], bucket = "default") {
11
- if(code.length === 0) {
12
- return;
13
- }
14
-
15
- if(!this.pages[pageUrl]) {
16
- this.pages[pageUrl] = {};
17
- }
18
- if(!this.pages[pageUrl][bucket]) {
19
- this.pages[pageUrl][bucket] = new Set();
20
- }
21
-
22
- this.pages[pageUrl][bucket].add(code.join("\n"));
23
- }
24
-
25
- getForPage(pageUrl, bucket = "default") {
26
- if(this.pages[pageUrl]) {
27
- return Array.from(this.pages[pageUrl][bucket] || []).join("\n");
28
- }
29
- return "";
30
- }
31
- }
32
-
33
- module.exports = Manager;