Gruntjs 使用grunt为coffeescript创建文件监视程序

Gruntjs 使用grunt为coffeescript创建文件监视程序,gruntjs,grunt-contrib-watch,grunt-contrib-coffee,Gruntjs,Grunt Contrib Watch,Grunt Contrib Coffee,我正在尝试使用grunt watch插件()来创建自定义的FileWatcher。我正在为coffeescript文件编写编译脚本,这些文件在更改时将被编译。这是基本配置 grunt.initConfig( pkg: grunt.file.readJSON 'package.json' watch: cofee_files: files: ['client/**/*.coffee'], tasks: ['start'],

我正在尝试使用grunt watch插件()来创建自定义的FileWatcher。我正在为coffeescript文件编写编译脚本,这些文件在更改时将被编译。这是基本配置

grunt.initConfig(
    pkg: grunt.file.readJSON 'package.json'
    watch:
      cofee_files:
        files: ['client/**/*.coffee'],
        tasks: ['start'],
        options:
          spawn: false,

grunt.registerTask( 'start', 'starting coffee compilation', (filepath)->
    console.log(filepath)

我需要获取文件路径作为输入,以便能够对文件执行编译,并将输出保存在相对于源coffeescript文件的文件路径的目录中。在我上面写的代码中,传入的filepath值未定义-我可以在日志输出中看到。请帮助我获取已修改文件的文件路径,以便我可以相应地动态配置coffeescript编译器。

您需要为监视事件注册一个处理程序。在那里,您将获得可用于配置咖啡任务的文件路径:

(代码未经测试,但我想你已经明白了)


很抱歉我之前有个假阳性。代码创建错误:
致命错误:无法调用未定义的方法“push”
。我还尝试过编译为
grunt.config.get('coffee:compile')
,但这也不起作用。你能提供语法上准确的代码吗?“watch”上grunt.event.on的识别错误,我更正了它。此外,董事会也可以帮助你理解事物,但不能帮助你完成工作!一定是你自己做的某种转移。。。
path = require 'path'

grunt.initConfig(
  pkg: grunt.file.readJSON 'package.json'

  watch:
    cofee_files:
      files: ['client/**/*.coffee'],
      tasks: ['start'],
      options:
        spawn: false,
  coffee:
    compile: 
      files: []


  grunt.event.on 'watch', (action, filepath) ->
    # modify your coffee task here
    newCoffeeConfig = 
      cwd: path.dirname(filepath)
      src: path.basename(filepath)
      dest: path.dirname(filepath)
      ext. '.js' 

    grunt.config.get('coffee:compile.files').push newCoffeeConfig
    grunt.task.run 'coffee:compile'