Gulp Livereload server not starting

╄→гoц情女王★ 提交于 2019-11-28 05:34:35

问题


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());
});


来源:https://stackoverflow.com/questions/27924121/gulp-livereload-server-not-starting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!