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

标签 coffeescript gruntjs grunt-contrib-watch grunt-contrib-coffee

我只想在我保存的单个文件上运行coffee lint和coffee compile。我的项目中有成百上千个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']

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

即使grunt表示运行,coffee编译也不产生任何JS文件。

这是保存单个咖啡文件后的输出:
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']

最佳答案

尝试在您的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

documentation中阅读更多内容

关于coffeescript - 如何仅在需要时使用grunt-contrib-watch和grunt-contrib-coffee编译CoffeeScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21502324/

相关文章:

javascript - React 未捕获错误 : Invariant Violation: _registerComponent(. ..):目标容器不是 DOM 元素

java - Android 项目的自动样式检查

javascript - Gulp 获取目录的 ls

angularjs - 如何使用 grunt 服务 angular 应用程序?

javascript - LoDash - 按数组属性分组而不将值作为一个键

ruby-on-rails - rails : access controller instance variable in CoffeeScript or JavaScript asset file

gruntjs - grunt-contrib-jshint 不会提示 console.log

gruntjs - Gruntfile.js 中的智能感知

javascript - Grunt imagemin - 观看多个文件/文件夹优化单个文件?

wordpress - Grunt watch livereload over MAMP Pro 虚拟主机和 ssl 返回错误 net::ERR_CONNECTION_CLOSED