简体   繁体   English

Gulp浏览器同步未重新加载

[英]Gulp browser-sync not reloading

This is my gulpfile.js file. 这是我的gulpfile.js文件。 It doesn't reload when I change and save my css or html, but it shows the "Connected to Browser-sync" message when I use gulp serve. 当我更改并保存CSS或HTML时,它不会重新加载,但是当我使用gulp服务时,它会显示“已连接到浏览器同步”消息。 I can't seem to figure out what is wrong. 我似乎无法找出问题所在。

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



gulp.task('styles', function() {
    gulp.src('./css/*.css')
        .pipe(gulp.dest('./css'))
});

//Watch task
gulp.task('default',function() {
    gulp.watch('./css/*.css',['styles']);
}); 

gulp.task('js', function () {
    return gulp.src('./js/*.js')
        // .pipe(concat('all.js'))
        .pipe(gulp.dest('./js'));
});

gulp.task('html', function () {
    return gulp.src('./*.html')
        .pipe(gulp.dest('./'));
});

gulp.task('js-watch', ['js'], browserSync.reload);

gulp.task('html-watch', ['html'], browserSync.reload);

gulp.task('css-watch', ['css'], browserSync.reload);

gulp.task('serve', ['js', 'html', 'styles'], function () {

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

    gulp.watch("./*.js", ['js-watch']);
    gulp.watch("./*.html", ['html-watch']);
    gulp.watch("./css/*.css", ['css-watch']);
});

Try this. 尝试这个。 All i did was add a new dependency.. run-sequence , to make it easy to organize how things are run. 我所做的只是添加了一个新的dependency .. run-sequence ,以使其易于组织运行方式。 All you need is to run gulp to start the script and everything should run perfectly. 您所需gulp就是运行gulp启动脚本,一切都应该完美运行。

Aside from that i created a site folder at the root of my project. 除此之外,我在项目的根目录下创建了一个site文件夹。 just because it seems more organize that way, and if you ever think of using jade, or sass, scss, etc. in your project and all you have to do is change the src path and keep the rendered output in the site folder.. Aside from that, everything is the same. 只是因为它看起来更加井井有条,而且,如果您曾经考虑在项目中使用jade或sass,scss等,您要做的就是更改src path并将渲染的输出保留在site文件夹中。除此之外,一切都一样。

EDIT I forgot to mention that i also added the pipe(bs.stream()); 编辑我忘了提到我也添加了pipe(bs.stream()); line at the end of each task such as HTML, styles, and JS, so that the browser reloads every time you make a change. 在每个任务(例如HTML,样式和JS)的末尾添加一行,以便您每次进行更改时浏览器都会重新加载。


In case you don't want to make the server folder to keep your project organized, the all you have to do is delete the site/ path where ever you see it. 如果您不想使server文件夹保持项目井井有条,那么您要做的就是删除您看到的site/路径。 and for server: 'site' just replace site with ./ . 对于server: 'site'只需将Site替换为./ Port you can delete it or define your own port 端口,您可以删除它或定义自己的端口

 var gulp = require('gulp'), bs = require('browser-sync').create(), sequence = require('run-sequence'); gulp.task('styles', function() { gulp.src('site/css/*.css') .pipe(gulp.dest('site/css')) .pipe(bs.stream()); }); gulp.task('js', function() { gulp.src(['site/js/*.js']) // .pipe(concat('all.js')) .pipe(gulp.dest('site/js')) .pipe(bs.stream()); }); gulp.task('html', function() { gulp.src('site/*.html') .pipe(gulp.dest('site')) .pipe(bs.stream()); }); gulp.task('browser-sync', function() { bs.init({ server: 'site', port: 3010 }); }); gulp.task('watch', ['build'], function() { gulp.watch(['site/css/*.css'], ['styles']); gulp.watch(['site/js/*.js'], ['js']); gulp.watch(['site/*.html'], ['html']); }); gulp.task('build', function(done) { sequence( ['html', 'js', 'styles'], 'browser-sync', done); }); gulp.task('default', ['watch']); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM