如何使用grunt contrib watch和grunt contrib coffee仅在需要时编译CoffeeScript?

如何使用grunt contrib watch和grunt contrib coffee仅在需要时编译CoffeeScript?,coffeescript,gruntjs,grunt-contrib-watch,grunt-contrib-coffee,Coffeescript,Gruntjs,Grunt Contrib Watch,Grunt Contrib Coffee,我只想在保存的单个文件上运行coffee lint和coffee compile。我的项目中有数百个CoffeeScript文件,编译所有这些文件需要花费太多时间 这是我的Grunfile文件: module.exports = (grunt) -> grunt.initConfig pkg: grunt.file.readJSON 'package.json' coffee: all: expand: true bar

我只想在保存的单个文件上运行coffee lint和coffee compile。我的项目中有数百个CoffeeScript文件,编译所有这些文件需要花费太多时间

这是我的Grunfile文件:

module.exports = (grunt) ->

  grunt.initConfig

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

    coffee:
      all:
        expand: true
        bare: true
        cwd: 'src/coffeescript/'
        src: '**/*.coffee'
        dest: 'public/js/compiled'
        ext: '.js'

    coffeelint:
      all: ['src/coffeescript/**/*.coffee']

    watch:
      coffeescript:
        files: ['src/**/*.coffee']
        tasks: ['coffeelint', 'coffee']
        options:
          spawn: false

  grunt.event.on 'watch', (action, filepath) ->
    grunt.config(['coffeelint', 'all'], filepath)
    grunt.config(['coffee', 'all'], filepath)

  grunt.loadNpmTasks 'grunt-coffeelint'
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-watch'

  grunt.registerTask 'default', ['coffeelint', 'coffee', 'watch']
coffeelint任务仅在更改的文件上成功运行

coffee编译不会生成任何JS文件,即使grunt说它正在运行

以下是保存单个咖啡文件后的输出:

OK
>> File "src/coffeescript/app.coffee" changed.


Running "coffeelint:all" (coffeelint) task
>> 1 file lint free.

Running "coffee:all" (coffee) task

Running "watch" task
Completed in 0.009s at Sat Feb 01 2014 13:10:07 GMT-0600 (CST) - Waiting...
这里怎么了?任何帮助都将不胜感激

更新:

下面是一个工作示例:

module.exports = (grunt) ->

  fs = require 'fs'
  isModified = (filepath) ->
    now = new Date()
    modified =  fs.statSync(filepath).mtime
    return (now - modified) < 10000

  grunt.initConfig

    coffee:
      options:
        sourceMap: true
        bare: true
        force: true # needs to be added to the plugin
      all:
        expand: true
        cwd: 'src/coffeescript/'
        src: '**/*.coffee'
        dest: 'public/js/compiled'
        ext: '.js'
      modified:
        expand: true
        cwd: 'src/coffeescript/'
        src: '**/*.coffee'
        dest: 'public/js/compiled'
        ext: '.js'
        filter: isModified

    coffeelint:
      options:
        force: true
      all:
        expand: true
        cwd: 'src/coffeescript/'
        src: '**/*.coffee'
      modified:
        expand: true
        cwd: 'src/coffeescript/'
        src: '**/*.coffee'
        filter: isModified

    watch:
      coffeescript:
        files: ['src/**/*.coffee']
        tasks: ['coffeelint:modified', 'coffee:modified']

  grunt.loadNpmTasks 'grunt-coffeelint'
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-watch'

  grunt.registerTask 'default', ['coffeelint:all', 'coffee:all', 'watch']
module.exports=(咕噜)->
fs=需要“fs”
isModified=(文件路径)->
现在=新日期()
modified=fs.statSync(filepath).mtime
返回值(现在修改)<10000
grunt.initConfig
咖啡:
选项:
sourceMap:true
是的
force:true#需要添加到插件中
全部:
扩展:正确
cwd:'src/coffeescript/'
src:“***咖啡”
dest:'public/js/compiled'
分机:'.js'
被改进的:
扩展:正确
cwd:'src/coffeescript/'
src:“***咖啡”
dest:'public/js/compiled'
分机:'.js'
过滤器:已修改
咖啡粉:
选项:
原力:对
全部:
扩展:正确
cwd:'src/coffeescript/'
src:“***咖啡”
被改进的:
扩展:正确
cwd:'src/coffeescript/'
src:“***咖啡”
过滤器:已修改
观察:
咖啡脚本:
文件:['src/***/*.coffee']
任务:['coffeelint:modified','coffeelint:modified']
grunt.loadNpmTasks'grunt coffeelint'
grunt.loadNpmTasks“grunt contrib咖啡”
grunt.loadNpmTasks'grunt contrib watch'
grunt.registerTask'default'、['coffeelint:all'、'coffeelint:all'、'Coffeet:all'、'watch']
仅当源代码比编译后的输出更新时,才可以使用编译文件

安装时请使用:

npm install grunt-newer --save-dev
然后稍微改变一下你的咖啡脚本任务。如果转到中的“动态构建文件对象”部分,您将看到需要在
文件
数组中保存与文件位置和编译输出位置相关的信息,以便任务正确执行

然后可以使用options对象指定附加选项,如
bare:true

因此,对于您的
coffee
任务,请执行以下操作:

   coffee:
      all:
        files: [{
          expand: true
          cwd: 'src/coffeescript/'
          src: '**/*.coffee'
          dest: 'public/js/compiled'
          ext: '.js'
        }]
        options:
          bare: true
          spawn: false
然后在您的监视任务中使用更新的,如下所示:

    watch:
      coffeescript:
        files: ['src/**/*.coffee']
        tasks: ['newer:coffeelint:all', 'newer:coffee:all']
        options:
          spawn: false

较新版本将只编译比其编译版本更新的文件

尝试在c任务中添加类似的内容

 coffee:
  all:
    filter: (filepath) ->
        fs = require('fs')
        now = new Date()
        modified =  fs.statSync(filepath).mtime
        return (now - modified) < 10000 # or another difference in millyseconds
咖啡:
全部:
过滤器:(文件路径)->
fs=需要('fs')
现在=新日期()
modified=fs.statSync(filepath).mtime
返回值(现在已修改)<10000#或以百万秒为单位的另一个差值

阅读更多信息,这里有两个问题

第一: Grunt将更改的文件路径生成为
src/coffeescript/some_file.coffee
,但在
coffee
任务中,您将
cwd
定义为
src/coffeescript

所以coffee任务实际上只需要
some_file.coffee
,但它得到
src/coffeescript/some_file.coffee
,然后将其扩展到
src/coffeescript/src/coffeescript/some_file.coffee
O

您的
coffeelint
任务没有任何
cwd
,因此它以完整的文件路径成功运行

相应地调整文件路径,或从coffee配置中删除
cwd

grunt.config(['coffee', 'all'], filepath.replace("src/coffeescript", ""))
第二: 这是次要的,可能不需要,但也要注意这一点

grunt.event.on 'watch', (action, filepath) ->
  grunt.config(['coffeelint', 'all'], filepath)
  grunt.config(['coffee', 'all'], filepath)
  ^^^^^^^ You might have to change above line to:
  grunt.config(['coffee', 'all', 'src'], filepath)
在coffeelint任务中,源直接设置在
all
下。但是在您的
coffee
任务中,源代码设置在
all->src
下。反映同样的情况也可能有用


编辑:次要附录

您可以将每次修改时间存储在地图中,而不是使用任意的持续时间阈值:

fs = require 'fs'
mtimes = {}
isModified = (filepath) ->
    mtime = fs.statSync(filepath).mtime
    if mtimes[filepath]?
        return mtimes[filepath] < mtime
    else
        mtimes[filepath] = mtime
        return yes
fs=需要“fs”
mtimes={}
isModified=(文件路径)->
mtime=fs.statSync(文件路径).mtime
如果mtimes[文件路径]?
返回mtimes[filepath]

尽管grunt没有提供目标文件路径作为filter函数的第二个参数,但这太糟糕了。这将启用一个简单的检查,以查看生成的文件是否存在并且是否比源文件旧…是否开箱即用…

这只编译已更改的咖啡文件,但它在所有咖啡文件上运行coffee lint:-(是的。我有相同的问题。首先:>>文件“src/coffeescript/app.coffee”已更改。正在运行“coffeelint:files”(coffeelint)任务>>1文件无绒线。正在运行“coffee:files”(coffee)任务警告:无法读取“未定义”文件(错误代码:enoint)。第二:未创建JavaScript文件。