Skip to content

Begin process of decoupling all templating languages #3747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Benchmark/BenchmarkGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BenchmarkGroup {
}

// TODO use addAsync everywhere instead
add(type, callback) {
add(type, callback, misc = {}) {
let benchmark = (this.benchmarks[type] = new Benchmark());

/** @this {any} */
Expand All @@ -43,6 +43,7 @@ class BenchmarkGroup {
value: {
type: isAsyncFunction(callback) ? "async" : "sync",
callback,
...misc,
},
});

Expand Down Expand Up @@ -73,7 +74,9 @@ class BenchmarkGroup {
setMinimumThresholdPercent(minimumThresholdPercent) {
let val = parseInt(minimumThresholdPercent, 10);
if (isNaN(val)) {
throw new Error("`setMinimumThresholdPercent` expects a number argument.");
throw new Error(
"`setMinimumThresholdPercent` expects a number argument.",
);
}
this.minimumThresholdPercent = val;
}
Expand Down
9 changes: 0 additions & 9 deletions src/Data/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ class TemplateData {
return this.dirs.data;
}

// This was async in 2.0 and prior but doesn’t need to be any more.
getInputDir() {
return this.dirs.input;
}

getDataDir() {
return this.dataDir;
}

exists(pathname) {
// It's common for data files not to exist, so we avoid going to the FS to
// re-check if they do via a quick-and-dirty cache.
Expand Down
4 changes: 2 additions & 2 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ Arguments:
return (
path.endsWith(".css") &&
// TODO how to make this work with relative includes?
!TemplatePath.startsWithSubPath(path, this.eleventyFiles.getIncludesDir())
!TemplatePath.startsWithSubPath(path, this.eleventyFiles?.includesDir)
);
});

Expand Down Expand Up @@ -1106,7 +1106,7 @@ Arguments:
return;
}

let dataDir = TemplatePath.stripLeadingDotSlash(this.templateData.getDataDir());
let dataDir = TemplatePath.stripLeadingDotSlash(this.templateData?.dataDir);
function filterOutGlobalDataFiles(path) {
return !dataDir || !TemplatePath.stripLeadingDotSlash(path).startsWith(dataDir);
}
Expand Down
15 changes: 0 additions & 15 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ class EleventyFiles {
return this.dirs.data;
}

// Backwards compat
getDataDir() {
return this.dataDir;
}

setFileSystemSearch(fileSystemSearch) {
this.fileSystemSearch = fileSystemSearch;
}
Expand Down Expand Up @@ -292,16 +287,6 @@ class EleventyFiles {
return Array.from(ignoreFiles);
}

/* Backwards compat */
getIncludesDir() {
return this.includesDir;
}

/* Backwards compat */
getLayoutsDir() {
return this.layoutsDir;
}

