From 7d39ac9335135511ebbc061e416dab9f03d77462 Mon Sep 17 00:00:00 2001 From: andoan16 <33853760+andoan16@users.noreply.github.com> Date: Sat, 28 Mar 2026 12:27:02 +0700 Subject: [PATCH] refactor(Util): create bundlelinker utility to track and propagate bundle additions across template renders This new utility class tracks bundle additions (CSS/JS) that were registered during collection item renders, so they can be replayed when those items' templateContent is accessed by a parent template. This is the core mechanism needed to solve the issue where nested partials' CSS/JS includes aren't loaded when accessed via collections. Affected files: BundleLinker.js Signed-off-by: andoan16 <33853760+andoan16@users.noreply.github.com> --- src/Util/BundleLinker.js | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Util/BundleLinker.js diff --git a/src/Util/BundleLinker.js b/src/Util/BundleLinker.js new file mode 100644 index 000000000..8c1e47f8a --- /dev/null +++ b/src/Util/BundleLinker.js @@ -0,0 +1,45 @@ +import debugUtil from "debug"; + +const debug = debugUtil("Eleventy:BundleLinker"); + +class BundleLinker { + #additions = new Map(); + + record(key, additions) { + if (!additions || additions.length === 0) { + return; + } + if (!this.#additions.has(key)) { + this.#additions.set(key, []); + } + for (let addition of additions) { + this.#additions.get(key).push(addition); + } + debug("Recorded %d bundle additions for key %o", additions.length, key); + } + + replay(key, bundleManager) { + if (!this.#additions.has(key)) { + return; + } + let additions = this.#additions.get(key); + debug("Replaying %d bundle additions for key %o", additions.length, key); + for (let addition of additions) { + bundleManager.addToBundle(addition.bucket, addition.content, addition.urlOverride); + } + } + + has(key) { + return this.#additions.has(key); + } + + delete(key) { + this.#additions.delete(key); + } + + clear() { + this.#additions.clear(); + } +} + +export { BundleLinker };