冠军

导航

Grunt 之通配符

在描述源码路径的时候,经常有一些特殊的奇怪的要求。Grunt 通过内建的 node-globminimatch 库提供了文件名的扩展机制。

常见的通配符如下:

  • * 匹配除了 / 之外的任意数量的数字和字符
  • ? 匹配除了 / 之外的单个字符
  • ** 匹配任意数量的字符,包括 /,这样可以包含任意级的路径
  • {} 提供一个以逗号 (,) 分割的或表达式列表
  • ! 放在表达式的开头表示取反

比如,foo/*.js 将会匹配 foo/ 文件夹下面的所有 .js 扩展名的文件,而 foo/**/*.js 则会匹配在 foo/ 目录下任意级别子目录中的 .js 扩展名的文件。

使用 ! 来不包含特定的文件,需要注意的是 ! 需要是路径的第一个字符。

为了更加简单地通配符,Grunt 允许使用数组来表示通配符。Grunt 将会安装顺序处理,返回的结果是唯一的。

例如

// You can specify single files, 简单文件名:
{src: 'foo/this.js', dest: ...}
// Or arrays of files, 使用数组表示多个文件名:
{src: ['foo/this.js', 'foo/that.js', 'foo/the-other.js'], dest: ...}
// Or you can generalize with a glob pattern, 使用通配符:
{src: 'foo/th*.js', dest: ...}

// This single node-glob pattern, 单个通配符:
{src: 'foo/{a,b}*.js', dest: ...}
// Could also be written like this, 通过数组,使用多个通配符:
{src: ['foo/a*.js', 'foo/b*.js'], dest: ...}

// All .js files, in foo/, in alpha order, 所有的 .js 文件,按照字符顺序:
{src: ['foo/*.js'], dest: ...}
// Here, bar.js is first, followed by the remaining files, in alpha order, 第一个是 bar.js, 其它文件按字母顺序 :
{src: ['foo/bar.js', 'foo/*.js'], dest: ...}

// All files except for bar.js, in alpha order, 除了 bar.js 之外的文件,按字母顺序:
{src: ['foo/*.js', '!foo/bar.js'], dest: ...}
// All files in alpha order, but with bar.js at the end, 所有文件按照字母顺序,bar.js 在最后.
{src: ['foo/*.js', '!foo/bar.js', 'foo/bar.js'], dest: ...}

// Templates may be used in filepaths or glob patterns, 可以嵌入表达式:
{src: ['src/<%= basename %>.js'], dest: 'build/<%= basename %>.min.js'}
// But they may also reference file lists defined elsewhere in the config, 引用其它地方定义的文件列表:
{src: ['foo/*.js', '<%= jshint.all.src %>'], dest: ...}

更多信息可以见 node-globminimatch 的文档。

 

posted on 2015-09-08 17:36  冠军  阅读(1700)  评论(0编辑  收藏  举报