首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grunt-contrib-watch的监控子任务

grunt-contrib-watch的监控子任务
EN

Stack Overflow用户
提问于 2013-05-13 08:42:58
回答 2查看 2.1K关注 0票数 4

我有以下Gruntfile.coffee。我正在监视监视任务,如下所示,以查看文件更改,然后将更改后的文件编译为coffee script。

代码语言:javascript
复制
# Watch task
watch:
 coffee:
  files: ['client/**/*.coffee','server/**/*/.coffee']
  options:
   nospawn: true
   livereload: true

# Watch changed files
grunt.event.on 'watch', (action, filepath) ->
 cwd = 'client/'
 filepath = filepath.replace(cwd,'')
 grunt.config.set('coffee',
  changed:
   expand: true
   cwd: cwd
   src: filepath
   dest: 'client-dist/'
   ext: '.js'
 )
 grunt.task.run('coffee:changed')

但是,我想添加另一个监视任务来复制不是咖啡文件的文件。我将如何监控这些更改?

我想做一件

代码语言:javascript
复制
# Watch copy task
grunt.event.on 'watch:copy', (action,filepath) -> ...
# Watch coffee task
grunt.event.on 'watch:coffee', (action,filepath) -> ...

但这似乎并不管用。想法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-13 11:44:25

我的解决方案--完成了工作,但并不美观。我欢迎更好的答案

基本上,我匹配传入文件的路径

如果其.coffee运行task编译任务

如果其.*运行复制任务

代码语言:javascript
复制
# Watch changed files
grunt.event.on 'watch', (action, filepath) ->

 # Determine server or client folder
 path = if filepath.indexOf('client') isnt -1 then 'client' else 'server'
 cwd = "#{path}/"
 filepath = filepath.replace(cwd,'')        

 # Minimatch for coffee files
 if minimatch filepath, '**/*.coffee'
  # Compile changed file
  grunt.config.set('coffee',
   changed:
    expand: true
    cwd: cwd
    src: filepath
    dest: "#{path}-dist/"
    ext: '.js'
  )
  grunt.task.run('coffee:changed')  

 # Minimatch for all others
 if minimatch filepath, '**/*.!(coffee)'
  # Copy changed file
  grunt.config.set('copy',
   changed:
    files: [
     expand: true
     cwd: cwd
     src: filepath
     dest: "#{path}-dist/"                      
    ]
  )
  grunt.task.run("copy:changed")
票数 2
EN

Stack Overflow用户

发布于 2013-05-13 09:46:58

看一下手表事件示例底部的注释:https://github.com/gruntjs/grunt-contrib-watch#using-the-watch-event

watch事件并不是用来替换Grunt API的。请改用tasks

代码语言:javascript
复制
watch:
  options:
    nospawn: true
    livereload: true
  coffee:
    files: ['client/**/*.coffee','server/**/*/.coffee']
    tasks: ['coffee']
  copy:
    files: ['copyfiles/*']
    tasks: ['copy']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16513483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档