From 650e583287c1c119de40a53ec2f02e2c6d838809 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Fri, 18 Nov 2022 20:45:47 -0600 Subject: [PATCH] Fixes #2654 --- src/TemplateLayout.js | 13 +++++++-- src/TemplateMap.js | 29 ++++++++++++------- src/Util/AsyncEventEmitter.js | 22 ++++++++------ test/TemplateLayoutTest.js | 4 +-- test/TemplateMapTest.js | 9 ++++++ test/TemplateTest.js | 2 +- .../_includes/first.liquid | 3 ++ .../_includes/second.liquid | 4 +++ .../_includes/third.liquid | 1 + 9 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 test/stubs-layouts-event/_includes/second.liquid create mode 100644 test/stubs-layouts-event/_includes/third.liquid diff --git a/src/TemplateLayout.js b/src/TemplateLayout.js index 67f05b2bd..c8472a4ac 100644 --- a/src/TemplateLayout.js +++ b/src/TemplateLayout.js @@ -104,6 +104,16 @@ class TemplateLayout extends TemplateContent { return map; } + async getLayoutChain() { + let map = await this.getTemplateLayoutMap(); + let layoutChain = []; + for (let j = map.length - 1; j >= 0; j--) { + layoutChain.push(map[j].template.inputPath); + } + layoutChain.reverse(); + return layoutChain; + } + async getData() { if (this.dataCache) { return this.dataCache; @@ -111,9 +121,7 @@ class TemplateLayout extends TemplateContent { let map = await this.getTemplateLayoutMap(); let dataToMerge = []; - let layoutChain = []; for (let j = map.length - 1; j >= 0; j--) { - layoutChain.push(map[j].template.inputPath); dataToMerge.push(map[j].frontMatterData); } @@ -121,7 +129,6 @@ class TemplateLayout extends TemplateContent { let data = TemplateData.mergeDeep(this.config, {}, ...dataToMerge); delete data[this.config.keys.layout]; - this.layoutChain = layoutChain.reverse(); this.dataCache = data; return data; } diff --git a/src/TemplateMap.js b/src/TemplateMap.js index 9fd2c22c7..b317d8fd1 100644 --- a/src/TemplateMap.js +++ b/src/TemplateMap.js @@ -374,9 +374,10 @@ class TemplateMap { this.checkForDuplicatePermalinks(); - await this.config.events.emitLazy("eleventy.layouts", () => - this.generateLayoutsMap() - ); + await this.config.events.emitLazy("eleventy.layouts", () => { + // returns promise + return this.generateLayoutsMap(); + }); await this.config.events.emitLazy("eleventy.serverlessUrlMap", () => this.generateServerlessUrlMap(orderedMap) @@ -654,19 +655,27 @@ class TemplateMap { return Promise.all(promises); } - generateLayoutsMap() { + async generateLayoutsMap() { let layouts = {}; for (let entry of this.map) { for (let page of entry._pages) { let tmpl = page.template; - let layout = page.data[this.config.keys.layout]; - if (layout) { - let layoutFilePath = tmpl.getLayout(layout)?.inputPath; - if (!layouts[layoutFilePath]) { - layouts[layoutFilePath] = new Set(); + let layoutKey = page.data[this.config.keys.layout]; + if (layoutKey) { + let layout = tmpl.getLayout(layoutKey); + let layoutChain = await layout.getLayoutChain(); + let priors = []; + for (let filepath of layoutChain) { + if (!layouts[filepath]) { + layouts[filepath] = new Set(); + } + layouts[filepath].add(page.inputPath); + for (let prior of priors) { + layouts[filepath].add(prior); + } + priors.push(filepath); } - layouts[layoutFilePath].add(page.inputPath); } } } diff --git a/src/Util/AsyncEventEmitter.js b/src/Util/AsyncEventEmitter.js index 5a39dde0b..e89da5a1c 100644 --- a/src/Util/AsyncEventEmitter.js +++ b/src/Util/AsyncEventEmitter.js @@ -30,16 +30,20 @@ class AsyncEventEmitter extends EventEmitter { return []; } - return this.emit.call( - this, - type, - ...args.map((arg) => { - if (typeof arg === "function") { - return arg(); + let argsMap = []; + for (let arg of args) { + if (typeof arg === "function") { + let r = arg(); + if (r instanceof Promise) { + r = await r; } - return arg; - }) - ); + argsMap.push(r); + } else { + argsMap.push(arg); + } + } + + return this.emit.call(this, type, ...argsMap); } } diff --git a/test/TemplateLayoutTest.js b/test/TemplateLayoutTest.js index 886253d94..3f2371d06 100644 --- a/test/TemplateLayoutTest.js +++ b/test/TemplateLayoutTest.js @@ -50,7 +50,7 @@ test("Get Layout Chain", async (t) => { await tl.getData(); - t.deepEqual(tl.layoutChain, [ + t.deepEqual(await tl.getLayoutChain(), [ "./test/stubs/_includes/layouts/layout-inherit-a.njk", "./test/stubs/_includes/layouts/layout-inherit-b.njk", "./test/stubs/_includes/layouts/layout-inherit-c.njk", @@ -75,7 +75,7 @@ test("Get Front Matter Data", async (t) => { thirdinherits: "c", }); - t.deepEqual(tl.layoutChain, [ + t.deepEqual(await tl.getLayoutChain(), [ "./test/stubs/_includes/layouts/layout-inherit-a.njk", "./test/stubs/_includes/layouts/layout-inherit-b.njk", "./test/stubs/_includes/layouts/layout-inherit-c.njk", diff --git a/test/TemplateMapTest.js b/test/TemplateMapTest.js index 445be51f7..8c1f1925a 100644 --- a/test/TemplateMapTest.js +++ b/test/TemplateMapTest.js @@ -1476,6 +1476,15 @@ test("eleventy.layouts Event", async (t) => { "./test/stubs-layouts-event/_includes/first.liquid": [ "./test/stubs-layouts-event/page.md", ], + "./test/stubs-layouts-event/_includes/second.liquid": [ + "./test/stubs-layouts-event/page.md", + "./test/stubs-layouts-event/_includes/first.liquid", + ], + "./test/stubs-layouts-event/_includes/third.liquid": [ + "./test/stubs-layouts-event/page.md", + "./test/stubs-layouts-event/_includes/first.liquid", + "./test/stubs-layouts-event/_includes/second.liquid", + ], }); }); diff --git a/test/TemplateTest.js b/test/TemplateTest.js index bbb068c8c..52543bfad 100644 --- a/test/TemplateTest.js +++ b/test/TemplateTest.js @@ -1819,7 +1819,7 @@ test("Get Layout Chain", async (t) => { await tmpl.getData(); - t.deepEqual(tmpl._layout.layoutChain, [ + t.deepEqual(await tmpl._layout.getLayoutChain(), [ "./test/stubs-incremental/layout-chain/_includes/base.njk", "./test/stubs-incremental/layout-chain/_includes/parent.njk", ]); diff --git a/test/stubs-layouts-event/_includes/first.liquid b/test/stubs-layouts-event/_includes/first.liquid index e02a27c0d..3a79f6171 100644 --- a/test/stubs-layouts-event/_includes/first.liquid +++ b/test/stubs-layouts-event/_includes/first.liquid @@ -1 +1,4 @@ +--- +layout: second +--- {{ content }} \ No newline at end of file diff --git a/test/stubs-layouts-event/_includes/second.liquid b/test/stubs-layouts-event/_includes/second.liquid new file mode 100644 index 000000000..48e588760 --- /dev/null +++ b/test/stubs-layouts-event/_includes/second.liquid @@ -0,0 +1,4 @@ +--- +layout: third.liquid +--- +{{ content }} \ No newline at end of file diff --git a/test/stubs-layouts-event/_includes/third.liquid b/test/stubs-layouts-event/_includes/third.liquid new file mode 100644 index 000000000..e02a27c0d --- /dev/null +++ b/test/stubs-layouts-event/_includes/third.liquid @@ -0,0 +1 @@ +{{ content }} \ No newline at end of file