Skip to content

Commit

Permalink
Fixes #3389
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 30, 2024
1 parent e45040e commit 8e4428e
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/Data/TemplateDataInitialGlobalData.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TemplateDataInitialGlobalData {
for (let key of keys) {
let returnValue = this.config.globalData[key];

// This section is problematic when used with eleventyComputed #3389
if (typeof returnValue === "function") {
returnValue = await returnValue();
}
Expand Down
14 changes: 12 additions & 2 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yaml from "js-yaml";
import matter from "gray-matter";
import debugUtil from "debug";

import { DeepCopy, TemplatePath } from "@11ty/eleventy-utils";
import { DeepCopy, TemplatePath, isPlainObject } from "@11ty/eleventy-utils";

import HtmlBasePlugin from "./Plugins/HtmlBasePlugin.js";
import RenderPlugin from "./Plugins/RenderPlugin.js";
Expand Down Expand Up @@ -1073,9 +1073,19 @@ class UserConfig {
}
}

// 3.0.0-alpha.18 started merging conflicts here (when possible), issue #3389
addGlobalData(name, data) {
name = this.getNamespacedName(name);
this.globalData[name] = data;
if (this.globalData[name]) {
if (isPlainObject(this.globalData[name]) && isPlainObject(data)) {
DeepCopy(this.globalData[name], data);
} else {
debug("Warning: overwriting a previous value set with addGlobalData(%o)", name);
this.globalData[name] = data;
}
} else {
this.globalData[name] = data;
}
return this;
}

Expand Down
118 changes: 118 additions & 0 deletions test/EleventyAddGlobalDataTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import test from "ava";
import Eleventy from "../src/Eleventy.js";

test("Eleventy addGlobalData should run once", async (t) => {
let count = 0;
let elev = new Eleventy("./test/stubs-addglobaldata/", "./test/stubs-addglobaldata/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
});

let results = await elev.toJSON();
t.is(count, 1);
});

test("Eleventy addGlobalData shouldn’t run if no input templates match!", async (t) => {
let count = 0;
let elev = new Eleventy(
"./test/stubs-addglobaldata-noop/",
"./test/stubs-addglobaldata-noop/_site",
{
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
}
);

let results = await elev.toJSON();
t.is(count, 0);
});

test("Eleventy addGlobalData can feed layouts to populate data cascade with layout data, issue #1245", async (t) => {
let elev = new Eleventy("./test/stubs-2145/", "./test/stubs-2145/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("layout", () => "layout.njk");
eleventyConfig.dataFilterSelectors.add("LayoutData");
},
});

let [result] = await elev.toJSON();
t.deepEqual(result.data, { LayoutData: 123 });
t.is(result.content.trim(), "FromLayoutlayout.njk");
});

test("Eleventy addGlobalData merge data #3389", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("eleventyComputed", {
testing(data) {
return `testing:${data.page.url}`;
}
});

eleventyConfig.addGlobalData("eleventyComputed", {
other(data) {
return `other:${data.page.url}`;
}
});

eleventyConfig.addTemplate("computed.njk", "{{ testing }}|{{ other }}", {})
},
});

let results = await elev.toJSON();
t.is(results.length, 1);
t.is(results[0].content, "testing:/computed/|other:/computed/");
});

test("Eleventy addGlobalData merge data #3389 lodash set", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("eleventyComputed.testing", () => {
return (data) => {
return `testing:${data.page.url}`;
}
});

eleventyConfig.addGlobalData("eleventyComputed.other", () => {
return (data) => {
return `other:${data.page.url}`;
}
});

eleventyConfig.addTemplate("computed.njk", "{{ testing }}|{{ other }}", {})
},
});

let results = await elev.toJSON();
t.is(results.length, 1);
t.is(results[0].content, "testing:/computed/|other:/computed/");
});

test.skip("Eleventy addGlobalData merge data #3389 no nested function", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("eleventyComputed.testing", (data) => {
return `testing:${data.page.url}`;
});

eleventyConfig.addGlobalData("eleventyComputed.other", (data) => {
return `other:${data.page.url}`;
});

eleventyConfig.addTemplate("computed.njk", "{{ testing }}|{{ other }}", {})
},
});

let results = await elev.toJSON();
t.is(results.length, 1);
t.is(results[0].content, "testing:/computed/|other:/computed/");
});

47 changes: 0 additions & 47 deletions test/EleventyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,53 +460,6 @@ test("Can Eleventy run two executeBuilds in parallel?", async (t) => {
);
});

test("Eleventy addGlobalData should run once", async (t) => {
let count = 0;
let elev = new Eleventy("./test/stubs-addglobaldata/", "./test/stubs-addglobaldata/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
});

let results = await elev.toJSON();
t.is(count, 1);
});

test("Eleventy addGlobalData shouldn’t run if no input templates match!", async (t) => {
let count = 0;
let elev = new Eleventy(
"./test/stubs-addglobaldata-noop/",
"./test/stubs-addglobaldata-noop/_site",
{
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
}
);

let results = await elev.toJSON();
t.is(count, 0);
});

test("Eleventy addGlobalData can feed layouts to populate data cascade with layout data, issue #1245", async (t) => {
let elev = new Eleventy("./test/stubs-2145/", "./test/stubs-2145/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("layout", () => "layout.njk");
eleventyConfig.dataFilterSelectors.add("LayoutData");
},
});

let [result] = await elev.toJSON();
t.deepEqual(result.data, { LayoutData: 123 });
t.is(result.content.trim(), "FromLayoutlayout.njk");
});

test("Unicode in front matter `tags`, issue #670", async (t) => {
let elev = new Eleventy("./test/stubs-670/", "./test/stubs-670/_site");

Expand Down

0 comments on commit 8e4428e

Please sign in to comment.