Skip to content

Commit

Permalink
Fixes #3359
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 11, 2024
1 parent d72c41e commit 47a3252
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,37 @@ class JavaScript extends TemplateEngine {
return "";
};

let originalModData = mod?.data;

if (typeof mod === "object" && mod.default && this.eleventyConfig.getIsProjectUsingEsm()) {
mod = mod.default;
}

if (typeof mod === "string" || mod instanceof Buffer || mod.then) {
return { render: () => mod };
} else if (typeof mod === "function") {
if (mod.prototype && ("data" in mod.prototype || "render" in mod.prototype)) {
if (mod.prototype?.data || mod.prototype?.render) {
if (!("render" in mod.prototype)) {
mod.prototype.render = noop;
}

if (!("data" in mod.prototype) && !mod.data && originalModData) {
mod.prototype.data = originalModData;
}

return new mod();
} else {
return {
...(originalModData ? { data: originalModData } : undefined),
render: mod,
};
}
} else if (
"data" in mod ||
"render" in mod ||
("default" in mod && this.eleventyConfig.getIsProjectUsingEsm())
) {
if (!("render" in mod)) {
if ("default" in mod) {
mod.render = mod.default;
} else {
mod.render = noop;
}
} else if ("data" in mod || "render" in mod) {
if (!mod.render) {
mod.render = noop;
}
if (!mod.data && originalModData) {
mod.data = originalModData;
}
return mod;
}
Expand Down Expand Up @@ -191,7 +198,7 @@ class JavaScript extends TemplateEngine {
inst = await this.getInstanceFromInputPath(inputPath);
}

if (inst && "render" in inst) {
if (inst?.render) {
return function (data = {}) {
// TODO does this do anything meaningful for non-classes?
// `inst` should have a normalized `render` function from _getInstance
Expand Down
24 changes: 23 additions & 1 deletion test/TemplateRenderJavaScriptTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from "ava";

import TemplateRender from "../src/TemplateRender.js";
import Eleventy from "../src/Eleventy.js";
import EleventyExtensionMap from "../src/EleventyExtensionMap.js";

import { getTemplateConfigInstance } from "./_testHelpers.js";
Expand Down Expand Up @@ -302,8 +303,29 @@ test("Class has page property already and keeps it", async (t) => {
await fn({ avaTest: t, page: { url: "/hi/" } });
});

test("Class has default export and another one too, issue #3288", async (t) => {
test("File has default function export and another one too, issue #3288", async (t) => {
let tr = await getNewTemplateRender("./test/stubs/default-export-and-others.11ty.js");
let fn = await tr.getCompiledTemplate();
t.is(await fn(), "<h1>hello</h1>")
});

test("File has default class export and another one too, issue #3359", async (t) => {
let elev = new Eleventy("./test/stubs/default-class-export-and-others.11ty.js", "");
let results = await elev.toJSON();

t.is(results[0].content, "<div>hello</div>")
});

test("File has default function export and another one for data too, issue #3359", async (t) => {
let elev = new Eleventy("./test/stubs/default-function-export-and-named-data.11ty.js", "");
let results = await elev.toJSON();

t.is(results[0].content, "<h1>Hello World</h1>")
});

test("File has default function export and another one for data too, issue #3359 (CommonJS)", async (t) => {
let elev = new Eleventy("./test/stubs/default-function-export-and-named-data.11ty.cjs", "");
let results = await elev.toJSON();

t.is(results[0].content, "<h1>Hello World</h1>")
});
10 changes: 10 additions & 0 deletions test/stubs/default-class-export-and-others.11ty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default class IndexPage {
render(data) {
const name = world();
return `<div>hello</div>`;
}
}

export function world() {
return "World";
}
1 change: 1 addition & 0 deletions test/stubs/default-export-and-others.11ty.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const foo = "test";

// render
export default () => "<h1>hello</h1>";
5 changes: 5 additions & 0 deletions test/stubs/default-function-export-and-named-data.11ty.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// render
module.exports = (data) => `<h1>${data.name} World</h1>`;
module.exports.data = function() {
return { name: "Hello" }
};
6 changes: 6 additions & 0 deletions test/stubs/default-function-export-and-named-data.11ty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function data() {
return { name: "Hello" }
};

// render
export default (data) => `<h1>${data.name} World</h1>`;

0 comments on commit 47a3252

Please sign in to comment.