forked from gulp-community/gulp-pug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.23 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
'use strict';
const through = require('through2');
const defaultPug = require('pug');
const ext = require('replace-ext');
const PluginError = require('plugin-error');
const log = require('fancy-log');
module.exports = function gulpPug(options) {
const opts = Object.assign({}, options);
const pug = opts.pug || opts.jade || defaultPug;
opts.data = Object.assign(opts.data || {}, opts.locals || {});
return through.obj(function compilePug(file, enc, cb) {
const data = Object.assign({}, opts.data, file.data || {});
opts.filename = file.path;
file.path = ext(file.path, opts.client ? '.js' : '.html');
if (file.isStream()) {
return cb(new PluginError('gulp-pug', 'Streaming not supported'));
}
if (file.isBuffer()) {
try {
let compiled;
const contents = String(file.contents);
if (opts.verbose === true) {
log('compiling file', file.path);
}
if (opts.client) {
compiled = pug.compileClient(contents, opts);
} else {
compiled = pug.compile(contents, opts)(data);
}
file.contents = new Buffer(compiled);
} catch (e) {
return cb(new PluginError('gulp-pug', e));
}
}
cb(null, file);
});
};