3

I have a basic gulp.js setup running in my wordpress environment. Everything works how I would like except for the way I am reloading my changed .php files. I have it set up like this:

gulp.task('php', function(){
    gulp.src('../*.php').pipe(livereload(server));
});

    gulp.task('watch', function() {
        server.listen(35729, function (err) {
            if (err) {
                return console.log(err)
            };
            gulp.watch('styl/src/*.scss', ['styl']);
            gulp.watch('js/src/*.js', ['js']);
            gulp.watch('../*.php', ['php']);
        });
    });

I am essentially just having it reload the page whenever any php file is saved, but whenever I save one, it will automatically refresh every php page, whether I have it open, whether it was saved, or anything else:

[gulp] footer.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/footer.php ...
[gulp] front-page.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/front-page.php ...
[gulp] functions.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/functions.php ...
[gulp] header.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/header.php ...
[gulp] index.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/index.php ...
[gulp] social-media.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/social-media.php ...
[gulp] template-contact.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/template-contact.php ...
[gulp] template-interior.php was reloaded.
... Reload /Users/KBD/sites/vi/cnt/themes/vi/template-interior.php ...

Is there a way I can have it only livereload the page that was saved? This seems to be the only thing I can't get to work how I want with gulp; I am loving how much faster it runs than grunt, though.

EDIT: @SirTophamHatt

gulp.task('watch', function() {
    server.listen(35729, function (err) {
        if (err) {
            return console.log(err)
        };
        gulp.watch('styl/src/*.scss', ['styl']);
        gulp.watch('js/src/*.js', ['js']);
        gulp.watch('js/src/*.coffee', ['coffee']);
        gulp.src('../*.php').pipe(watch()).pipe(livereload(server));

    });
});
4

2 回答 2

4

请改用gulp-watch插件。观看单个文件并仅将其传递给您的结果要好得多。另外,如果您使用而不是.watch({glob: ['files']}).pipe(...)gulp.src(['files']).pipe(watch()).pipe(...)

另一种方法是使用gulp-neweror gulp-changed,它们都使用时间戳来确定文件是否已更改。我也没有亲自使用过,但gulp-newer似乎有更多的功能。但是,两者都无法检测到新文件。

于 2014-02-15T16:39:46.953 回答
0

参考这里,你可以简单地watch

gulp.watch('../*.php').on('change', function(file) {
    livereload(server).changed(file.path);
});

此外,目前gulp-livereload支持自动创建tiny-lr服务器实例。因此,您不需要tiny-lr明确要求:

livereload = require('gulp-livereload');

gulp.task('task foo', function() {
    gulp.src('foo').pipe(...).pipe(livereload());
});

gulp.task('watch', function() {
    gulp.watch('foo', ['task foo'])
    gulp.watch('bar').on('change', function(file) {
        livereload().changed(file.path);
    });
});
于 2014-03-28T10:09:43.250 回答