-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (35 loc) · 1.05 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
const through = require('through2');
const Twig = require('twig');
const gutil = require('gulp-util');
const merge = require('merge');
const PluginError = gutil.PluginError;
module.exports = function(opt) {
function transform(file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError('gulp-compile-twig', 'Streaming not supported'));
}
const options = merge({
twig: 'twig'
}, opt);
let data;
try {
// Reset all caches
Twig.cache();
const template = Twig.twig({
id: file.relative,
data: file.contents.toString('utf8')
});
data = template.compile(options);
}
catch (err) {
return cb(new PluginError('gulp-compile-twig', err));
}
file.contents = new Buffer(data);
file.path = `${ file.path }.js`;
cb(null, file);
}
return through.obj(transform);
};