-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler-less.js
50 lines (50 loc) · 1.59 KB
/
handler-less.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
var Resource = require('./protolus-resource');
var Class = require('Classy');
var less = require('less');
var LessHandler = new Class({
Extends : Resource.Handler,
initialize : function(options){
this.parent('less');
},
handle : function(options, callback){
var path = process.cwd()+options.location;
var parts = path.split('/');
parts.pop();
var dir = parts.join('/');
try{
var parser = new(less.Parser)({
paths: [dir], // Specify search paths for @import directives
filename: path // Specify a filename, for better error messages
});
parser.parse(options.body, function (e, tree) {
if(e){
console.log('LESS ERROR', e);
callback('/* ERROR : '+e.message+'*'+'/');
}else{
try{
var css = tree.toCSS({ compress: options.compact || false });
callback(css);
}catch(ex){
console.log('LESS ERROR', ex);
}
}
});
}catch(ex){
callback('/* ERROR : '+ex.message+'*'+'/');
}
},
tagProfile : function(){
return {
name : 'link',
attrs : {
rel:"stylesheet",
type:"text/css"
},
target : 'href'
};
}
});
var instance = new LessHandler();
Resource.registerHandler('less', instance);
LessHandler.instance = instance;
module.exports = LessHandler;