-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (57 loc) · 1.64 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
57
58
59
60
61
62
63
64
var async = require('async');
var path = require('path');
var less = require('less');
var fs = require('fs');
function isLess(filename) {
var ext = path.extname(filename);
if (ext === '.less') {
return true;
}
return false;
}
module.exports = function (builder, options) {
options = options || {};
var bindex = 0;
var lessImport = [];
builder.on('dependency', depHandler);
function depHandler(b){
b.dep = true;
b.on('dependency', depHandler);
if(!b.config.styles) return;
b.config.styles.forEach(function(file){
if(isLess(file) && !~lessImport.indexOf(b.path(file))){
lessImport.push(b.path(file));
}
});
}
builder.hook('before styles', function (builder, callback) {
if(!builder.root) {
if(!builder.config.styles) return callback();
builder.config.styles.forEach(function(file){
isLess(file) && builder.removeFile('styles', file);
});
return callback();
}
builder.config.styles.slice().forEach(function(file){
if(isLess(file) && !~lessImport.indexOf(builder.path(file))){
lessImport.push(builder.path(file));
}
isLess(file) && builder.removeFile('styles', file);
});
var importsLess = "";
lessImport.forEach(function(file){
importsLess+='@import "'+file +'";\n';
});
var parser = new less.Parser(options.env || {});
var cssConfig = options.cssConfig || {};
parser.parse(importsLess, function (error, tree) {
if (error) {
return callback(error);
}
var css = tree.toCSS(cssConfig);
builder.addFile('styles', "compiledLess", css);
callback()
});
});
};
module.exports.ext = ['.less']