我正在尝试使用 livereload 让 gulp 工作。Gulp 无法控制 Web 服务器本身(实际的 Web 应用程序是一个 php 站点。当我在服务器上运行 nmap 时,我没有看到 livereload 工作,并且 chrome 扩展名指示同样的事情。
这是我的吞咽任务:
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)
这是一个基于connect
服务器和connect-livereload
和gulp-livereload
插件的简单且经过测试的livereload解决方案:
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
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/"
}
});
});
这是一个使用 sass 编译的简单使用示例:
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());
});