Skip to content

Commit 48495a8

Browse files
committed
a few bug fixes
1 parent 7c806bb commit 48495a8

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/Engines/Nunjucks.js

+13-17
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { TemplatePath } from "@11ty/eleventy-utils";
55
import TemplateEngine from "./TemplateEngine.js";
66
import EleventyBaseError from "../Errors/EleventyBaseError.js";
77
import { augmentObject } from "./Util/ContextAugmenter.js";
8-
import { withResolvers } from "../Util/PromiseUtil.js";
9-
import isAsyncFunction from "../Util/IsAsyncFunction.js";
108

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

@@ -21,10 +19,11 @@ export default class Nunjucks extends TemplateEngine {
2119
this.nunjucksPrecompiledTemplates = this.config.nunjucksPrecompiledTemplates || {};
2220
this._usingPrecompiled = Object.keys(this.nunjucksPrecompiledTemplates).length > 0;
2321

24-
this.setLibrary(this.config.libraryOverrides.njk);
2522
this.asyncFilters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.asyncFilters || {};
2623
this.filters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.filters
2724

25+
this.setLibrary(this.config.libraryOverrides.njk);
26+
2827
// v3.1.0-alpha.1 we’ve moved to use Nunjucks’ internal cache instead of Eleventy’s
2928
// this.cacheable = false;
3029
}
@@ -128,7 +127,7 @@ export default class Nunjucks extends TemplateEngine {
128127
/** @this {any} */
129128
callback = function (...args) {
130129
// Note that `callback` is already a function as the `#add` method throws an error if not.
131-
let ret = callback.call(this, ...args);
130+
let ret = filters[name].call(this, ...args);
132131
if (ret instanceof Promise) {
133132
throw new Error(
134133
`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.`,
@@ -137,13 +136,13 @@ export default class Nunjucks extends TemplateEngine {
137136
return ret;
138137
}
139138
}
140-
else if (isAsyncFunction(callback)) {
139+
else if (isAsync) {
141140
// we need to wrap the async function in a nunjucks compatible callback
142141
/** @this {any} */
143142
callback = async function (...args) {
144143
let cb = args.pop();
145144
// Note that `callback` is already a function as the `#add` method throws an error if not.
146-
let ret = await callback.call(this, ...args);
145+
let ret = await filters[name].call(this, ...args);
147146
cb(null, ret);
148147
}
149148
}
@@ -162,7 +161,6 @@ export default class Nunjucks extends TemplateEngine {
162161
source: this.ctx,
163162
lazy: false, // context.env?.opts.throwOnUndefined,
164163
});
165-
166164
return fn.call(this, ...args);
167165
} catch (e) {
168166
throw new EleventyNunjucksError(
@@ -488,17 +486,15 @@ export default class Nunjucks extends TemplateEngine {
488486
}
489487

490488
return function (data) {
491-
let { promise, resolve, reject } = withResolvers();
492-
493-
tmpl.render(data, (error, result) => {
494-
if (error) {
495-
reject(error);
496-
} else {
497-
resolve(result);
498-
}
489+
return new Promise((resolve, reject) => {
490+
tmpl.render(data, (error, result) => {
491+
if (error) {
492+
reject(error);
493+
} else {
494+
resolve(result);
495+
}
496+
})
499497
});
500-
501-
return promise;
502498
};
503499
}
504500
}

src/UserConfig.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ class UserConfig {
283283

284284
getFilters(options = {}) {
285285
let working = this.universal.filters;
286-
if (options.type) {
286+
if (options.async) {
287287
working = objectFilter(
288288
this.universal.filters,
289-
(entry) => entry.__eleventyInternal?.type === options.type,
289+
(entry) => entry.__eleventyInternal?.async === options.async,
290290
);
291291
}
292292
if (options.lang) {
@@ -1253,11 +1253,13 @@ class UserConfig {
12531253
}),
12541254
filters: this.getFilters({
12551255
lang: "njk",
1256+
async: false,
12561257
}),
12571258
},
12581259
liquid: {
12591260
filters: this.getFilters({
12601261
lang: "liquid",
1262+
async: false,
12611263
}),
12621264
}
12631265
},

0 commit comments

Comments
 (0)