getFileGlobs() {
return this.normalizedTemplateGlobs;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class JavaScript extends TemplateEngine {
this.instances = {};

this.cacheable = false;
this.filters = templateConfig.config.__theCodeCriesInPain.javascript;

this.config.events.on("eleventy#templateModified", (inputPath, metadata = {}) => {
let { usedByDependants, relevantLayouts } = metadata;
Expand Down Expand Up @@ -141,7 +142,7 @@ export default class JavaScript extends TemplateEngine {

getJavaScriptFunctions(inst) {
let fns = {};
let configFns = this.config.javascriptFunctions;
let configFns = this.filters;

for (let key in configFns) {
// prefer pre-existing `page` javascriptFunction, if one exists
Expand Down
6 changes: 3 additions & 3 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default class Liquid extends TemplateEngine {

this.liquidOptions = this.config.liquidOptions || {};

this.filters = eleventyConfig.config.__theCodeCriesInPain.liquid.filters

this.setLibrary(this.config.libraryOverrides.liquid);

this.argLexer = moo.compile(Liquid.argumentLexerOptions);
Expand All @@ -32,9 +34,7 @@ export default class Liquid extends TemplateEngine {
// warning, the include syntax supported here does not exactly match what Jekyll uses.
this.liquidLib = override || new LiquidJs(this.getLiquidOptions());
this.setEngineLib(this.liquidLib);

this.addFilters(this.config.liquidFilters);

this.addFilters(this.filters);
// TODO these all go to the same place (addTag), add warnings for overwrites
this.addCustomTags(this.config.liquidTags);
this.addAllShortcodes(this.config.liquidShortcodes);
Expand Down
8 changes: 0 additions & 8 deletions src/Engines/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ export default class Markdown extends TemplateEngine {
setLibrary(mdLib) {
this.mdLib = mdLib || markdownIt(this.getMarkdownOptions());

// Overrides a highlighter set in `markdownOptions`
// This is separate so devs can pass in a new mdLib and still use the official eleventy plugin for markdown highlighting
if (this.config.markdownHighlighter && typeof this.mdLib.set === "function") {
this.mdLib.set({
highlight: this.config.markdownHighlighter,
});
}

if (typeof this.mdLib.disable === "function") {
// Disable indented code blocks by default (Issue #2438)
this.mdLib.disable("code");
Expand Down
60 changes: 43 additions & 17 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TemplatePath } from "@11ty/eleventy-utils";
import TemplateEngine from "./TemplateEngine.js";
import EleventyBaseError from "../Errors/EleventyBaseError.js";
import { augmentObject } from "./Util/ContextAugmenter.js";
import { withResolvers } from "../Util/PromiseUtil.js";

const debug = debugUtil("Eleventy:Nunjucks");

Expand All @@ -20,6 +19,9 @@ export default class Nunjucks extends TemplateEngine {
this.nunjucksPrecompiledTemplates = this.config.nunjucksPrecompiledTemplates || {};
this._usingPrecompiled = Object.keys(this.nunjucksPrecompiledTemplates).length > 0;

this.asyncFilters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.asyncFilters || {};
this.filters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.filters

this.setLibrary(this.config.libraryOverrides.njk);

// v3.1.0-alpha.1 we’ve moved to use Nunjucks’ internal cache instead of Eleventy’s
Expand All @@ -28,7 +30,7 @@ export default class Nunjucks extends TemplateEngine {

#getFileSystemDirs() {
let paths = new Set();
paths.add(super.getIncludesDir());
paths.add(super.includesDir);
paths.add(TemplatePath.getWorkingDir());

// Filter out undefined paths
Expand Down Expand Up @@ -100,9 +102,8 @@ export default class Nunjucks extends TemplateEngine {
});

this.setEngineLib(this.njkEnv);

this.addFilters(this.config.nunjucksFilters);
this.addFilters(this.config.nunjucksAsyncFilters, true);
this.addFilters(this.filters);
this.addFilters(this.asyncFilters, true);

// TODO these all go to the same place (addTag), add warnings for overwrites
// TODO(zachleat): variableName should work with quotes or without quotes (same as {% set %})
Expand All @@ -121,7 +122,35 @@ export default class Nunjucks extends TemplateEngine {

addFilters(filters, isAsync) {
for (let name in filters) {
this.njkEnv.addFilter(name, Nunjucks.wrapFilter(name, filters[name]), isAsync);
let callback = filters[name];
if (!isAsync) {
/** @this {any} */
callback = function (...args) {
// Note that `callback` is already a function as the `#add` method throws an error if not.
let ret = filters[name].call(this, ...args);
if (ret instanceof Promise) {
throw new Error(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this supposed to be catched?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s not. This is the same code addFilter added to nunjucks. It has been relocated to the nunjucks file for the purpose of decoupling

`Nunjucks *is* async-friendly with \`addFilter("${name}", async function() {})\` but you need to supply an \`async function\`. You returned a promise from \`addFilter("${name}", function() {})\`. Alternatively, use the \`addAsyncFilter("${name}")\` configuration API method.`,
);
}
return ret;
}
}
else if (isAsync) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong but it doesn't look like you need an else if because isAsync is Boolean already.

// we need to wrap the async function in a nunjucks compatible callback
/** @this {any} */
callback = async function (...args) {
let cb = args.pop();
// Note that `callback` is already a function as the `#add` method throws an error if not.
let ret = await filters[name].call(this, ...args);
cb(null, ret);
}
}
this.njkEnv.addFilter(
name,
Nunjucks.wrapFilter(name, callback),
isAsync,
);
}
}

Expand All @@ -132,7 +161,6 @@ export default class Nunjucks extends TemplateEngine {
source: this.ctx,
lazy: false, // context.env?.opts.throwOnUndefined,
});

return fn.call(this, ...args);
} catch (e) {
throw new EleventyNunjucksError(
Expand Down Expand Up @@ -458,17 +486,15 @@ export default class Nunjucks extends TemplateEngine {
}

return function (data) {
let { promise, resolve, reject } = withResolvers();

tmpl.render(data, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
return new Promise((resolve, reject) => {
tmpl.render(data, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
})
});

return promise;
};
}
}
5 changes: 0 additions & 5 deletions src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ export default class TemplateEngine {
return this.name;
}

// Backwards compat
getIncludesDir() {
return this.includesDir;
}

/**
* @protected
*/
Expand Down
4 changes: 0 additions & 4 deletions src/TemplateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ class TemplateContent {
return this.inputPath;
}

getInputDir() {
return this.inputDir;
}

isVirtualTemplate() {
let def = this.getVirtualTemplateDefinition();
return !!def;
Expand Down
6 changes: 0 additions & 6 deletions src/TemplateLayoutPathResolver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from "node:fs";
import { TemplatePath } from "@11ty/eleventy-utils";
// import debugUtil from "debug";
// const debug = debugUtil("Eleventy:TemplateLayoutPathResolver");
Expand Down Expand Up @@ -43,11 +42,6 @@ class TemplateLayoutPathResolver {
return this.dirs.layouts || this.dirs.includes;
}

/* Backwards compat */
getLayoutsDir() {
return this.layoutsDir;
}

setAliases() {
this.aliases = Object.assign({}, this.config.layoutAliases, this.aliases);
}
Expand Down
5 changes: 0 additions & 5 deletions src/TemplateRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class TemplateRender {
return this.dirs.includes;
}

/* Backwards compat */
getIncludesDir() {
return this.includesDir;
}

get config() {
return this.#config;
}
Expand Down
Loading