Skip to content

Commit

Permalink
Fixes #3347
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 4, 2024
1 parent ed4a89a commit 9d1572c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,15 @@ class JavaScript extends TemplateEngine {
}

async #getInstanceFromInputPath(inputPath) {
let isEsm = this.eleventyConfig.getIsProjectUsingEsm();
const mod = await EleventyImport(inputPath, isEsm ? "esm" : "cjs");
let mod;
let relativeInputPath =
this.eleventyConfig.directories.getInputPathRelativeToInputDirectory(inputPath);
if (this.eleventyConfig.userConfig.isVirtualTemplate(relativeInputPath)) {
mod = this.eleventyConfig.userConfig.virtualTemplates[relativeInputPath].content;
} else {
let isEsm = this.eleventyConfig.getIsProjectUsingEsm();
mod = await EleventyImport(inputPath, isEsm ? "esm" : "cjs");
}

let inst = this._getInstance(mod);
if (inst) {
Expand Down
4 changes: 4 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@ class UserConfig {
};
}

isVirtualTemplate(virtualInputPath) {
return Boolean(this.virtualTemplates[virtualInputPath]);
}

#setDirectory(key, dir) {
if (this.isPluginExecution()) {
throw new Error(
Expand Down
43 changes: 43 additions & 0 deletions test/EleventyVirtualTemplatesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,46 @@ test("Virtual templates as layouts, issue #2307", async (t) => {
t.deepEqual(results[0].content.trim(), `<!-- Layout --><h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
});

test("11ty.js Virtual Templates (object), issue #3347", async (t) => {
let templateDefinition = {
data: () => {
return { var: 2 };
},
render: function(data) {
return `this is a test ${data.var}.`;
}
};

let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.11ty.js", templateDefinition);
}
});

let results = await elev.toJSON();

t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `this is a test 2.`);
// TODO support rawInput on 11ty.js?
// t.deepEqual(results[0].rawInput, templateDefinition);
});

test("11ty.js Virtual Templates (function), issue #3347", async (t) => {
let templateDefinition = function(data) {
return `this is a test ${data.page.url}.`;
};

let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.11ty.js", templateDefinition);
}
});

let results = await elev.toJSON();

t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `this is a test /virtual/.`);
// TODO support rawInput on 11ty.js?
// t.deepEqual(results[0].rawInput, templateDefinition);
});

0 comments on commit 9d1572c

Please sign in to comment.