zoukankan      html  css  js  c++  java
  • nodejs 批量编译less 文件为css

    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文件

  • 相关阅读:
    TortoiseGit 连接 git服务器免输入用户名和密码的方法
    mongodb 对参数类型的严格区分
    google API 使用Client Login 登录授权
    GAPI is the Google Analytics PHP5 Interface
    pr导出mp4格式提示无法播放解决方案
    PR如何导出mp4格式的视频
    pr 如何给视频进行加速,慢速处理
    如何用premiere添加配乐?pr视频添加音乐
    操作系统-银行家算法
    操作系统 内存分配算法
  • 原文地址:https://www.cnblogs.com/xuxian/p/4201012.html
Copyright © 2011-2022 走看看