@@ -5,8 +5,6 @@ import { TemplatePath } from "@11ty/eleventy-utils";
55import TemplateEngine from "./TemplateEngine.js" ;
66import EleventyBaseError from "../Errors/EleventyBaseError.js" ;
77import { augmentObject } from "./Util/ContextAugmenter.js" ;
8- import { withResolvers } from "../Util/PromiseUtil.js" ;
9- import isAsyncFunction from "../Util/IsAsyncFunction.js" ;
108
119const 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}
0 commit comments