nodejs 批量編譯less 檔案為css

來源:互聯網
上載者:User

標籤:

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

我們在用less時,有時會有很多less塊,一個一個手動編譯很麻煩,使用下面的代碼,可以一次性遞迴編譯 在項目less檔案目錄,建立個js檔案。粘貼代碼如下

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/‘);

然後運行node

node 建立的js檔案

nodejs 批量編譯less 檔案為css

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.