-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
56 lines (43 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const through = require('through2')
const PluginError = require('plugin-error')
const ejs = require('ejs')
const PLUGIN_NAME = 'gulp-ejs'
function render(data, options = {}) {
return through.obj(function(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file)
}
if (file.isStream()) {
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
}
options.filename = file.path
const ejsData = Object.assign({}, data, file.data)
try {
const rendered = ejs.render(file.contents.toString(), ejsData, options)
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) {
this.emit(
'error',
new PluginError(PLUGIN_NAME, err, { fileName: file.path })
)
}
callback()
})
}
// expose ejs object for configuration
render.__EJS__ = ejs
module.exports = render