【问题标题】:I can't get livereload to work when using node.js and gulp使用 node.js 和 gulp 时我无法让 livereload 工作
【发布时间】:2014-07-22 19:44:08
【问题描述】:

我的 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”任务运行并且控制台输出“索引已更改”,但是 connect.reload() 什么都不做。

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

我做错了什么?

【问题讨论】:

    标签: javascript node.js gulp 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());
    });
    

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多