2015-03-18 86 views
2

Gulp對我來說運行良好,直到保存文件,當出現錯誤時:「Task'browsersync.reload'不在您的gulpfile中」。我不確定我做錯了什麼(我遵循Gulp教程,而且源代碼相同,據我所見)。使用BrowserSync和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']); 

回答

1

解決:我從中學到的這本書是錯誤的。在http://www.browsersync.io/docs/api/的幫助下,我能夠弄清楚'browsersync.reload'不應該有任何引號。編輯:

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]); 
}); 
+0

我通常做我gulpfile.js的頂部:'變種BS =需要( '瀏覽器的同步');'然後低於'VAR重載= bs.reload;'然後你watch語句可以是:'gulp.watch('...',['task',reload]);'''''''''' – 2015-03-20 15:19:34

2

I get an error: "Task 'browsersync.reload' is not in your gulpfile"

讀取錯誤信息應導致你的這些行:

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

您試圖運行一個任務叫browsersync.reload你顯然不有。

提示:也許這不是你應該運行的任務?