forked from rotundasoftware/sass-css-stream
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
54 lines (45 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
47
48
49
50
51
52
53
54
var through = require('through');
var less = require('less');
var path = require('path');
var extend = require('extend-object');
module.exports = function(file, opts) {
var data = '',
config = extend({
compress: false,
paths: []
}, opts);
if (!/\.less$/.test(file)) {
return through();
}
return through(write, end);
function write(buf) {
data += buf;
}
function end() {
var self = this;
var fileConfig = extend({}, config);
// Injects the path of the current file.
fileConfig.filename = file;
less.render(data, fileConfig, function (err, output) {
if (err) {
self.emit('error', new Error(getErrorMessage(err), file, err.line));
} else {
self.queue(output.css);
}
self.queue(null);
});
}
function getErrorMessage(err) {
var msg = err.message;
if (err.line) {
msg += ", line " + err.line;
}
if (err.column) {
msg += ", column " + err.column;
}
if (err.extract) {
msg += ": \"" + err.extract + "\"";
}
return msg;
}
};