Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Less 'CSS' wrapper for SocketStream 0.3

var fs = require('fs'),
pathlib = require('path'),
less = require('less'),
_prependedLess;

Expand All @@ -12,9 +13,37 @@ exports.prependLess = function(prependedLess) {
}
};

function errorBodyStyling(root,path, err) {
path = path.replace(root,'');
var filename = err.filename.replace(root,'');
var r = [
'body:before {',
'display:block;',
'position:absolute;',
'top:15px; left:20px; right:20px;',
'z-index: 10000;',
'content:"Error compiling LESS file: '+path+' at '+(filename||'?')+' '+(err.line||'?')+':'+(err.index||'?')+' '+err.message+'";',
'border: 1px solid #888;',
'border-radius: 5px;',
'background-color: rgba(255,255,255,.5);',
'background-color: white;',
'padding: 10px 15px;',
'}'
];
return r.join('\n');
}

exports.decidePaths = function decidePaths(path, options) {

var p = [ pathlib.dirname(path) ];
//TODO by default we should add the ss.root to paths at end
if (options.paths) p = p.concat(options.paths);
return p;
};

// root and config are passed as arguments, but are not used
//
exports.init = function () {
exports.init = function (root,config) {

return {

Expand All @@ -28,26 +57,45 @@ exports.init = function () {

compile: function(path, options, cb) {

// Get dir from path to enable @include commmand
var dir = path.split('/'); { dir.pop(); }

var input = fs.readFileSync(path, 'utf8');

var compress = options && options.compress;

var parser = new(less.Parser)({paths: [dir.join('/')], filename: path});

if(_prependedLess) {
input = _prependedLess + '\n' + input;
}
var opts = {};
for(var n in config) opts[n] = config[n];
opts.filename = path;
opts.paths = exports.decidePaths(path, config);
opts.compress = options.compress;

less.logger.addListener({
//TODO passing along log output
debug: function(msg) {

},
info: function(msg) {

},
warn: function(msg) {

},
error: function(msg) {

}
});

//TODO it seems less still blows up on some path errors, is there a way to catch them?

parser.parse(input, function(err, tree) {
less.render(input, opts, function(err, out) {
if (err) {
console.log('! - Unable to compile Less file %s into CSS', path);
console.log(err);
return cb(errorBodyStyling(root, path, err));
}
var css = tree.toCSS({compress: compress});
cb(css);
//TODO out.map
cb(out.css);
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"node": ">= 0.10.0"
},
"dependencies": {
"less": "1.7.x"
"less": "2.0.x"
},
"devDependencies": {}
}