Skip to content

Commit 2a5f76e

Browse files
Tietewrogeriopvl
authored andcommitted
feat: add async rendering support
Add async option to support the new ejs async renderfile feature (since v2.5.8) Check https://github.com/mde/ejs/releases/tag/v2.5.8
1 parent 737796a commit 2a5f76e

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ ejs.__EJS__.fileLoader = function () { /* custom file loader */ }
5656

5757
**Note:** As of version 4, the exported ejs object was renamed from `ejs` to `__EJS__`.
5858

59+
### Async rendering (requires runtime support)
60+
61+
You can use async/await in ejs template by passing `{async: true}` in options:
62+
63+
```javascript
64+
const ejs = require('gulp-ejs')
65+
66+
async function blah() { /* async task */ }
67+
68+
gulp.src("./templates/*.ejs")
69+
.pipe(ejs({blah}, {async: true}))
70+
.pipe(gulp.dest("./dist"))
71+
```
72+
73+
In template, await will be used to call async functions for example: `<%= await blah() %>`
74+
5975
## API
6076

6177
### ejs(data, options)

index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,24 @@ function render(data, options = {}) {
2121
const ejsData = Object.assign({}, data, file.data)
2222

2323
try {
24-
file.contents = Buffer.from(
25-
ejs.render(file.contents.toString(), ejsData, options)
26-
)
27-
28-
this.push(file)
24+
const rendered = ejs.render(file.contents.toString(), ejsData, options)
25+
26+
if (options.async && typeof rendered.then === 'function') {
27+
rendered.then(rendered => {
28+
file.contents = Buffer.from(rendered)
29+
this.push(file)
30+
}).catch(err => {
31+
this.emit(
32+
'error',
33+
new PluginError(PLUGIN_NAME, err, { fileName: file.path })
34+
)
35+
}).then(callback)
36+
37+
return
38+
}
39+
40+
file.contents = Buffer.from(rendered);
41+
this.push(file);
2942
} catch (err) {
3043
this.emit(
3144
'error',

test/index.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,37 @@ describe('gulp-ejs', function() {
104104

105105
stream.end()
106106
})
107+
108+
it('should render async ejs template', function(done) {
109+
/* Skip test if async function is not supported (Node 7.5-) */
110+
try {
111+
/* eslint-disable-next-line no-new-func */
112+
(new Function('return (async function(){}).constructor;'))()
113+
} catch (e) {
114+
if (e instanceof SyntaxError) {
115+
this.skip()
116+
} else {
117+
throw e
118+
}
119+
}
120+
121+
const title = () => new Promise((resolve, reject) => {
122+
process.nextTick(() => resolve('gulp-ejs'))
123+
})
124+
const stream = ejs({ title: title }, { async: true })
125+
126+
stream.on('data', data => {
127+
assert.strictEqual(data.contents.toString(), '<h1>gulp-ejs</h1>')
128+
})
129+
130+
stream.on('end', done)
131+
132+
stream.write(
133+
new Vinyl({
134+
contents: Buffer.from('<h1><%= await title() %></h1>')
135+
})
136+
)
137+
138+
stream.end()
139+
})
107140
})

0 commit comments

Comments
 (0)