升级到Gulp 4后,实时重新加载无法正常工作

时间:2018-12-26 13:15:12

标签: gulp gulp-watch gulp-connect

我的gulpfile.js如下所示,

var gulp = require('gulp');
var pug = require('gulp-pug');
var sass = require('gulp-sass');
var minifyCSS = require('gulp-csso');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var connect = require('gulp-connect');

gulp.task('connect', function () {
    connect.server({
        root: 'build',
        livereload: true,
        port: 3000
    });
});

gulp.task('html', function () {
    return gulp.src('client/html/**/*.pug')
        .pipe(pug({
            pretty: true
        }))
        .pipe(gulp.dest('build'))
        .pipe(connect.reload())
});

gulp.task('css', function () {
    return gulp.src(['node_modules/bootstrap/scss/bootstrap.scss', 'client/scss/*.scss'])
        .pipe(sass({ outputStyle: 'compressed' }))
        .pipe(gulp.dest('build/css'))
        .pipe(connect.reload())
});

gulp.task('images', function () {
    return gulp.src(['client/assets/**/*.*'])
        .pipe(gulp.dest('build/assets'))
        .pipe(connect.reload())
});

gulp.task('js', function () {
    return gulp.src('client/javascript/*.js')
        .pipe(sourcemaps.init())
        .pipe(concat('app.min.js'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('build/js'))
        .pipe(connect.reload())
});

gulp.task('watch', gulp.parallel('html', 'css', 'js'));

gulp.task('default', gulp.series('connect', 'watch', 'html', 'css', 'images', 'js'));

运行此代码时,在控制台上没有出现任何错误,但是在浏览器上也没有自动重新加载内容。我对早期版本的Gulp做到了这一点,但是无法以某种方式在Gulp 4上运行。现在,每当我停止服务器时,都会出现以下错误,

[18:44:35] The following tasks did not complete: default, connect
[18:44:35] Did you forget to signal async completion?

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

尝试更改您的connect函数以显式调用done();我认为在Gulp 4中调用series()时会需要此

gulp.task('connect', function (done) {
    connect.server({
        root: 'build',
        livereload: true,
        port: 3000
    });

    done();
});

答案 1 :(得分:0)

gulp.task('html', (done) => 
   gulp.src('client/html/**/*.pug')
      .pipe(pug({
          pretty: true
      }))
      .pipe(gulp.dest('build'))
      .on('end', () => { 
         connect.reload();
         done();
      });
);