2014-07-22 45 views
1

我gulpfile如下:我不能讓livereload如何使用Node.js和一口何時工作

var gulp = require('gulp'), 
    connect = require('gulp-connect'), 
    sass = require('gulp-ruby-sass'), 
    autoprefixer = require('gulp-autoprefixer'), 
    minifycss = require('gulp-minify-css'), 
    jshint = require('gulp-jshint'), 
    uglify = require('gulp-uglify'), 
    imagemin = require('gulp-imagemin'), 
    rename = require('gulp-rename'), 
    clean = require('gulp-clean'), 
    concat = require('gulp-concat'), 
    notify = require('gulp-notify'), 
    cache = require('gulp-cache'), 
    livereload = require('gulp-livereload'); 

gulp.task('styles', function() { 
    return gulp.src('main.scss') 

    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.$ 
    .pipe(gulp.dest('css')) 
    .pipe(rename({suffix: '.min'})) 
    .pipe(minifycss()) 
    .pipe(gulp.dest('css')); 
}); 

    gulp.task('default', function() { 
    gulp.start('styles'); 
    gulp.start('server'); 
    gulp.start('watch'); 
}); 


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


gulp.task('indexChange',function(){ 
    console.log('index has changed'); 
    connect.reload(); 
}); 

gulp.task('watch', function() { 

    gulp.watch('index.html', ['indexChange']); 


}); 

當我改變我的index.html文件的「indexChange」任務運行和控制檯輸出「索引已經改變',但是connect.reload()什麼也不做。

我檢查了肝載入端口和連接服務器端口,所以我知道兩者都在運行。但我無法弄清楚我錯過了哪些代碼來讓livereload工作。

我在做什麼錯?

回答

1

您需要將管道返回到connect.reload();爲它的工作, 試試這個:

// add gulp-watch 
var watch = require('gulp-watch'); 

gulp.task('indexChange',function(){ 
    console.log('index has changed'); 
}); 

gulp.task('watch', function() { 
    watch({glob: './index.html'}, ['indexChange']).pipe(connect.reload()); 
}); 
+0

謝謝!這工作! – user1577173