forked from gulp-community/gulp-pug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (38 loc) · 1.28 KB
/
index.js
File metadata and controls
45 lines (38 loc) · 1.28 KB
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
'use strict';
var objectAssign = require('object-assign');
var through = require('through2');
var defaultPug = require('pug');
var ext = require('gulp-util').replaceExtension;
var PluginError = require('gulp-util').PluginError;
var log = require('gulp-util').log;
module.exports = function gulpPug(options) {
var opts = objectAssign({}, options);
var pug = opts.pug || opts.jade || defaultPug;
opts.data = objectAssign(opts.data || {}, opts.locals || {});
return through.obj(function compilePug(file, enc, cb) {
var data = objectAssign({}, 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 {
var compiled;
var 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);
});
};