Skip to content

Commit

Permalink
Adds support for global component manager cache from next version of …
Browse files Browse the repository at this point in the history
…WebC
  • Loading branch information
zachleat committed Mar 15, 2023
1 parent 515e3ce commit 818e418
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/eleventyWebcTemplate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require("path");
const debug = require("debug")("Eleventy:WebC");


const { EleventyRenderPlugin } = require("@11ty/eleventy");
const CompileString = EleventyRenderPlugin.String;

Expand All @@ -23,17 +22,19 @@ module.exports = function(eleventyConfig, options = {}) {
eleventyConfig.addTemplateFormats("webc");

let _WebC;
let globalComponentManager;
let componentsMap = false; // cache the glob search
let moduleScript;

eleventyConfig.on("eleventy.before", async () => {
// For ESM in CJS
let { WebC, ModuleScript } = await import("@11ty/webc");
let { WebC, ModuleScript, ComponentManager } = await import("@11ty/webc");
moduleScript = ModuleScript;
_WebC = WebC;
globalComponentManager = new ComponentManager();

if(options.components) {
componentsMap = WebC.getComponentsMap(options.components);
componentsMap = WebC.getComponentsMap(options.components); // second argument is ignores here
}
});

Expand Down Expand Up @@ -63,7 +64,7 @@ module.exports = function(eleventyConfig, options = {}) {
let evaluatedString = await moduleScript.evaluateScript(contents, {
...this,
...data,
}, `Check the permalink for ${inputPath}`);
}, `Check the permalink for ${inputPath}`, "eleventyWebcPermalink:" + inputPath);
return evaluatedString;
} catch(e) {
debug("Error evaluating dynamic permalink, returning raw string contents instead: %o\n%O", contents, e);
Expand All @@ -79,6 +80,7 @@ module.exports = function(eleventyConfig, options = {}) {
compile: async function(inputContent, inputPath) {
let page = new _WebC();

page.setGlobalComponentManager(globalComponentManager);
page.setBundlerMode(true);
page.setContent(inputContent, inputPath);

Expand Down Expand Up @@ -110,6 +112,7 @@ module.exports = function(eleventyConfig, options = {}) {
return content;
});

// Render function
return async (data) => {
let setupObject = {
data,
Expand Down

0 comments on commit 818e418

Please sign in to comment.