javascript - Less to css via node script -
found solution see bottom of post
trying build less (less css) using build script nodejs
i trying build css file less using node script, file structure cannot change , like:
/public/css/app.css /resources/assets/less/xenon.less /quicksync/02_lesscompiler.js
the contents of less file include other less files same level , onward in filstructure .less file itself, eg:
// import bootstrap variables & mixins @import "bs-less/variables.less"; @import "bs-less/mixins.less"; // lesshat @import "other-less/lesshat.less"; ... etc etc
from above link tried following:
var less = require( 'less' ); var fs = require( 'fs' ); var path = require('path'); var basepath = __dirname + '/..'; var lesspath = basepath + '/resources/assets/less/xenon.less', outputpath = basepath + '/public/css/app.css'; fs.readfile( lesspath ,function(error,data){ data = data.tostring(); console.log( error ); console.log( data ); less.render(data, function (e, css) { console.log( e ); console.log( css ); fs.writefile( outputpath, css, function(err){ if( err ){ console.log(err ); } console.log('done'); }); }); });
the console output of above solution is:
null // import bootstrap variables & mixins @import "bs-less/variables.less"; @import "bs-less/mixins.less"; // lesshat @import "other-less/lesshat.less"; // xenon basic ui @import "xenon-core.less"; // forms @import "xenon-forms.less"; // xenon components @import "xenon-components.less"; // xenon skins @import "xenon-skins.less"; { [error: 'xenon-skins.less' wasn't found. tried - xenon-skins.less,xenon-skins.less] type: 'file', filename: 'input', index: 311, line: 18, callline: nan, callextract: undefined, column: 0, extract: [ '// xenon skins', '@import "xenon-skins.less";', undefined ], message: '\'xenon-skins.less\' wasn\'t found. tried - xenon-skins.less,xenon-skins.less', stack: undefined } undefined done
however, output file reads
undefined undefined
i found guys blog: http://onedayitwillmake.com/blog/2013/03/compiling-less-from-a-node-js-script/ , tried following:
var less = require( 'less' ); var fs = require( 'fs' ); var path = require('path'); var lesspath = basepath + '/resources/assets/less/xenon.less', outputpath = basepath + '/public/css/app.css'; //ensure directory exists var ensuredirectory = function (filepath) { var dir = path.dirname(filepath); var existssync = fs.existssync || path.existssync; if (!existssync(dir)) { fs.mkdirsync(dir); } }; fs.readfile( lesspath, function(error, data){ var datastring = data.tostring(); var options = { paths : [basepath + '/resources/assets/less'], // .less file search paths outputdir : basepath + '/public/css', // output directory, note '/' optimization : 1, // optimization level, higher better more volatile - 1 value filename : "xenon.less", // root .less file compress : false, // compress? yuicompress : false // use yui compressor? }; console.log( options ); options.outputdir = path.resolve( process.cwd(), options.outputdir) + "/"; ensuredirectory( options.outputdir ); var parser = new less.parser(options); console.log( 'parsing' ); parser.parse( datastring, function ( error, csstree ) { if ( error ) { console.log( 'error compiling less:' ); console.log( error ); return false; } // create css csstree var cssstring = csstree.tocss({ compress: options.compress, yuicompress: options.yuicompress }); fs.writefile(outputpath, cssstring, function (err) { logtime('compiled less: ' + outputpath); //run mai passed callback function callback(); }); }); });
however output of error:
d:\myproject\node_modules\less\lib\less\parser\parser.js:117 imports.contents[fileinfo.filename] = str; ^ typeerror: cannot read property 'contents' of undefined @ object.parser.parse (d:\myproject\node_modules\less\lib\less\parser\parser.js:117:20) @ d:\myproject\quicksync\02_lesscompiler.js:51:16 @ fsreqwrap.readfileafterclose [as oncomplete] (fs.js:380:3)
this driving me crazy, sass go:
require('node-sass').render({ file: scsspath, outfile: outputpath, outputstyle: 'expanded', sourcecomments: true }, function( err, result ){ //write result disk. });
what simple way of building css node less compiler?
solution:
fs.readfile( lesspath ,function(error,data){ data = data.tostring(); less.render(data, { paths: [ basepath + '/resources/assets/less/' ] },function (e, css) { fs.writefile( outputpath, css.css, function(err){ if( err ){ console.log(err ); } console.log('done'); }); }); });
the first works here, maybe should
console.log(error)
in readfile callback, path wrong , file cant opened.
Comments
Post a Comment