Skip to content

Commit

Permalink
Fixes #2790
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Mar 22, 2023
1 parent da10b5c commit a9323c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const EventBusUtil = require("../Util/EventBusUtil");
class JavaScriptTemplateNotDefined extends EleventyBaseError {}

class JavaScript extends TemplateEngine {
// which data keys to bind to `this` in JavaScript template functions
// static DATA_KEYS_TO_BIND = ["page", "eleventy"];
static DATA_KEYS_TO_BIND = ["page", "eleventy"];

constructor(name, dirs, config) {
super(name, dirs, config);
this.instances = {};
Expand Down Expand Up @@ -50,7 +54,9 @@ class JavaScript extends TemplateEngine {
}
return new mod();
} else {
return { render: mod };
return {
render: mod,
};
}
} else if ("data" in mod || "render" in mod) {
if (!("render" in mod)) {
Expand All @@ -67,7 +73,6 @@ class JavaScript extends TemplateEngine {

const mod = require(TemplatePath.absolutePath(inputPath));
let inst = this._getInstance(mod);

if (inst) {
this.instances[inputPath] = inst;
} else {
Expand Down Expand Up @@ -110,11 +115,10 @@ class JavaScript extends TemplateEngine {

static wrapJavaScriptFunction(inst, fn) {
return function (...args) {
if (inst && inst.page) {
this.page = inst.page;
}
if (inst && inst.eleventy) {
this.eleventy = inst.eleventy;
for (let key of JavaScript.DATA_KEYS_TO_BIND) {
if (inst && inst[key]) {
this[key] = inst[key];
}
}

return fn.call(this, ...args);
Expand All @@ -136,9 +140,13 @@ class JavaScript extends TemplateEngine {
// TODO does this do anything meaningful for non-classes?
// `inst` should have a normalized `render` function from _getInstance

// only blow away existing inst.page if it has a page.url
if (!inst.page || inst.page.url) {
inst.page = data.page;
for (let key of JavaScript.DATA_KEYS_TO_BIND) {
if (!inst[key] && data[key]) {
// only blow away existing inst.page if it has a page.url
if (key !== "page" || !inst.page || inst.page.url) {
inst[key] = data[key];
}
}
}
Object.assign(inst, this.getJavaScriptFunctions(inst));

Expand Down
16 changes: 16 additions & 0 deletions test/EleventyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,19 @@ test("Eleventy tag collection with spaces in the tag name, issue #2851", async (
t.deepEqual(result.length, result[0].data.collections.all.length);
t.deepEqual(result[0].data.collections["tag with spaces"].length, 1);
});

test("this.eleventy on JavaScript template functions, issue #2790", async (t) => {
t.plan(3);

let elev = new Eleventy("./test/stubs-2790", "./test/stubs-2790/_site", {
config: function (eleventyConfig) {
eleventyConfig.addJavaScriptFunction("jsfunction", function () {
t.truthy(this.eleventy);
return this.eleventy.generator.split(" ")[0];
});
},
});
let result = await elev.toJSON();
t.deepEqual(result.length, 1);
t.deepEqual(result[0].content, `<p>Eleventy</p>`);
});
3 changes: 3 additions & 0 deletions test/stubs-2790/page.11ty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function ({ name }) {
return `<p>${this.jsfunction(name)}</p>`;
};

0 comments on commit a9323c3

Please sign in to comment.