2017-04-06 33 views
0

我很新的呻吟,我想达到以下目标咕噜创建并运行更少的编译任务

  • 迭代在指示位置的每个目录
  • 编译更少的文件,以CSS

源:

  • 主题1
    • file.less
  • 主题2
    • file.less
  • 主题3
    • file.less

后处理:

  • 主题1
    • file.less
    • out.css
  • 主题2
    • file.less
    • out.css
  • 主题3
    • file.less
    • out.css

我读的地方,咕噜这样做,这是我的整个run.js文件:

var grunt = require('grunt'), 
    less = require('less'); 

    grunt.initConfig({ 
    less: { 
     compileCore: { 
     options: { 
      strictMath: true 
     }, 
     files: [{ 
      expand: true, 
      src: ['themes/**/file.less'], 
      ext: '.css', 
      extDot: 'first' 
     }] 
     } 
    } 
    }); 

    grunt.registerTask('default', ['less']); 

    grunt.task.run('default'); 

它不给任何错误,但它也不工作,我怎么能使这工作? (再次,我有呼噜声没有任何先进经验,还)

,如果可能也实现只用命令行执行这个目标(无乳宁Node.js的文件)​​,这将是一件好事;)

回答

1

你可以使用grunt.file.expandMapping(https://gruntjs.com/api/grunt.file#grunt.file.expandmapping) 来遍历目录中的所有文件,并根据需求提供选项。

下面的代码将根据您的要求工作。

module.exports = function (grunt) { 
 
    grunt.initConfig({ 
 
     less: { 
 
      development: { 
 
       files: grunt.file.expandMapping(['themes/**/*.less'], '', { 
 
        rename: function (destBase, destPath) { 
 
         return destBase + destPath.replace('.less', '.css'); 
 
        } 
 
       }) 
 
      } 
 
     } 
 
    }); 
 
    grunt.loadNpmTasks('grunt-contrib-less'); 
 
    grunt.registerTask('default', ['less']); 
 
}