【问题标题】:gulp-livereload reloading before server process finishes restarting在服务器进程完成重新启动之前重新加载 gulp-livereload
【发布时间】:2014-10-03 01:02:18
【问题描述】:

我正在配置我的gulp.js gulpfile 来监视我的目录中的文件更改,并且 1) 重新加载我的快速服务器和 2) 发出 livereload "change" 事件,以便我的浏览器(带有 LiveReload chrome 扩展)将重新加载我正在开发的页面。

这是我的 gulpfile:

var gulp = require('gulp')
  , nodemon = require('gulp-nodemon')
  , livereload = require('gulp-livereload');

gulp.task('startReloadServer', function() {
  livereload.listen();
});

gulp.task('demon', function () {
  nodemon({
    script: 'server.js',
    ext: 'js html',
    env: {
      'NODE_ENV': 'development'
    }
  })
    .on('start', ['startReloadServer'])
    .on('restart', function () {
      console.log('restarted!');
      livereload.changed();
    });
});

gulp.task('default', ['demon']);

然而,当我对其中一个视图 (.html) 文件进行更改时,我的浏览器会在节点服务器进程有机会重新联机之前重新加载页面:

如何更改我的 gulpfile 以在 nodemon 准备好接收请求后发出 livereload.changed() 事件?

【问题讨论】:

    标签: javascript gulp livereload nodemon


    【解决方案1】:

    我发现了类似的问题,然后我写了我的解决方案。
    Gulp to watch when node app.listen() is invoked or to port (livereload, nodejs and gulp)

    nodemon({script: 'app.js',
             nodeArgs: ['--harmony'],
             stdout: false})
        .on('readable', function(data) {
            this.stdout.on('data', function(chunk) {
                if (/koa server listening/.test(chunk)) {
                    console.log('livereload');
                    livereload.reload();
                }
                process.stdout.write(chunk);
            });
            this.stderr.pipe(process.stderr);
        });
    

    【讨论】:

      【解决方案2】:

      我也遇到了这个问题。我能想到的最好办法就是将呼叫livereload.changed() 延迟一秒钟。

      nodemon.on('restart', function() {
        setTimeout(livereload.changed, 1000);
      });
      

      文档:setTimeout

      【讨论】:

        猜你喜欢
        • 2014-12-07
        • 2015-03-11
        • 2017-01-10
        • 2015-06-25
        • 1970-01-01
        • 1970-01-01
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多