Grunt手表在grunt服务器任务期间不起作用

时间:2013-12-07 21:52:24

标签: gruntjs livereload grunt-contrib-watch

我正在使用Grunt执行执行监视任务的grunt服务器任务。不幸的是,在保存较少或咖啡文件后,应该由手表触发的“默认”任务不会被触发,因此浏览器不会进行实时重载。

我正在搜索问题,但没有找到合适的解决方案。

以下是 Gruntfile.coffee 的摘录:

grunt.registerTask "server", [
    'less'
    'coffee'
    'copy'
    "concurrent:server"
    "connect:livereload"
    "open"
    "watch"
]

grunt.registerTask "default", [
    'less'
    'coffee'
    'copy'
]

watch:
    coffee:
        files: [
            '<%= assets %>/scripts/{,*/}*.coffee'
            'Gruntfile.coffee'
            '<%= assets %>/scripts/*.coffee'
        ]

    less:
        files: [
            '<%= assets %>/styles/**/**/*.less'
            '<%= assets %>/styles/**/*.less'
            '<%= assets %>/styles/*.less'
            ]

    files: [
        'app/**/*.php'
        'app/**/*.html'
    ]

    tasks: 'default'

    livereload:
        options:
            livereload: LIVERELOAD_PORT # is 37562

        files: [
            "<%= assets %>/{,*/}*.html"
            '<%= assets %>/styles/**/*.less'
            '<%= assets %>/scripts/*.coffee'
            '<%= assets %>/styles/**/*.less'
            '<%= assets %>/styles/*.less'
        ]

提前感谢您的帮助! :)

2 个答案:

答案 0 :(得分:2)

为什么不为每个观看的文件组定义单独的任务?

watch:
  coffee:
    files: [
      'Gruntfile.coffee'
      '<%= assets %>/scripts/*.coffee'
    ]
    tasks: 'coffee'

  less:
    files: [
      '<%= assets %>/styles/**/**/*.less'
      '<%= assets %>/styles/**/*.less'
      '<%= assets %>/styles/*.less'
    ]
    tasks: 'less

这将在保存.coffee文件时编译CoffeeScript,并在保存.less文件时编译。更改less文件时重新编译所有coffee文件没有意义,反之亦然。

答案 1 :(得分:2)

您正在观看任务配置错误。需要files块观看并tasks才能运行。您的coffeeless目标都配置为监视文件但不运行任务。然后你有一个不在任何目标范围内的块。

仔细查看监视任务中的示例:https://github.com/gruntjs/grunt-contrib-watch#examples

这是(半)固定配置:

watch:
  coffee:
    files: [
      '<%= assets %>/scripts/{,*/}*.coffee'
      'Gruntfile.coffee'
      '<%= assets %>/scripts/*.coffee'
    ]
    tasks: ['coffee']

  less:
    files: [
      '<%= assets %>/styles/**/**/*.less'
      '<%= assets %>/styles/**/*.less'
      '<%= assets %>/styles/*.less'
    ]
    tasks: ['less']

   TARGETNAMENEEDEDHERE:
     files: [
       'app/**/*.php'
       'app/**/*.html'
     ]
     tasks: 'default'

   ###
   The files in this target are not configured correctly.
   Typically people create a livereload target to watch destination files.
   So the livereload will only trigger when one of the above targets writes
   to a destination file. But you're watching source files below.
   ###
   livereload:
      options:
        livereload: LIVERELOAD_PORT # is 37562
      files: [
        "<%= assets %>/{,*/}*.html"
        '<%= assets %>/styles/**/*.less'
        '<%= assets %>/scripts/*.coffee'
        '<%= assets %>/styles/**/*.less'
        '<%= assets %>/styles/*.less'
      ]