Livereload无法正常工作

Evg*_*nov 12 node.js livereload gulp

我使用了gulp-webapp(来自yeoman的生成器)并添加了一些其他任务(如gulp-sass和gulp-coffee).

但现在Livereload还没有开始.我需要看到这样的事情

[gulp] Live reload server listening on: 35729
Run Code Online (Sandbox Code Playgroud)

但输出看起来像

?  app git:(master) ? gulp watch
[gulp] Using gulpfile ~/Dev/lsd/app/gulpfile.js
[gulp] Starting 'clean'...
[gulp] Starting 'styles'...
[gulp] Starting 'scripts'...
[gulp] Starting 'connect'...
[gulp] Finished 'connect' after 68 ms
Started connect web server on http://localhost:9000
[gulp] Finished 'scripts' after 181 ms
[gulp] gulp-size: total 128.75 kB
[gulp] Finished 'styles' after 806 ms
[gulp] Starting 'serve'...
[gulp] Finished 'serve' after 5.73 ms
Run Code Online (Sandbox Code Playgroud)

我不明白,我的问题是什么.

我的gulpfile.coffee:

"use strict"

gulp = require("gulp")
$ = require("gulp-load-plugins")()

gulp.task "styles", ->
  gulp.src("app/styles/main.scss").pipe($.sass()).pipe($.autoprefixer("last 1 version")).pipe(gulp.dest(".tmp/styles")).pipe $.size()

gulp.task "scripts", ->
  gulp.src("app/scripts/**/*.coffee").pipe($.coffee()).pipe gulp.dest(".tmp/scripts")

gulp.task "html", [
  "styles"
  "scripts"
], ->
  jsFilter = $.filter("**/*.js")
  cssFilter = $.filter("**/*.css")
  gulp.src("app/*.html").pipe($.useref.assets()).pipe(jsFilter).pipe($.uglify()).pipe(jsFilter.restore()).pipe(cssFilter).pipe($.csso()).pipe(cssFilter.restore()).pipe($.useref.restore()).pipe($.useref()).pipe(gulp.dest("dist")).pipe $.size()

gulp.task "clean", ->
  gulp.src([
    ".tmp"
    "dist"
  ],
    read: false
  ).pipe $.clean()

gulp.task "build", [
  "html"
  "fonts"
]
gulp.task "default", ["clean"], ->
  gulp.start "build"

gulp.task "connect", ->
  connect = require("connect")
  app = connect().use(require("connect-livereload")(port: 35729)).use(connect.static("app")).use(connect.static(".tmp")).use(connect.directory("app"))
  require("http").createServer(app).listen(9000).on "listening", ->
    console.log "Started connect web server on http://localhost:9000"

gulp.task "serve", [
  "styles"
  "scripts"
  "connect"
], ->
  require("opn") "http://localhost:9000"

gulp.task "watch", [
  "clean"
  "serve"
], ->
  server = $.livereload()
  gulp.watch([
    "app/*.html"
    ".tmp/styles/**/*.css"
    ".tmp/scripts/**/*.js"
  ]).on "change", (file) ->
    server.changed file.path

  gulp.watch "app/styles/**/*.scss", ["styles"]
  gulp.watch "app/scripts/**/*.coffee", ["scripts"]
Run Code Online (Sandbox Code Playgroud)

mar*_*-gj 1

我使用 gulp 和 livereload,它们都工作得很好。
看看这里。这是我用于开发目的的服务器任务。它正在全面运作。

这个片段已经过测试

http = require "http"
path = require "path"
Promise = Promise or require("es6-promise").Promise
express = require "express"
gulp = require "gulp"
{log} = require("gulp-util")
tinylr = require "tiny-lr"
livereload = require "gulp-livereload"

ROOT = "dist"
GLOBS = [path.join(ROOT, "/**/*")]
PORT = 8000
PORT_LR = PORT + 1

app = express()
app.use require("connect-livereload") {port: PORT_LR}
app.use "/", express.static "./dist"

http.createServer(app).listen PORT, ->
  log "Started express server on http://localhost: #{PORT}"

lrUp = new Promise (resolve, reject) ->
  lrServer = tinylr()
  lrServer.listen PORT_LR, (err) ->
    return reject(err)  if err
    resolve lrServer

gulp.watch GLOBS, (evt) ->
  return if evt.type is "deleted"

  lrUp.then (lrServer) ->
    log "LR: reloading", path.relative(ROOT, evt.path)
    gulp.src(evt.path).pipe livereload(lrServer)
Run Code Online (Sandbox Code Playgroud)

请注意,我使用不同的 livereload 端口 (9001),我这样做是因为您通常希望并行运行 livereload 服务器的多个实例。仅当您使用浏览器扩展时才建议使用端口 35729,但由于您使用的是连接中间件,因此您不需要使用浏览器扩展。

希望这可以帮助