简体   繁体   English

在Gulp中使用BrowserSync时发生错误

[英]Errors when using BrowserSync with Gulp

Gulp is running fine for me until saving a file, when I get an error: "Task 'browsersync.reload' is not in your gulpfile". Gulp在保存文件之前一直运行良好,直到出现错误:“任务'browsersync.reload'不在您的gulpfile中”。 I'm not sure what I'm doing wrong (I'm following a Gulp tutorial and the source is the same, as far as I can see). 我不确定自己在做什么错(据我所了解的,我正在遵循Gulp教程,并且来源是相同的)。

Gulp file: Gulp文件:

// Load Node Modules/Plugins
var gulp = require('gulp');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var jshint = require('gulp-jshint');
var imagemin = require('gulp-imagemin');
var connect = require('connect');
var serve = require('serve-static');
var browsersync = require('browser-sync');

// Styles task
gulp.task('styles', function(){
    return gulp.src('app/css/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('dist'));
});

gulp.task('scripts', function(){
    return gulp.src('app/js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
});

gulp.task('images', function(){
    return gulp.src('app/img/*')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/img'));
});

gulp.task('server', function(){
    return connect().use(serve(__dirname))
            .listen(8080)
            .on('listening', function(){
                console.log('Server Running: View at http://localhost:8080');
            });
});

gulp.task('browsersync', function(cb) {
    return browsersync({
        server: {
            baseDir:'./'
        }
    }, cb);
});

gulp.task('watch', function(){
    gulp.watch('app/css/*.css', ['styles', 'browsersync.reload']);
    gulp.watch('app/js/*.js', ['scripts', 'browsersync.reload']);
    gulp.watch('app/img/*', ['images', 'browsersync.reload']);
});

gulp.task('default', ['styles', 'scripts', 'images', 'server', 'browsersync', 'watch']);

I get an error: "Task 'browsersync.reload' is not in your gulpfile" 我收到错误消息:“任务'browsersync.reload'不在您的gulpfile中”

Reading the error message should lead you to these lines: 阅读错误消息将使您进入以下行:

gulp.watch('app/css/*.css', ['styles', 'browsersync.reload']);
gulp.watch('app/js/*.js', ['scripts', 'browsersync.reload']);
gulp.watch('app/img/*', ['images', 'browsersync.reload']);

You are trying to run a task called browsersync.reload which you obviously don't have. 您正在尝试运行一个您显然没有的名为browsersync.reload的任务。

Hint: maybe it's not a task you're supposed to run? 提示:也许这不是您应该运行的任务?

Solved: The book I'm learning this from is wrong. 解决:我正在学习的书是错误的。 With help from http://www.browsersync.io/docs/api/ I was able to figure out that 'browsersync.reload' should not have any quotes. http://www.browsersync.io/docs/api/的帮助下,我能够弄清楚'browsersync.reload'应该不带引号。 Edit: 编辑:

gulp.task('watch', function(){
    gulp.watch('app/css/*.css', ['styles', browsersync.reload]);
    gulp.watch('app/js/*.js', ['scripts', browsersync.reload]);
    gulp.watch('app/img/*', ['images', browsersync.reload]);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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