如何让grunt-sass以递归方式处理整个目录?

时间:2015-10-21 22:08:25

标签: sass gruntjs

我一直无法运行grunt-sass任务,因为我想使用这样的配置:

sass: {
    options: {
        outputStyle: 'compressed'
    },
    dist: {
        src: 'src/client/**/*.scss',
        dest: 'src/client/**/*.css'
    }
}

它似乎没有响应通配符选择器我只能指定一个完整的文件名才能使它工作,所以我编写了自己的任务函数,但我不知道如何调用grunt-从我的功能中使用适当的参数?

function compileSass(path){
    var fs = require('fs'),
        sass = require('node-sass'),
        compileSassFile = function(path){
            grunt.log.writeln('compile file: '+path);
            // how to call grunt-sass here with options {outputStyle: 'compressed'} and dist {src: path: dest: path.replace('.scss', '.css')} ?
        },
        processDir = function(path){
            var files = fs.readdirSync(path);
            for(var i = 0; i < files.length; i++){
                var fileName = files[i];
                if(fs.statSync(path + '/' + fileName).isDirectory()){
                    processDir(path + '/' + fileName);
                } else if(fileName.indexOf('.scss') > 0 && fileName.indexOf('_') !== 0){
                    compileSassFile(path + '/' + fileName)
                }
            }
        };
        processDir(path);
}

grunt.registerTask('buildSass', function(){compileSass('src/client')});

2 个答案:

答案 0 :(得分:1)

值得一读Grunt Files。它为您的任务将针对哪些文件以及它将对输出执行的操作提供了很大的灵活性。

你的Grunt任务应该是这样的:

sass: {
    options: {
        outputStyle: 'compressed'
    },
    dist: {
        files: [
          {
              expand: true, // Recursive
              cwd: "src/client", // The startup directory
              src: ["**/*.scss"], // Source files
              dest: "src/client", // Destination
              ext: ".css" // File extension 
          }
        ]
    }
}

答案 1 :(得分:0)

Colin Bacons更直接地回答问题的问题,但是我实际上最终使用的另一种方法是不使用grunt-sass,而是使用grunt-exec并将node-sass直接添加到我的拥有package.json文件,然后我用grunt-exec直接从终端运行node-sass:

public function getAllCat(){
    $query = sprintf("SELECT * FROM `itemCat`");
        $result = $this->localDB->query($query);    
$cat = '<option value="">Please Select</option>';
$data = array();
    while($row = $this->localDB->fetch_assoc($result))
{
array_push($data, array('iditemCat'=>$row['iditemCat'], 'itemCatName'=>$row['itemCatName'],  'itemCatChildof'=>$row['itemCatChildof']));

}

$sortedArray = array();
foreach($data as $d) {
        if($d['itemCatChildof'] == 0) {
            $sortedArray[$d['iditemCat']] = $d;
        } else {
            $sortedArray[$d['itemCatChildof']]['children'][] = $d;
        }
    }


 foreach($sortedArray as $value){
   $cat .= '<optgroup label="'.$value['itemCatName'].'">';
    foreach($value['children'] as $child){
        $cat .='<option     value="'.$child['iditemCat'].'">'.$child['itemCatName'].'</option>';
    }
  $cat .=' </optgroup>';
 }

 return $cat;
}