Comparing version 4.0.0 to 4.1.0
# Changelog | ||
## v4.1.0 | ||
- Add support for the new ejs ([v.2.5.8](https://github.com/mde/ejs/releases/tag/v2.5.8)) async rendering feature. (@Tietew) | ||
## v4.0.0 | ||
@@ -4,0 +8,0 @@ |
21
index.js
@@ -24,7 +24,20 @@ 'use strict' | ||
try { | ||
file.contents = Buffer.from( | ||
ejs.render(file.contents.toString(), ejsData, options) | ||
) | ||
const rendered = ejs.render(file.contents.toString(), ejsData, options) | ||
this.push(file) | ||
if (options.async && typeof rendered.then === 'function') { | ||
rendered.then(rendered => { | ||
file.contents = Buffer.from(rendered) | ||
this.push(file) | ||
}).catch(err => { | ||
this.emit( | ||
'error', | ||
new PluginError(PLUGIN_NAME, err, { fileName: file.path }) | ||
) | ||
}).then(callback) | ||
return | ||
} | ||
file.contents = Buffer.from(rendered); | ||
this.push(file); | ||
} catch (err) { | ||
@@ -31,0 +44,0 @@ this.emit( |
{ | ||
"name": "gulp-ejs", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "A plugin for Gulp that parses ejs template files", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -59,2 +59,24 @@ # gulp-ejs [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url] | ||
### Async rendering (requires runtime support) | ||
Since ejs [v2.5.8](https://github.com/mde/ejs/releases/tag/v2.5.8) added support for promise/async-await `renderFile`, you can now use this option with gulp-ejs v4.1.0. | ||
You can use async/await in your ejs templates by passing `{ async: true }` in the ejs options hash: | ||
```javascript | ||
const ejs = require('gulp-ejs') | ||
async function foobar() { /* async task */ } | ||
gulp.src('./templates/*.ejs') | ||
.pipe(ejs({ foobar }, { async: true })) | ||
.pipe(gulp.dest('./dist')) | ||
``` | ||
Then in your templates use `await` to call async functions. Here's an example: | ||
```javascript | ||
<%= await foobar() %> | ||
``` | ||
## API | ||
@@ -61,0 +83,0 @@ |
@@ -107,2 +107,35 @@ 'use strict' | ||
}) | ||
it('should render async ejs template', function(done) { | ||
/* Skip test if async function is not supported (Node 7.5-) */ | ||
try { | ||
/* eslint-disable-next-line no-new-func */ | ||
(new Function('return (async function(){}).constructor;'))() | ||
} catch (e) { | ||
if (e instanceof SyntaxError) { | ||
this.skip() | ||
} else { | ||
throw e | ||
} | ||
} | ||
const title = () => new Promise((resolve, reject) => { | ||
process.nextTick(() => resolve('gulp-ejs')) | ||
}) | ||
const stream = ejs({ title: title }, { async: true }) | ||
stream.on('data', data => { | ||
assert.strictEqual(data.contents.toString(), '<h1>gulp-ejs</h1>') | ||
}) | ||
stream.on('end', done) | ||
stream.write( | ||
new Vinyl({ | ||
contents: Buffer.from('<h1><%= await title() %></h1>') | ||
}) | ||
) | ||
stream.end() | ||
}) | ||
}) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
129
11791
11
150
2