nodejs 批量編譯less 文件爲css

http://www.html-js.com/article/1359javascript

咱們在用less時,有時會有不少less塊,一個一個手動編譯很麻煩,使用下面的代碼,能夠一次性遞歸編譯 在項目less文件目錄,新建個js文件。粘貼代碼以下css


var fs = require('fs'), path = require('path'), exec = require('child_process').exec, sourcePath, targetPath;  //獲取命令行中的路徑 process.argv.forEach(function (val, index, array) { if (index == 2) { sourcePath = val; } if (index == 3) { targetPath = val; } }) var lessc = function (rootPath, targetPath) { //取得當前絕對路徑 rootPath = path.resolve(rootPath); //目標路徑絕對路徑 targetPath = path.resolve(targetPath); //判斷目錄是否存在 fs.exists(rootPath, function (exists) {  //路徑存在 if (exists) {  //獲取當前路徑下的全部文件和路徑名 var childArray = fs.readdirSync(rootPath); if (childArray.length) { for (var i = 0; i < childArray.length; i++) { var currentFilePath = path.resolve(rootPath, childArray[i]); var currentTargetPath = path.resolve(targetPath, childArray[i])  //讀取文件信息 var stats = fs.statSync(currentFilePath);  //如果目錄則遞歸調用 if (stats.isDirectory()) { lessc(currentFilePath, currentTargetPath); } else {  //判斷文件是否爲less文件 if (path.extname(currentFilePath) === ".less") { var newFilePath = path.resolve(targetPath, path.basename(currentFilePath, '.less') + ".css"); if (!fs.existsSync(targetPath)) { fs.mkdirSync(targetPath); } console.log(newFilePath); exec("lessc -x " + currentFilePath + " > " + newFilePath); } } } } } else { console.log("directory is not exists"); } }); } lessc('./', './css/'); 

而後運行nodehtml

node 新建的js文件java

相關文章
相關標籤/搜索