Skip to content

Commit

Permalink
Fixes #2267
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 14, 2022
1 parent 0d2171e commit 5def7d8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Engines/Custom.js
Expand Up @@ -165,12 +165,15 @@ class CustomEngine extends TemplateEngine {
defaultRenderer, // bind defaultRenderer to compile function
})(str, inputPath);

// Bind defaultRenderer to render function
if ("then" in fn && typeof fn.then === "function") {
// Promise, wait to bind
return fn.then((fn) => fn.bind({ defaultRenderer }));
} else if ("bind" in fn && typeof fn.bind === "function") {
return fn.bind({ defaultRenderer });
// Support `undefined` to skip compile/render
if (fn) {
// Bind defaultRenderer to render function
if ("then" in fn && typeof fn.then === "function") {
// Promise, wait to bind
return fn.then((fn) => fn.bind({ defaultRenderer }));
} else if ("bind" in fn && typeof fn.bind === "function") {
return fn.bind({ defaultRenderer });
}
}

return fn;
Expand Down
16 changes: 16 additions & 0 deletions test/TemplateRenderCustomTest.js
Expand Up @@ -215,3 +215,19 @@ test("JavaScript functions should not be mutable but not *that* mutable", async
let data = await tmpl.getData();
t.is(await tmpl.render(data), "<p>Paragraph</p>");
});

test("Return undefined in compile to ignore #2267", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.extensionMap.add({
extension: "txt",
key: "txt",
compile: function (str, inputPath) {
return;
},
});

let tr = getNewTemplateRender("txt", null, eleventyConfig);

let fn = await tr.getCompiledTemplate("<p>Paragraph</p>");
t.is(fn, undefined);
});
28 changes: 28 additions & 0 deletions test/TemplateTest-CustomExtensions.js
Expand Up @@ -438,3 +438,31 @@ test("Access to default renderer when you override an existing extension (async
let data = await tmpl.getData();
t.is(await tmpl.render(data), "hi");
});

test("Return undefined in compile to ignore #2267", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.extensionMap.add({
extension: "txt",
key: "txt",
compileOptions: {
cache: false,
},
getData: false,
compile: function (str, inputPath) {
return;
},
});

let dataObj = new TemplateData("./test/stubs/", eleventyConfig);
let tmpl = getNewTemplate(
"./test/stubs/custom-extension.txt",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);

let data = await tmpl.getData();
t.is(await tmpl.render(data), undefined);
});

0 comments on commit 5def7d8

Please sign in to comment.