nodejs 批量编译less 文件为css

news/2024/7/19 12:56:47 标签: nodejs, less, css, js, function

我们在用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文件




http://www.niftyadmin.cn/n/951140.html

相关文章

零缺陷编程读书笔记(一)引言

我们带着下面3个问题来学习引言&#xff1a; 1 你肯为自己的bug买单吗&#xff1f; 2 测试是谁的工作&#xff1a;开发工程师&#xff0c;测试工程师&#xff1f; 3 有错误就是有错误&#xff0c;版本备份&#xff01; 1 几年前在一次偶然翻阅 Donald Knuth所著《TEX The …

SVG Animation动画

http://www.waylau.com/svg-animation/

零缺陷编程读书笔记(二)假想的编译程序

1 读者可以考虑一下倘若编译程序能够正确地指出代码中的所有问题&#xff0c;那相应程序的错误 情况会怎样&#xff1f;这不单指语法错误&#xff0c;还包括程序中的任何问题&#xff0c;不管它有多么隐蔽。 2 假如在间谍卫星上用摄像机对准某个典型的软件车间。就会看到程…

TTF文字文件在线转成base64代码格式网址

http://www.motobit.com/util/base64-decoder-encoder.asp 摘自&#xff1a;http://blog.netsh.org/posts/download-webfont-base64_1714.netsh.html 各种文字文件类型互相转换网址大全&#xff1a;http://everythingfonts.com/

零缺陷编程读书笔记(三)自己设计并使用断言(1)

1 该语句在多数情况下都会工作得很好&#xff0c;除非 malloc的调用产生失败。当 malloc失败时&#xff0c;就会给 memcpy返回一个 NULL指针。由于 memcpy处理不了 NULL指针&#xff0c;所以出现了错误。 编译程序查不出这种或其他类似的错误。同样&#xff0c;编译程序也查不…

20个XR项目路演,近20个资本机构出席!

诚邀您参加2020 Qualcomm XR生态合作伙伴大会 2020 Qualcomm XR生态合作伙伴大会暨第二届XR创新应用挑战赛颁奖典礼将于2020年9月5日在江西南昌举行。届时&#xff0c;来自XR&#xff08;扩展现实&#xff09;领域的领先企业以及业界专家将就当前XR技术和应用进行交流&#xff…

js判断ie版本以及怪异模式,纯JS,可封装组件。

js判断ie版本以及怪异模式&#xff0c;纯JS&#xff0c;可封装组件。 var Brower function checkIE(){// 判断是否为IEvar isIE navigator.userAgent.toLocaleLowerCase().indexOf(msie) ! -1;// 判断是否为IE5678var isLteIE8 isIE && ![1,];// 用于防止因通过IE8的…