Gulp Livereload server not starting

2019-01-27 00:50发布

问题:

I am attempting to get gulp working with livereload. Gulp is not in control of the webserver itself (the actual web app is a php site. When I run nmap on the server I don't see livereload working, and the chrome extension indicates the same thing.

Here is my gulp task:

gulp = require 'gulp'
{livereload} = require('gulp-load-plugins')()

gulp.task 'watch', ['styles'], ->
    livereload.listen()

    gulp.watch './public/include/less/**/*.less', ['styles']
    gulp.watch('./public/include/css/**/*.css').on('change', livereload.changed)

回答1:

Here is a simple and tested livereload solution based on connect server and connect-livereload and gulp-livereload plugins:


var gulp = require('gulp');
var connect = require('connect');
var connectLivereload = require('connect-livereload');
var opn = require('opn');
var gulpLivereload = require('gulp-livereload');

var config = {
    rootDir: __dirname,
    servingPort: 8080,

    // the files you want to watch for changes for live reload
    filesToWatch: ['*.{html,css,js}', '!Gulpfile.js']
}

// The default task - called when you run `gulp` from CLI
gulp.task('default', ['watch', 'serve']);

gulp.task('watch', ['connect'], function () {
  gulpLivereload.listen();
  gulp.watch(config.filesToWatch, function(file) {
    gulp.src(file.path)
      .pipe(gulpLivereload());
  });
});

gulp.task('serve', ['connect'], function () {
  return opn('http://localhost:' + config.servingPort);
});

gulp.task('connect', function(){
  return connect()
    .use(connectLivereload())
    .use(connect.static(config.rootDir))
    .listen(config.servingPort);
});


EDIT.

I missed the PHP part. I haven't worked with it but this might help you: https://github.com/micahblu/gulp-connect-php



回答2:

Why you not use browser-sync ? is quite simple and less complicated.

He is automatic open browser window with default port 3000.

Here is simple example of use:

var gulp = require(''gulp),
    browserSync = require('browser-sync').create();


// Static server
gulp.task('browser-sync', function() {
    browserSync.init({
        server: {
            baseDir: "./flex_grid/"
        }
    });
});

Here is a simple use example with sass compile:

var gulp         = require('gulp'),
    sass         = require('gulp-ruby-sass'),
    watch        = require('gulp-watch'),
    sourcemaps   = require('gulp-sourcemaps'),
    autoprefixer = require('gulp-autoprefixer');
    var browserSync = require('browser-sync').create();

gulp.task('default', ['serve']);


gulp.task('serve', ['sass'], function() {

    browserSync.init({
        server: "./flex_grid/"
    });

    gulp.watch("src/sass/*.scss", ['sass']);
    gulp.watch("flex_grid/*.html").on('change', browserSync.reload);
});


gulp.task('sass', function() {
    return sass(
       'src/sass/flex_grid.scss', {
        style: 'expanded',
        sourcemap: true
    }).on('error', function (err) {
        console.error('Error! ', err.message);
    })
    .pipe(autoprefixer({
        browsers: [ '> 0%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1' ],
        cascade: false
    }))
    .on('error', function (err) {
        console.error('Error! ', err.message);
    })
    .pipe(sourcemaps.write('.', {
        includeContent: false
    }))
    .on('error', function (err) {
        console.error('Error! ', err.message);
    })
    .pipe(gulp.dest('flex_grid'))
    .pipe(browserSync.stream());
});