1

我使用 grunt-contrib-watch 任务(v. 0.5.3)来启用 LiveReload:

        livereload: {
            options: {
               middleware: function (connect) {
                           return [lrSnippet, mountFolder(connect, '.tmp'), 
                           mountFolder(connect, 'src'),
                           proxySnippet];
               }
            }
        }

    //...some other tasks...


        watch: {
                    livereload: {
                        options: {
                            livereload: LIVERELOAD_PORT
                        },
                        files: [
                            'src/*.html'
                        ]
                    }
               }

//...................................

grunt.registerTask('server', [
        'clean:server',
        'recess:compile',
        'configureProxies',
        'connect:livereload',
        'open',
        'watch'
    ]);

在运行grunt server --verbose(包括watch任务)时,我最终得到以下控制台输出:

Running "watch" task
    Waiting...Verifying property watch exists in config...OK
    Verifying property watch.livereload.files exists in config...OK
    Live reload server started on port: 35729
    Watching src/404.html for changes.
    Watching src/app for changes.
    Watching src/assets for changes.
    Watching src/common for changes.
    Watching src/less for changes.
    Watching src/vendor for changes.
    Watching src/index.html for changes.

例如,我们在这里看到的src/index.html是观察到的,因此我尝试更改页面标题以查看实时修改。但是,一旦我保存我的文件,进程就会退出..

我了解到watch如果提供的文件路径无效,任务可能会退出。但是,Watching src/index.html for changes断言它存在,不是吗?

我不明白。

4

2 回答 2

2

所以问题是...... NodeJs 0.10.18的一个错误。

我更新到 0.10.21 并且整个作品没有改变我的第一个 Gist 中的任何内容。

总之,如果你有 OSX 10.9 (Mavericks),你必须更新 Node 到 0.10.21

于 2013-11-18T12:41:31.633 回答
0

不确定这是否是您遇到的问题,但您似乎是直接使用 connect-livereload 中间件,而不是直接设置 livereload 选项,如grunt-contrib-connect支持的那样。

这是一个简单的 GruntFile 示例,仅使用 grunt-contrib-watch 和 grunt-contrib-connect:

module.exports = function (grunt) {
  var LIVERELOAD_PORT = 12345;

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    connect: {
      server: {
        options: {
          port: 9001,
          base: './src',
          livereload: LIVERELOAD_PORT
        }
      }
    },
    watch: {
      develop: {
        files: 'src/*.html',
        options: {
          livereload: {
            port: LIVERELOAD_PORT
          }
        }
      }
    }

  });

  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('server', [
    'connect:server',
    'watch'
  ]);
};

如果您不需要配置特定端口并且可以使用默认端口,那就更简单了——只需将两个任务的 livereload 属性更改为 true:

livereload: true
于 2013-11-18T00:56:00.933 回答