0

我正在设置一个 Gruntfile,我试图在其中:

  1. 为客户端编译一些coffeescript到javascript。
  2. 注意要为客户端编译为 javascript 的咖啡脚本的更改。
  3. 注意后端(服务器)coffeescript 文件的更改,并在发现更改时重新启动咖啡应用程序。

我已经使用这个来完成前两个步骤:

module.exports = (grunt) ->
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'
    coffee:
      compile:
        expand: true
        flatten: true
        cwd: 'public/src'
        src: ['*.coffee']
        dest: 'public/dist'
        ext: '.js'
    watch:
      coffee:
        files: ['public/src/*.coffee']
        tasks: ['coffee']

  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.registerTask 'default', ['coffee', 'watch']

但我不确定如何进行第三步。

目前的目录结构如下所示:

app
  lib.coffee
  routes.coffee
public/
  dist/
    client.js
  src/
    client.coffee
Gruntfile.coffee
package.json
server.coffee

我将如何观察应用程序目录或 server.coffee 文件中任何内容的更改并使用 grunt 自动启动服务器(例如“coffee server.coffee”)?

服务器也使用 express - 重新启动应用程序是否需要在启动之前查看端口是否再次可用?

4

1 回答 1

1

最终设法让这个工作:

module.exports = (grunt) ->
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'
    coffee:
      compile:
        expand: true
        flatten: true
        cwd: 'public/src'
        src: ['*.coffee']
        dest: 'public/dist'
        ext: '.js'
    watch:
      coffee:
        files: ['public/src/*.coffee']
        tasks: ['coffee']
      express:
        files: ['server.coffee']
        tasks: ['express:dev']
        options:
          spawn: false
    express:
      dev:
        options:
          script: 'server.coffee'
          opts: ['/path/to/coffee']
          #port: 8080

  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-express-server'
  grunt.registerTask 'default', ['coffee', 'express:dev', 'watch']
于 2014-07-03T14:02:50.337 回答