gulp.watch监听文件

Gulp.watch()会返回咱们熟知的watcher。咱们能够利用watcher来监听额外的事件或者向watch中添加文件。html

例如,在执行一系列任务和调用一个函数时,你就能够在返回的watcher中添加监听change事件:git

var watcher = gulp.watch('templates/*.tmpl.html', ['build']);
watcher.on('change', function (event) {
   console.log('Event type: ' + event.type); // added, changed, or deleted
   console.log('Event path: ' + event.path); // The path of the modified file
});

除了change事件,还能够监听不少其余的事件:github

  • end 在watcher结束时触发(这意味着,在文件改变的时候,任务或者回调不会执行)
  • error 在出现error时触发
  • ready 在文件被找到并正被监听时触发
  • nomatch 在glob没有匹配到任何文件时触发

Watcher对象也包含了一些能够调用的方法:gulp

  • watcher.end() 中止watcher(以便中止执行后面的任务或者回调函数)
  • watcher.files() 返回watcher监听的文件列表
  • watcher.add(glob) 将与指定glob相匹配的文件添加到watcher(也接受可选的回调当第二个参数)
  • watcher.remove(filepath) 从watcher中移除个别文件

 

更多详情,请看:http://www.tuicool.com/articles/AzI3Ib函数

其余文章:http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/ui

相关文章
相关标签/搜索