【问题标题】:Gulp 4. The problem with browser-sync // The following tasks did not complete: browser-syncGulp 4. browser-sync 的问题 // 以下任务没有完成: browser-sync
【发布时间】:2019-01-07 15:20:28
【问题描述】:

不明白在浏览器同步中读取文档的错误是什么,gulp 4 实际上没有任何关于它的内容。在 gulp 4 的文档中也没有理解(异步)。如何解决这个问题。谢谢。 bla bla bla bla bla bla bla bla bla

 'default' errored after 23 ms
 The following tasks did not complete: browser-sync
 Did you forget to signal async completion?

var gulp        = require('gulp'),
      gutil         = require('gulp-util' ),
      sass          = require('gulp-sass'),
      browserSync   = require('browser-sync'),
      concat        = require('gulp-concat'),
      uglify        = require('gulp-uglify'),
      cleancss      = require('gulp-clean-css'),
      rename        = require('gulp-rename'),
      autoprefixer  = require('gulp-autoprefixer'),
      notify        = require('gulp-notify');

gulp.task('browser-sync', function() {
    browserSync({
        server: {
            baseDir: 'app'
        },
        notify: false,
        // open: false,
        // online: false, // Work Offline Without Internet Connection
        // tunnel: true, tunnel: "projectname", // Demonstration page: http://projectname.localtunnel.me
    })
});

gulp.task('styles', function() {
    return gulp.src('app/'+syntax+'/**/*.'+syntax+'')
    .pipe(sass({ outputStyle: 'expanded' }).on("error", notify.onError()))
    .pipe(rename({ suffix: '.min', prefix : '' }))
    .pipe(autoprefixer(['last 15 versions']))
    .pipe(cleancss( {level: { 1: { specialComments: 0 } } }))
    .pipe(gulp.dest('app/css'))
    .pipe(browserSync.stream())
});

gulp.task('scripts', function() {
    return gulp.src([
        'app/libs/jquery/dist/jquery.min.js',
        'app/js/common.js',
        ])
    .pipe(concat('scripts.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest('app/js'))
    .pipe(browserSync.reload({ stream: true }))
});

gulp.task('code', function() {
    return gulp.src('app/*.html')
    .pipe(browserSync.reload({ stream: true }))
});


    gulp.task('watch', function() {
        gulp.watch('app/'+syntax+'/**/*.'+syntax+'', gulp.parallel('styles'));
        gulp.watch(['libs/**/*.js', 'app/js/common.js'], gulp.parallel('scripts'));
        gulp.watch('app/*.html', gulp.parallel('code'))
    });
    gulp.task('default', gulp.parallel('watch', 'browser-sync'));

【问题讨论】:

标签: javascript html css gulp


【解决方案1】:

改用这个:

gulp.task('browser-sync', function(done) {
    browserSync({
        server: {
            baseDir: 'app'
        },
        notify: false,
        // open: false,
        // online: false, // Work Offline Without Internet Connection
        // tunnel: true, tunnel: "projectname", // Demonstration page: http://projectname.localtunnel.me
    });
    done();
});

注意done's - 这是一种指示浏览器同步设置任务完成的简单方法。见callback to signal async completion


对于@Dirk 关于不使用.create() 的评论,请参阅

发布 2.0.0 语法(推荐)

虽然上述内容仍受支持 [ed.没有 create()],我们现在推荐以下 反而。调用 .create() 意味着您获得了唯一的参考并允许 您可以创建多个服务器或代理。

来自api documentation

因此,使用 create 允许打开 browserSync 的多个实例 - 提供不同的文件或查看/重新加载不同的文件 - 但在更简单的情况下不是必需的。

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2017-01-28
    • 2020-10-17
    • 1970-01-01
    相关资源
    最近更新 更多