使用node.js和gulp时,我无法使livereload工作

时间:2014-07-22 19:44:09

标签: javascript node.js gulp livereload

我的gulpfile如下:

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'任务运行并且控制台输出'index已更改',但connect.reload()不执行任何操作。

我检查了liverload端口和连接服务器端口,所以我知道它们都在运行。但我无法弄清楚我缺少什么代码才能让livereload工作。

我做错了什么?

1 个答案:

答案 0 :(得分: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());
});