javascript - 使用 node.js 和 gulp 时我无法让 livereload 工作

标签 javascript node.js gulp livereload

我的gulp文件如下:

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 工作。

我做错了什么?

最佳答案

您需要将管道返回到 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());
});

关于javascript - 使用 node.js 和 gulp 时我无法让 livereload 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24896443/

相关文章:

node.js - 将异步函数的输出传递给 gulp.dest?

css - 使用 nodejs gulp 在 Less 中导入 .css 文件

javascript - 子进程完成时的事件

javascript - 使用 Express.js 后端 API 处理 React 中的 session

javascript - 使用 Spring Boot 和 Zuul 进行 CORS 预检请求的 Host 与 Origin header

javascript - 套接字 IO V0.7 : How to send message to more that one specific client

node.js - 使用累加器在 while 循环内向 API 发出请求

javascript - PHP 更改按钮文本并相应更新数据库

Heroku 上的 Python 和 Node.js

javascript - 捆绑后如何处理 Javascript 依赖关系?