我正在配置我的 gulp.js gulpfile 以监视我的目录文件更改,以及 1) 重新加载我的快速服务器和 2) 发出 livereload“更改”事件,以便我的浏览器(带有 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']);
然而,当我更改其中一个 View (.html) 文件时,我的浏览器会在节点服务器进程有机会重新联机之前重新加载页面:
如何更改我的 gulpfile 以在 nodemon 准备好接收请求后发出 livereload.changed() 事件?
最佳答案
我发现了类似的问题,然后我写了我的解决方案。
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);
});
关于javascript - 在服务器进程完成重启之前 gulp-livereload 重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25222300/