Search code examples
node.jssassgulpbrowser-sync

Gulp 4 - Tasks not finished - Gulpfile.js Browsersync


I'm a bit stuck with changing my old Gulp 3 Gulpfile to work in Gulp 4.

I have got to a point where it all seems to work except that the default and serve tasks are not marked as finished in powershell.

How can I unblock the code and allow them to finish correctly please?

var gulp        = require('gulp');
var browserSync = require('browser-sync').create();
var sass        = require('gulp-sass');

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
    return gulp.src(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'])
        .pipe(sass())
        .pipe(gulp.dest("src/css"))
        .pipe(browserSync.stream());
});

// Move the javascript files into our /src/js folder
gulp.task('js', function() {
    return gulp.src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 'node_modules/jquery/dist/jquery.min.js', 'node_modules/popper.js/dist/umd/popper.min.js'])
        .pipe(gulp.dest("src/js"))
        .pipe(browserSync.stream());
});

// Static Server + watching scss/html files
gulp.task('serve', gulp.series('sass', function() {

    browserSync.init({
        server: "./src"  
    });

    gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'], gulp.series('sass'));
    gulp.watch("src/*.html").on('change', browserSync.reload);
}));

gulp.task('default', gulp.parallel('js', 'serve'));


Solution

  • The second task in your serve series doesn't return anything, gulp doesn't understand that. A task can return a stream or a promise - or it must call the callback parameter to signal completion, like so:

    gulp.task('serve', gulp.series('sass', function(cb) {
        // Your js task code here
        cb()
    }))
    

    Read the latest gulp4 documentation, it'll help you.