【问题标题】:Gulp.js: Reload altered php/html pagesGulp.js:重新加载更改的 php/html 页面
【发布时间】:2014-02-15 16:17:46
【问题描述】:

我在我的 wordpress 环境中运行了一个基本的 gulp.js 设置。除了我重新加载更改的 .php 文件的方式之外,一切都按我的意愿进行。我是这样设置的:

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

有没有办法让它只实时重新加载保存的页面?这似乎是我唯一无法使用 gulp 进行工作的事情;不过,我喜欢它的运行速度比 grunt 快得多。

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

    });
});

【问题讨论】:

  • 我已经在上面发布了我的监视任务,因为整个文件似乎要发布的内容很多,但如果您愿意,我可以找到一种方法来发布更多内容!
  • 你能把它发布在 jsfiddle 或 plunkr 上吗?让 livereload 与 PHP 一起工作对我来说将是一个巨大的帮助。
  • 给你。如果您有任何问题,请告诉我。 codepen.io/thesublimeobject/pen/uzeJd

标签: javascript wordpress livereload gulp


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    参考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);
        });
    });
    

    【讨论】:

    • 对上述代码的小修正:它是changed(file.path) 而不是change(file.path)
    • 它是:livereload.changed(file.path);不是:livereload().changed(file.path);
    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 2020-04-01
    • 2015-03-29
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多