summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/LayoutCache.js
diff options
context:
space:
mode:
authorShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
committerShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
commit7a52ddeba2a68388b544f529d2d92104420f77b0 (patch)
tree15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/@11ty/eleventy/src/LayoutCache.js
parent53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff)
Changed from static to 11ty!
Diffstat (limited to 'node_modules/@11ty/eleventy/src/LayoutCache.js')
-rw-r--r--node_modules/@11ty/eleventy/src/LayoutCache.js98
1 files changed, 98 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/LayoutCache.js b/node_modules/@11ty/eleventy/src/LayoutCache.js
new file mode 100644
index 0000000..006f502
--- /dev/null
+++ b/node_modules/@11ty/eleventy/src/LayoutCache.js
@@ -0,0 +1,98 @@
+import { TemplatePath } from "@11ty/eleventy-utils";
+
+import eventBus from "./EventBus.js";
+
+// Note: this is only used for TemplateLayout right now but could be used for more
+// Just be careful because right now the TemplateLayout cache keys are not directly mapped to paths
+// So you may get collisions if you use this for other things.
+class LayoutCache {
+ constructor() {
+ this.cache = {};
+ this.cacheByInputPath = {};
+ }
+
+ clear() {
+ this.cache = {};
+ this.cacheByInputPath = {};
+ }
+
+ // alias
+ removeAll() {
+ for (let layoutFilePath in this.cacheByInputPath) {
+ this.remove(layoutFilePath);
+ }
+ this.clear();
+ }
+
+ size() {
+ return Object.keys(this.cacheByInputPath).length;
+ }
+
+ add(layoutTemplate) {
+ let keys = new Set();
+
+ if (typeof layoutTemplate === "string") {
+ throw new Error(
+ "Invalid argument type passed to LayoutCache->add(). Should be a TemplateLayout.",
+ );
+ }
+
+ if ("getFullKey" in layoutTemplate) {
+ keys.add(layoutTemplate.getFullKey());
+ }
+
+ if ("getKey" in layoutTemplate) {
+ // if `key` was an alias, also set to the pathed layout value too
+ // e.g. `layout: "default"` and `layout: "default.liquid"` will both map to the same template.
+ keys.add(layoutTemplate.getKey());
+ }
+
+ for (let key of keys) {
+ this.cache[key] = layoutTemplate;
+ }
+
+ // also the full template input path for use with eleventy --serve/--watch e.g. `_includes/default.liquid` (see `remove` below)
+ let fullPath = TemplatePath.stripLeadingDotSlash(layoutTemplate.inputPath);
+ this.cacheByInputPath[fullPath] = layoutTemplate;
+ }
+
+ has(key) {
+ return key in this.cache;
+ }
+
+ get(key) {
+ if (!this.has(key)) {
+ throw new Error(`Could not find ${key} in LayoutCache.`);
+ }
+
+ return this.cache[key];
+ }
+
+ remove(layoutFilePath) {
+ layoutFilePath = TemplatePath.stripLeadingDotSlash(layoutFilePath);
+ if (!this.cacheByInputPath[layoutFilePath]) {
+ // not a layout file
+ return;
+ }
+
+ let layoutTemplate = this.cacheByInputPath[layoutFilePath];
+ layoutTemplate.resetCaches();
+
+ let keys = layoutTemplate.getCacheKeys();
+ for (let key of keys) {
+ delete this.cache[key];
+ }
+
+ delete this.cacheByInputPath[layoutFilePath];
+ }
+}
+
+let layoutCache = new LayoutCache();
+
+eventBus.on("eleventy.resourceModified", () => {
+ // https://github.com/11ty/eleventy-plugin-bundle/issues/10
+ layoutCache.removeAll();
+});
+
+// singleton
+export default layoutCache;