From c1c4caf30232b9b43f7cc8bdb60b4aae754658dc Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Sat, 9 Aug 2025 10:48:26 -0700 Subject: [PATCH 01/10] Adds (hacky) tracking of paginations. Fixes #37 --- src/CodeManager.js | 45 ++++++++++++++++++++++++++++++++++++-- src/eleventy.shortcodes.js | 9 ++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/CodeManager.js b/src/CodeManager.js index 809c242..3aa6795 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -64,6 +64,7 @@ class CodeManager { reset() { this.pages = {}; + this.paginationPages = {}; } static normalizeBuckets(bucket) { @@ -89,6 +90,16 @@ class CodeManager { } } + addPaginationUrls(url, subUrls = []) { + if(!subUrls.length) { return; } + if(!this.paginationPages[url]) { + this.paginationPages[url] = new Set(); + } + for(let u of subUrls) { + this.paginationPages[url].add(u); + } + } + addToPage(pageUrl, code = [], bucket) { if(!Array.isArray(code) && code) { code = [code]; @@ -142,14 +153,40 @@ class CodeManager { getBucketsForPage(pageData) { let pageUrl = pageData.url; + if(this.paginationPages[pageUrl]) { // Merge + let result = new Set(); + for(let url of this.paginationPages[pageUrl]) { + result = result.union(new Set(this.getBucketsForPage(url))); + } + return Array.from(result.entries()); + } if(!this.pages[pageUrl]) { return []; } return Object.keys(this.pages[pageUrl]); } - getRawForPage(pageData, buckets = undefined) { - let url = pageData.url; + _getRawForPagination(url, buckets) { + let result = new Set(); + for(let subUrl of this.paginationPages[url]) { + if(!this.pages[subUrl]) { continue; } + for(let b of buckets) { + if(!this.pages[subUrl][b]) { continue; } + for(let entry of this.pages[subUrl][b]) { + result.add(entry); + } + } + } + return result; + } + + _getRawForPage(url, buckets = undefined) { + // Merge data from pagination sub-pages. + if(this.paginationPages[url]) { + buckets = CodeManager.normalizeBuckets(buckets); + return this._getRawForPagination(url, buckets); + } + if(!this.pages[url]) { debug("No bundle code found for %o on %o, %O", this.name, url, this.pages); return new Set(); @@ -173,6 +210,10 @@ class CodeManager { debug("Retrieving %o for %o (buckets: %o, entries: %o, size: %o)", this.name, url, buckets, set.size, size); return set; + } + + getRawForPage(pageData, buckets = undefined) { + return this._getRawForPage(pageData.url, buckets); } async getForPage(pageData, buckets = undefined) { diff --git a/src/eleventy.shortcodes.js b/src/eleventy.shortcodes.js index 6bb8843..4405f4a 100644 --- a/src/eleventy.shortcodes.js +++ b/src/eleventy.shortcodes.js @@ -56,6 +56,15 @@ function eleventyBundleShortcodes(eleventyConfig, pluginOptions = {}) { if(url) { pagesUsingBundles[url] = true; } + let paginationUrls = (this?.ctx?.pagination?.items || []). + filter((i) => !!(i?.url)). + map((i) => i.url); + if(paginationUrls.length) { + paginationUrls.forEach((u) => { + pagesUsingBundles[u] = true; + }) + managers[type].addPaginationUrls(url, paginationUrls); + } return OutOfOrderRender.getAssetKey("get", type, bucket); }); From 41ab600806268f264b49b5f395034d674654aa78 Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Sat, 9 Aug 2025 11:05:08 -0700 Subject: [PATCH 02/10] Fixup method names --- src/CodeManager.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/CodeManager.js b/src/CodeManager.js index 3aa6795..42177fd 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -180,7 +180,8 @@ class CodeManager { return result; } - _getRawForPage(url, buckets = undefined) { + getRawForPage(pageData, buckets = undefined) { + let url = pageData.url; // Merge data from pagination sub-pages. if(this.paginationPages[url]) { buckets = CodeManager.normalizeBuckets(buckets); @@ -212,10 +213,6 @@ class CodeManager { return set; } - getRawForPage(pageData, buckets = undefined) { - return this._getRawForPage(pageData.url, buckets); - } - async getForPage(pageData, buckets = undefined) { let set = this.getRawForPage(pageData, buckets); let bundleContent = Array.from(set).join("\n"); From 55fb59d425f9703e3db0b33f02cb679bb4f9ef8e Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Sat, 9 Aug 2025 11:13:41 -0700 Subject: [PATCH 03/10] Fixup whitespace --- src/CodeManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CodeManager.js b/src/CodeManager.js index 42177fd..b23507d 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -211,7 +211,7 @@ class CodeManager { debug("Retrieving %o for %o (buckets: %o, entries: %o, size: %o)", this.name, url, buckets, set.size, size); return set; - } + } async getForPage(pageData, buckets = undefined) { let set = this.getRawForPage(pageData, buckets); From fe8009914adad7f4be13f9e2a1d10fe4250da21e Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Sat, 9 Aug 2025 11:15:43 -0700 Subject: [PATCH 04/10] Fixup indentation --- src/CodeManager.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CodeManager.js b/src/CodeManager.js index b23507d..a497935 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -170,9 +170,9 @@ class CodeManager { let result = new Set(); for(let subUrl of this.paginationPages[url]) { if(!this.pages[subUrl]) { continue; } - for(let b of buckets) { - if(!this.pages[subUrl][b]) { continue; } - for(let entry of this.pages[subUrl][b]) { + for(let b of buckets) { + if(!this.pages[subUrl][b]) { continue; } + for(let entry of this.pages[subUrl][b]) { result.add(entry); } } From 4333461173b060048f722882ff24e1f417979bb5 Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Mon, 29 Sep 2025 16:14:09 -0700 Subject: [PATCH 05/10] Fixup buckets calculation. --- src/CodeManager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CodeManager.js b/src/CodeManager.js index a497935..4a2dbd3 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -168,6 +168,8 @@ class CodeManager { _getRawForPagination(url, buckets) { let result = new Set(); + buckets = CodeManager.normalizeBuckets(buckets); + // Merge data from pagination sub-pages. for(let subUrl of this.paginationPages[url]) { if(!this.pages[subUrl]) { continue; } for(let b of buckets) { @@ -182,9 +184,7 @@ class CodeManager { getRawForPage(pageData, buckets = undefined) { let url = pageData.url; - // Merge data from pagination sub-pages. if(this.paginationPages[url]) { - buckets = CodeManager.normalizeBuckets(buckets); return this._getRawForPagination(url, buckets); } From 238fa6e69a040fa97e66e6dad39b6c14cf3baeb6 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Fri, 22 Aug 2025 17:35:27 -0500 Subject: [PATCH 06/10] Fix eleventy-plugin-webc unit test bug with bundles not being processed --- src/eleventy.shortcodes.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/eleventy.shortcodes.js b/src/eleventy.shortcodes.js index 4405f4a..507cc8b 100644 --- a/src/eleventy.shortcodes.js +++ b/src/eleventy.shortcodes.js @@ -3,19 +3,14 @@ import debugUtil from "debug"; const debug = debugUtil("Eleventy:Bundle"); -function eleventyBundleShortcodes(eleventyConfig, pluginOptions = {}) { +export default function(eleventyConfig, pluginOptions = {}) { let managers = eleventyConfig.getBundleManagers(); let writeToFileSystem = true; - let pagesUsingBundles = {}; function bundleTransform(content, stage = 0) { - if(typeof content !== "string") { - return content; - } - + // Only run if content is string // Only run if managers are in play - // Only run on pages that have fetched bundles via `getBundle` or `getBundleFileUrl` - if(Object.keys(managers).length === 0 || this.page.url && !pagesUsingBundles[this.page.url]) { + if(typeof content !== "string" || Object.keys(managers).length === 0) { return content; } @@ -36,8 +31,6 @@ function eleventyBundleShortcodes(eleventyConfig, pluginOptions = {}) { return; } - pagesUsingBundles = {}; - if(outputMode !== "fs") { writeToFileSystem = false; debug("Skipping writing to the file system due to output mode: %o", outputMode); @@ -76,11 +69,6 @@ function eleventyBundleShortcodes(eleventyConfig, pluginOptions = {}) { throw new Error(`Invalid bundle type: ${type}. Available options: ${Object.keys(managers)}`); } - let url = explicitUrl || this.page?.url; - if(url) { - pagesUsingBundles[url] = true; - } - return OutOfOrderRender.getAssetKey("file", type, bucket); }); @@ -107,5 +95,3 @@ function eleventyBundleShortcodes(eleventyConfig, pluginOptions = {}) { }); }); }; - -export default eleventyBundleShortcodes; From bdaa2f97d6408533001ee865bbc9643724288365 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Fri, 22 Aug 2025 17:37:32 -0500 Subject: [PATCH 07/10] v3.0.7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 82e6f86..21c8405 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@11ty/eleventy-plugin-bundle", - "version": "3.0.6", + "version": "3.0.7", "description": "Little bundles of code, little bundles of joy.", "main": "eleventy.bundle.js", "type": "module", From f212206c1f2af4d131166efc306dbf75c84c16c6 Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Sat, 9 Aug 2025 10:48:26 -0700 Subject: [PATCH 08/10] Adds (hacky) tracking of paginations. Fixes #37 --- src/CodeManager.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/CodeManager.js b/src/CodeManager.js index 4a2dbd3..78f50cd 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -65,6 +65,7 @@ class CodeManager { reset() { this.pages = {}; this.paginationPages = {}; + this.paginationPages = {}; } static normalizeBuckets(bucket) { @@ -100,6 +101,7 @@ class CodeManager { } } + addToPage(pageUrl, code = [], bucket) { if(!Array.isArray(code) && code) { code = [code]; @@ -182,9 +184,24 @@ class CodeManager { return result; } - getRawForPage(pageData, buckets = undefined) { - let url = pageData.url; + _getRawForPagination(url, buckets) { + let result = new Set(); + for(let subUrl of this.paginationPages[url]) { + if(!this.pages[subUrl]) { continue; } + for(let b of buckets) { + if(!this.pages[subUrl][b]) { continue; } + for(let entry of this.pages[subUrl][b]) { + result.add(entry); + } + } + } + return result; + } + + _getRawForPage(url, buckets = undefined) { + // Merge data from pagination sub-pages. if(this.paginationPages[url]) { + buckets = CodeManager.normalizeBuckets(buckets); return this._getRawForPagination(url, buckets); } @@ -211,6 +228,10 @@ class CodeManager { debug("Retrieving %o for %o (buckets: %o, entries: %o, size: %o)", this.name, url, buckets, set.size, size); return set; + } + + getRawForPage(pageData, buckets = undefined) { + return this._getRawForPage(pageData.url, buckets); } async getForPage(pageData, buckets = undefined) { From d32efbf69a22a8170f9aad2311e6efa5219f4830 Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Mon, 29 Sep 2025 19:34:42 -0700 Subject: [PATCH 09/10] fixup missing declaration --- src/eleventy.shortcodes.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/eleventy.shortcodes.js b/src/eleventy.shortcodes.js index 507cc8b..974ed2f 100644 --- a/src/eleventy.shortcodes.js +++ b/src/eleventy.shortcodes.js @@ -37,6 +37,8 @@ export default function(eleventyConfig, pluginOptions = {}) { } }); + let pagesUsingBundles = {}; + // e.g. `getBundle` shortcode to get code in current page bundle // bucket can be an array // This shortcode name is not configurable on purpose (for wider plugin compatibility) From 97c1b180d8df1efedb8131c74cbea5665203aa3b Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Wed, 1 Oct 2025 23:54:05 -0700 Subject: [PATCH 10/10] Add tests --- src/CodeManager.js | 1 - .../pagination-support/_includes/template.njk | 5 +++++ .../pagination-support/eleventy.config.js | 15 +++++++++++++++ test/stubs/pagination-support/index.md | 9 +++++++++ test/stubs/pagination-support/post1.md | 12 ++++++++++++ test/stubs/pagination-support/post2.md | 12 ++++++++++++ test/stubs/pagination-support/post3.md | 12 ++++++++++++ test/test.js | 19 +++++++++++++++++++ 8 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/stubs/pagination-support/_includes/template.njk create mode 100644 test/stubs/pagination-support/eleventy.config.js create mode 100644 test/stubs/pagination-support/index.md create mode 100644 test/stubs/pagination-support/post1.md create mode 100644 test/stubs/pagination-support/post2.md create mode 100644 test/stubs/pagination-support/post3.md diff --git a/src/CodeManager.js b/src/CodeManager.js index 78f50cd..8181f1a 100644 --- a/src/CodeManager.js +++ b/src/CodeManager.js @@ -101,7 +101,6 @@ class CodeManager { } } - addToPage(pageUrl, code = [], bucket) { if(!Array.isArray(code) && code) { code = [code]; diff --git a/test/stubs/pagination-support/_includes/template.njk b/test/stubs/pagination-support/_includes/template.njk new file mode 100644 index 0000000..0999088 --- /dev/null +++ b/test/stubs/pagination-support/_includes/template.njk @@ -0,0 +1,5 @@ +{% getBundle "css" %} + +{%- for post in pagination.items -%} + {{ post.content | safe }} +{%- endfor -%} \ No newline at end of file diff --git a/test/stubs/pagination-support/eleventy.config.js b/test/stubs/pagination-support/eleventy.config.js new file mode 100644 index 0000000..fa8bf28 --- /dev/null +++ b/test/stubs/pagination-support/eleventy.config.js @@ -0,0 +1,15 @@ +import bundlePlugin from "../../../eleventy.bundle.js"; + +export default function(eleventyConfig) { + eleventyConfig.addPlugin(bundlePlugin, { + force: true, // for testing + immediate: true, + // toFileDirectory: "bundle", + bundles: false, + }); + + // eleventyConfig.addBundle("css"); + eleventyConfig.addBundle("css", { + bundleHtmlContentFromSelector: "style", + }); +}; \ No newline at end of file diff --git a/test/stubs/pagination-support/index.md b/test/stubs/pagination-support/index.md new file mode 100644 index 0000000..9209bca --- /dev/null +++ b/test/stubs/pagination-support/index.md @@ -0,0 +1,9 @@ +--- +pagination: + data: collections.all + size: 2 + alias: posts +templateEngineOverride: njk,md +layout: "./template.njk" +--- + diff --git a/test/stubs/pagination-support/post1.md b/test/stubs/pagination-support/post1.md new file mode 100644 index 0000000..d2024e1 --- /dev/null +++ b/test/stubs/pagination-support/post1.md @@ -0,0 +1,12 @@ +--- +title: "Post 1" +templateEngineOverride: njk,md +--- + +{% css %} + +{% endcss %} + +# First Post!!!!! + +*sigh* \ No newline at end of file diff --git a/test/stubs/pagination-support/post2.md b/test/stubs/pagination-support/post2.md new file mode 100644 index 0000000..5270c16 --- /dev/null +++ b/test/stubs/pagination-support/post2.md @@ -0,0 +1,12 @@ +--- +title: "Post 2" +templateEngineOverride: njk,md +--- + +{% css %} + +{% endcss %} + +# Second + +Was this a good idea? \ No newline at end of file diff --git a/test/stubs/pagination-support/post3.md b/test/stubs/pagination-support/post3.md new file mode 100644 index 0000000..cb541a3 --- /dev/null +++ b/test/stubs/pagination-support/post3.md @@ -0,0 +1,12 @@ +--- +title: "Post 3" +templateEngineOverride: njk,md +--- + +{% css %} + +{% endcss %} + +# Third + +**Running out of steam.** \ No newline at end of file diff --git a/test/test.js b/test/test.js index efaa4c9..2218962 100644 --- a/test/test.js +++ b/test/test.js @@ -484,3 +484,22 @@ test("`) }); + + +test("Pagination support", async t => { + let elev = new Eleventy("test/stubs/pagination-support/", undefined, { + configPath: "test/stubs/pagination-support/eleventy.config.js", + config: function(eleventyConfig) { + eleventyConfig.setQuietMode(true); + } + }); + await elev.write(); + t.deepEqual( + normalize(fs.readFileSync("_site/index.html", "utf8")), + ` +

First Post!!!!!

+

sigh

+

Second

+

Was this a good idea?

` + ); +}); \ No newline at end of file