-
Notifications
You must be signed in to change notification settings - Fork 116
/
index.js
56 lines (44 loc) · 1.41 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
var less = require('less');
var through2 = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var path = require('path');
var defaults = require('lodash.defaults');
module.exports = function (options) {
// Mixes in default options.
options = defaults(options || {}, {
compress: false,
});
function transform (file, enc, next) {
var self = this;
if (file.isNull()) {
this.push(file); // pass along
return next();
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-less', 'Streaming not supported'));
return next();
}
var str = file.contents.toString('utf8');
// Clones the options object.
var opts = defaults({}, options);
// Injects the path of the current file.
opts.filename = file.path;
less.render(str, opts, function (err, css) {
if (err) {
// convert the keys so PluginError can read them
err.lineNumber = err.line;
err.fileName = err.filename;
// add a better error message
err.message = err.message + ' in file ' + err.fileName + ' line no. ' + err.lineNumber;
self.emit('error', new PluginError('gulp-less', err));
} else {
file.contents = new Buffer(css);
file.path = gutil.replaceExtension(file.path, '.css');
self.push(file);
}
next();
});
}
return through2.obj(transform);
};