0

我将 Grunt 与执行监视任务的 grunt 服务器任务一起使用。不幸的是,在保存了一个less或coffee文件后,应该由watch taks触发的“默认”任务没有被触发,因此浏览器不会重新加载。

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

这是我的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'
        ]

提前谢谢你的帮助!:)

4

2 回答 2

2

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

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

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

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

于 2013-12-08T04:26:19.940 回答
2

您的监视任务配置不正确。files观看和tasks运行需要花费大量时间。您coffeeless目标都配置为监视文件但不运行任务。然后你有一个不在任何目标内的块。

仔细看看 watch 任务的例子: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'
      ]
于 2013-12-08T17:38:00.327 回答