@@ -5,8 +5,6 @@ import { TemplatePath } from "@11ty/eleventy-utils";
5
5
import TemplateEngine from "./TemplateEngine.js" ;
6
6
import EleventyBaseError from "../Errors/EleventyBaseError.js" ;
7
7
import { augmentObject } from "./Util/ContextAugmenter.js" ;
8
- import { withResolvers } from "../Util/PromiseUtil.js" ;
9
- import isAsyncFunction from "../Util/IsAsyncFunction.js" ;
10
8
11
9
const debug = debugUtil ( "Eleventy:Nunjucks" ) ;
12
10
@@ -21,10 +19,11 @@ export default class Nunjucks extends TemplateEngine {
21
19
this . nunjucksPrecompiledTemplates = this . config . nunjucksPrecompiledTemplates || { } ;
22
20
this . _usingPrecompiled = Object . keys ( this . nunjucksPrecompiledTemplates ) . length > 0 ;
23
21
24
- this . setLibrary ( this . config . libraryOverrides . njk ) ;
25
22
this . asyncFilters = eleventyConfig . config . __theCodeCriesInPain . nunjucks . asyncFilters || { } ;
26
23
this . filters = eleventyConfig . config . __theCodeCriesInPain . nunjucks . filters
27
24
25
+ this . setLibrary ( this . config . libraryOverrides . njk ) ;
26
+
28
27
// v3.1.0-alpha.1 we’ve moved to use Nunjucks’ internal cache instead of Eleventy’s
29
28
// this.cacheable = false;
30
29
}
@@ -128,7 +127,7 @@ export default class Nunjucks extends TemplateEngine {
128
127
/** @this {any} */
129
128
callback = function ( ...args ) {
130
129
// 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 ) ;
132
131
if ( ret instanceof Promise ) {
133
132
throw new Error (
134
133
`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 {
137
136
return ret ;
138
137
}
139
138
}
140
- else if ( isAsyncFunction ( callback ) ) {
139
+ else if ( isAsync ) {
141
140
// we need to wrap the async function in a nunjucks compatible callback
142
141
/** @this {any} */
143
142
callback = async function ( ...args ) {
144
143
let cb = args . pop ( ) ;
145
144
// 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 ) ;
147
146
cb ( null , ret ) ;
148
147
}
149
148
}
@@ -162,7 +161,6 @@ export default class Nunjucks extends TemplateEngine {
162
161
source : this . ctx ,
163
162
lazy : false , // context.env?.opts.throwOnUndefined,
164
163
} ) ;
165
-
166
164
return fn . call ( this , ...args ) ;
167
165
} catch ( e ) {
168
166
throw new EleventyNunjucksError (
@@ -488,17 +486,15 @@ export default class Nunjucks extends TemplateEngine {
488
486
}
489
487
490
488
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
+ } )
499
497
} ) ;
500
-
501
- return promise ;
502
498
} ;
503
499
}
504
500
}
0 commit comments