【问题标题】:Monitoring sub-task for grunt-contrib-watch监控 grunt-contrib-watch 的子任务
【发布时间】:2013-05-06 23:32:51
【问题描述】:

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

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

但这似乎不起作用。想法?

【问题讨论】:

    标签: javascript build coffeescript gruntjs


    【解决方案1】:

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

    基本上,我匹配传入文件的路径
    如果它的 .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")
    

    【讨论】:

      【解决方案2】:

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

      watch 事件并非旨在替换 Grunt API。请改用tasks

      watch:
        options:
          nospawn: true
          livereload: true
        coffee:
          files: ['client/**/*.coffee','server/**/*/.coffee']
          tasks: ['coffee']
        copy:
          files: ['copyfiles/*']
          tasks: ['copy']
      

      【讨论】:

      • 我只想要更改的文件...监视任务运行编译这些文件夹中的所有文件
      • 然后在watch事件中继续使用grunt.config.set()。只是不要在watch 事件中使用grunt.task.run()。这就是tasks 的用途。
      • 我想在过滤掉更改的文件后运行任务...那我该怎么做呢?
      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2023-03-12
      • 2014-04-16
      • 1970-01-01
      相关资源
      最近更新 更多