简体   繁体   中英

How to create a simple html live reload with gulp and browser-sync

I'm new to using Gulp (especially Gulp 4) and to start of I just want to have a very basic live reload on any changes in my index.html file (in the root folder). So far I have this:

var gulp = require("gulp"),
browserSync = require("browser-sync").create();

// Static server
gulp.task('browser-sync', function() {
    browserSync.init({
        server: {
            baseDir: "./"
        }
    });
});

gulp.task('default', ['html'], function() {
    // watch for HTML changes
    gulp.watch('*.html', function() {
       // run styles upon changes
       gulp.run('html');
    });
 });

How do I connect the browser-sync task with the gulp watch? I have tried using the official documentation but the examples there seem to be more complex than what I am trying to achieve.

Change to:

gulp.task('default', gulp.series('html', function() {
  // watch for HTML changes
  gulp.watch('*.html', gulp.series('html'))
}));

and see a how to migrate from gulp v3 to v4 guide since most of the code you are going to find is v3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM