@11ty/eleventy-plugin-webc 0.5.0 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@11ty/eleventy-plugin-webc",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "WebC support for Eleventy adds for Single File Web Components",
5
5
  "main": "eleventyWebcPlugin.js",
6
6
  "scripts": {
@@ -39,9 +39,9 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@11ty/eleventy": "^2.0.0-canary.16",
42
- "@11ty/webc": "^0.5.2"
42
+ "@11ty/webc": "^0.6.0"
43
43
  },
44
44
  "devDependencies": {
45
- "ava": "^4.3.3"
45
+ "ava": "^5.0.1"
46
46
  }
47
47
  }
@@ -26,11 +26,21 @@ module.exports = function(eleventyConfig, options = {}) {
26
26
  let cssManager = new CodeManager();
27
27
  let jsManager = new CodeManager();
28
28
  let incremental = new WebCIncremental();
29
+ let componentsMap = false; // cache the glob search
30
+ let componentsMapKey;
29
31
 
30
- eleventyConfig.on("eleventy.before", () => {
32
+ eleventyConfig.on("eleventy.before", async () => {
31
33
  cssManager.reset();
32
34
  jsManager.reset();
33
- incremental.setComponents(options.components);
35
+
36
+ // For ESM in CJS
37
+ let { WebC } = await import("@11ty/webc");
38
+ incremental.setWebC(WebC);
39
+
40
+ if(options.components) {
41
+ componentsMap = WebC.getComponentsMap(options.components);
42
+ componentsMapKey = JSON.stringify(componentsMap);
43
+ }
34
44
  });
35
45
 
36
46
  function getCss(pageUrl, bucket = "default") {
@@ -55,16 +65,6 @@ module.exports = function(eleventyConfig, options = {}) {
55
65
  eleventyConfig.addExtension("webc", {
56
66
  outputFileExtension: "html",
57
67
 
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
68
  isIncrementalMatch: function (incrementalFilePath) {
69
69
  // Eleventy layouts don’t appear directly in the WebC component graph, so we use the `eleventy.layouts` map here
70
70
  if(incremental.isTemplateUsingLayout(this.inputPath, incrementalFilePath)) {
@@ -73,7 +73,8 @@ module.exports = function(eleventyConfig, options = {}) {
73
73
 
74
74
  let {page, setup} = incremental.get(this.inputPath);
75
75
  if(page && setup) {
76
- if(page.getComponents(setup).includes(incrementalFilePath)) {
76
+ let components = page.getComponents(setup);
77
+ if(components.includes(incrementalFilePath)) {
77
78
  return true;
78
79
  }
79
80
  }
@@ -81,9 +82,21 @@ module.exports = function(eleventyConfig, options = {}) {
81
82
  return false;
82
83
  },
83
84
 
85
+ compileOptions: {
86
+ cache: true,
87
+ getCacheKey: function(contents, inputPath) {
88
+ // if global components change, recompile!
89
+ return contents + inputPath + componentsMapKey;
90
+ }
91
+ },
92
+
84
93
  compile: async function(inputContent, inputPath) {
85
94
  let page = incremental.add(inputContent, inputPath);
86
95
 
96
+ if(componentsMap) {
97
+ page.defineComponents(componentsMap);
98
+ }
99
+
87
100
  // Add Eleventy JavaScript Functions as WebC helpers (Universal Filters also populate into these)
88
101
  for(let helperName in this.config.javascriptFunctions) {
89
102
  page.setHelper(helperName, this.config.javascriptFunctions[helperName]);
@@ -108,7 +121,8 @@ module.exports = function(eleventyConfig, options = {}) {
108
121
  return async (data) => {
109
122
  let setupObject = { data };
110
123
  if(data.webc?.components) {
111
- setupObject.components = incremental.getComponentMap(relativePath(data.page.inputPath, data.webc.components));
124
+ let WebC = incremental.webc;
125
+ setupObject.components = WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
112
126
  }
113
127
 
114
128
  let setup = await page.setup(setupObject);
@@ -2,36 +2,12 @@ class WebCIncremental {
2
2
  constructor() {
3
3
  this.pages = {};
4
4
  this.setups = {};
5
- this.globalComponentsMap = false;
6
- this.components = {};
7
5
  }
8
6
 
9
7
  setWebC(cls) {
10
8
  this.webc = cls;
11
9
  }
12
10
 
13
- needsComponents() {
14
- return this.globalComponentsMap === false;
15
- }
16
-
17
- setComponents(glob) {
18
- let map = this.getComponentMap(glob);
19
- if(map) {
20
- this.globalComponentsMap = map;
21
- }
22
- }
23
-
24
- getComponentMap(glob) {
25
- if(!this.webc) {
26
- return false;
27
- }
28
-
29
- let WebC = this.webc;
30
- let components = WebC.getComponentsMap(glob);
31
- // console.log( glob, Object.keys(components).length );
32
- return components;
33
- }
34
-
35
11
  setLayouts(layouts) {
36
12
  this.layouts = layouts;
37
13
  }
@@ -58,10 +34,6 @@ class WebCIncremental {
58
34
 
59
35
  page.setContent(inputContent, inputPath);
60
36
 
61
- if(this.globalComponentsMap) {
62
- page.defineComponents(this.globalComponentsMap);
63
- }
64
-
65
37
  this.pages[inputPath] = page;
66
38
 
67
39
  return page;
@@ -69,12 +41,11 @@ class WebCIncremental {
69
41
 
70
42
  addSetup(inputPath, setup) {
71
43
  this.setups[inputPath] = setup;
72
- this.components[inputPath] = setup.serializer.components;
73
44
  }
74
45
 
75
46
  get(inputPath) {
76
47
  let setup = this.setups[inputPath];
77
- let components = this.components[inputPath];
48
+ let components = setup.serializer.components;
78
49
 
79
50
  if(setup && components) {
80
51
  setup.serializer.restorePreparsedComponents(components);