diff --git a/lib/wrapper.js b/lib/wrapper.js index 2864ea8..b39c942 100644 --- a/lib/wrapper.js +++ b/lib/wrapper.js @@ -3,6 +3,7 @@ // Less 'CSS' wrapper for SocketStream 0.3 var fs = require('fs'), + pathlib = require('path'), less = require('less'), _prependedLess; @@ -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 { @@ -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); }); } }; diff --git a/package.json b/package.json index 1cfd96b..3ab0754 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "node": ">= 0.10.0" }, "dependencies": { - "less": "1.7.x" + "less": "2.0.x" }, "devDependencies": {} }