Back

使用 grunt时,生成多个文件的参数: (options grunt used generate files )

发布时间: 2015-02-24 03:34:00

refer to:  http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically

很多时候, 我们会运行一条命令生成多个文件, 例如: coffee *.coffee 

在grunt中, 运行类似的命令时,就要使用 对应的参数了. 

首先要把 expand: true 设置好.  expand Set to true to enable the following options:

cwd:  当前的工作路径 All src matches are relative to (but don't include) this path.

src: 被处理的文件的格式.  Pattern(s) to match, relative to the cwd.

dest: 生成的文件的路径前缀. Destination path prefix.

ext: 在生成的文件中, 使用这个扩展名.  Replace any existing extension with this value in generated dest paths.

extDot: TODO. Used to indicate where the period indicating the extension is located. Can take either 'first' (extension begins after the first period in the file name) or 'last' (extension begins after the last period), and is set by default to 'first' [Added in 0.4.3]

flatten: 把生成的文件名中的路径部分去掉. 只留下纯文件名.  Remove all path parts from generated dest paths.

rename:  This function is called for each matched src file, (after extension renaming and flattening). The dest and matched src path are passed in, and this function must return a new dest value. If the same dest is returned more than once, each src which used it will be added to an array of sources for it.

下面是一个 例子:  (注意 files: 后面对应的是一个 array [] , 而不是一个 hash: {} )

# Gruntfile.coffee 
module.exports = (grunt) ->
  grunt.initConfig
    coffee:
      compile_multiple_files:
        options:
          bare: true
        files:
          # 编译多个文件
          [
            expand: true
            flatten: true
            cwd: 'app/controllers/'
            src: ['*.coffee']
            dest: 'app/controllers/'
            ext: '.js'
          ]

  #grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-contrib-coffee'

  grunt.registerTask 'default', [ 'coffee' ]

如果遇到 Object true has no method 'indexOf' , 就一定是 files 后面跟的不是 array. 参考:   http://stackoverflow.com/questions/18137432/referring-to-grunt-targets-resulting-in-warning-object-true-has-no-method-inde

Back