0

我正在尝试使用 grunt watch 插件(https://www.npmjs.org/package/grunt-contrib-watch)来创建自定义文件监视器。我正在为咖啡脚本文件编写编译脚本,以便在更改时进行编译。这是基本配置。

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)

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

4

1 回答 1

0

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

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

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'
于 2014-05-28T21:20:08.033 回答