Created
June 15, 2012 15:59
-
-
Save vekexasia/2937237 to your computer and use it in GitHub Desktop.
Node js less compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
less = require('less'), | |
compressor = require('node-minify'), | |
directory = 'bootstrapless/', | |
rootLess = 'bootstrap.less'; | |
var parser = new(less.Parser)({ | |
paths: [directory] // Specify search paths for @import directives | |
}); | |
fs.watch(directory, onFileEvent); | |
// Below this line we find only functions | |
/** | |
* Called when an event occurs on a file inside the watched directory | |
* @param event the event ( String ) | |
* @param filename the filename ( withouth abs path ) | |
*/ | |
function onFileEvent(event, filename) { | |
if (event == 'change') { | |
if ( /[a-zA-Z]+\.less$/.test(filename)) { | |
fs.readFile(directory+rootLess, 'ascii', onFileRead); | |
} | |
} | |
} | |
/** | |
* Called when the readFile ended to read the file | |
* @param err | |
* @param data | |
*/ | |
function onFileRead(err, data) { | |
if (err){ | |
throw err; | |
} | |
parser.parse(data, onLessParsed); | |
} | |
function onLessParsed (e, tree) { | |
if (e) { | |
throw e; | |
} | |
try { | |
var outCss = tree.toCSS({compress:false}); | |
var outFile = directory+"out/"+rootLess.replace('.less','.css'); | |
fs.writeFile(outFile, tree.toCSS({compress:false}), function (err) { | |
onFileWritten(err, outFile); | |
} ); | |
} catch (e) { | |
console.log(e.filename+"["+e.line+"]: "+e.message); | |
} | |
} | |
/** | |
* On File written to css | |
* @param err | |
*/ | |
function onFileWritten (err, outFile) { | |
if (err) throw err; | |
console.log(outFile+" Saved!"); | |
new compressor.minify({ | |
type: 'yui-css', | |
fileIn: outFile, | |
fileOut: outFile.replace('.css','.min.css') | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment