如何根据需要使用grunt-contrib-watch和grunt-contrib-coffee来编译CoffeeScript?

Eri*_*Red 10 coffeescript gruntjs grunt-contrib-watch grunt-contrib-coffee

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

这是我的Gruntfile:

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']
Run Code Online (Sandbox Code Playgroud)

coffeelint任务仅在更改的文件上成功运行.

咖啡编译不会产生任何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...
Run Code Online (Sandbox Code Playgroud)

这有什么不对?任何帮助将不胜感激!

更新:

这是一个有效的例子:

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']
Run Code Online (Sandbox Code Playgroud)

agc*_*nti 10

当源比其编译输出更新时,您可以使用grunt-newer仅编译文件.

安装它:

npm install grunt-newer --save-dev
Run Code Online (Sandbox Code Playgroud)

然后稍微改变你的coffeescript任务.如果您转到Grunt.js文档中的 "动态构建文件对象"部分,您将看到需要将与编译输出的文件位置和位置相关的信息保存在files数组中,以便正确执行任务.

附加选项,例如bare: true,可以随后用选项指定的对象.

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

   coffee:
      all:
        files: [{
          expand: true
          cwd: 'src/coffeescript/'
          src: '**/*.coffee'
          dest: 'public/js/compiled'
          ext: '.js'
        }]
        options:
          bare: true
          spawn: false
Run Code Online (Sandbox Code Playgroud)

然后在你的监视任务中使用更新,如下所示:

    watch:
      coffeescript:
        files: ['src/**/*.coffee']
        tasks: ['newer:coffeelint:all', 'newer:coffee:all']
        options:
          spawn: false
Run Code Online (Sandbox Code Playgroud)

然后,较新的只会编译比其编译版本更新的文件.


kha*_*iuk 4

尝试将这样的内容添加到您的 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
Run Code Online (Sandbox Code Playgroud)

阅读文档中的更多内容