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

标签 javascript build coffeescript gruntjs

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

# 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')

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

我想到了

# Watch copy task
grunt.event.on 'watch:copy', (action,filepath) -> ...
# Watch coffee task
grunt.event.on 'watch:coffee', (action,filepath) -> ...

但这似乎行不通。想法?

最佳答案

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

基本上,我匹配传入文件的路径
如果它的 .coffee 运行咖啡编译任务
如果它的 .* 运行复制任务

# 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")

关于javascript - 监控 grunt-contrib-watch 的子任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16513483/

相关文章:

javascript - 从缩小文件中提取单个 javascript 文件代码

javascript - 为什么 sessionStorage 跨多个选项卡保留?

javascript - 如何在 coffeescript(或 javascript)中等待回调?

javascript - Gulp:同一任务中的不同 Pipe 集合(CoffeeScript 和 JavaScript)

javascript - 捆绑 JavaScript 类(class) Uncaught SyntaxError : Unexpected token <

javascript - JQGrid 减少窗口调整大小事件中的列数

iphone - Apple Mach-O Linker错误Xcode

java - Gradle 构建失败并出现 Javadoc NullPointerException

java |图书馆内部图书馆包装

javascript - Coffeescript:Splat Sum 函数