2014-02-15 90 views
3

我有一个基本的gulp.js设置在我的wordpress环境中运行。除了我重新加载更改后的.php文件的方式外,一切都可以运行。我有它设置如下:Gulp.js:刷新改变的php/html页面

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']); 
     }); 
    }); 

我基本上只是让它重新载入,只要任何PHP文件被保存的页面,但每当我救一个,它会自动刷新每一个PHP页面,我是否有它打开,无论是保存还是其他:

[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 ... 

有没有一种方法,我可以只有livereload保存的页面?这似乎是我无法工作的唯一方式,我想要如何喝一口水;不过,我喜欢它跑得比咕噜快多少。

编辑:@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)); 

    }); 
}); 
+0

我希望看到你的完整配置,一饮而尽,因为我试图用一口运行PHP的服务器和livereload当我改变PHP/PHTML,CSS和js文件。 – SirTophamHatt

+0

我上面发布了我的观看任务,因为整个文件看起来好像很多帖子,但是如果您愿意,我可以找到一种发布更多的方法! – thesublimeobject

+0

你可以把它张贴在jsfiddle或plunkr上吗?这对我得到livereload使用PHP来说是一个巨大的帮助。 – SirTophamHatt

回答

4

使用gulp-watch plugin代替。观看单个文件要好得多,只有将这些文件传递给你的结果。另外,如果您使用watch({glob: ['files']}).pipe(...)而不是gulp.src(['files']).pipe(watch()).pipe(...),它还可以查看新文件以及文件。

另一种方法是使用gulp-newergulp-changed,它们都使用时间戳来确定文件是否已更改。我还没有亲自使用,但gulp-newer似乎有一点功能。但是,它们都不能检测到新文件。

+0

这正是我想要的,谢谢。 – thesublimeobject

0

参考文献here,你可以简单地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); 
    }); 
}); 
+0

对以上代码的小修改:它被改变(file.path)'不改变(file.path)' –

+0

@IvanV。是的,谢谢=) – okm

+0

这是:livereload.changed(file.path); NOT:livereload()。changed(file.path); –