Gulp 4 browserSync重新加载

问题描述 投票:0回答:1

我在使用browserSync时遇到问题我尝试了几种不同的方法后无法获得重载。 BrowserSync本身已启动并运行,但是当我手动重新加载没有任何反应时,我必须打开一个新选项卡以查看任何更改。我并不是真正理解gulp 4,我看过的所有资源似乎都在使用完全不同的方法。非常感谢任何帮助随时提出任何问题。

enter image description here

var gulp = require('gulp');
var sass = require('gulp-sass');
var concatcss = require('gulp-concat');
var concatjs = require('gulp-concat');
var uglifycss = require('gulp-uglifycss');
var reload = require('browser-sync').reload();
var nunjucks = require('gulp-nunjucks-render');
var browserSync = require('browser-sync').create();


sass.compiler = require('node-sass');

gulp.task('sass', function () {
 return gulp.src('./Edit/sass/*.scss')
   .pipe(sass().on('error', sass.logError))
   .pipe(gulp.dest('./Edit/css'));
});

gulp.task('concatcss', function() {
  return gulp.src('./Edit/css/*.css')
    .pipe(concatcss('style.css'))
    .pipe(gulp.dest('./Edit/css/concated/'));
});

gulp.task('concatjs', function() {
  return gulp.src('./Edit/java-script/*.js')
    .pipe(concatjs('scripts.js'))
    .pipe(gulp.dest('./Upload/js/'));
});

gulp.task('css', function () {
  return gulp.src('./Edit/css/concated/*.css')
    .pipe(uglifycss({
      "maxLineLen": 80,
      "uglyComments": true
    }))
    .pipe(gulp.dest('./upload/css'));
});

 
gulp.task('browserSync', function() {

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

});


gulp.task('run',gulp.parallel('browserSync', gulp.series('sass','concatcss','concatjs','css')));

gulp.task('watch', function(){
    gulp.watch('./Edit/sass/*.scss',gulp.series('sass'));
    gulp.watch('./Edit/css/*.css',gulp.series('concatcss'));
    gulp.watch('./Edit/java-script/*.js',gulp.series('concatjs')); // maybe put extra task in all gulp series for browser sync
    gulp.watch('./Edit/css/concated/*.css', gulp.series('css'));

});

gulp.task('default', gulp.parallel('watch', 'run'));
gulp
1个回答
1
投票

对于其他遇到麻烦的人来说,我的解决方案就是将这个.on('change',browserSync.relod)添加到所需手表的末尾。

gulp.task('watch', function(){
    gulp.watch('./Edit/sass/*.scss',gulp.series('sass'));
    gulp.watch('./Edit/css/*.css',gulp.series('concatcss'));
    gulp.watch('./Edit/java-script/*.js',gulp.series('concatjs')); // maybe put extra task in all gulp series for browser sync
    gulp.watch('./Edit/css/concated/*.css', gulp.series('css'));
    gulp.watch('./Upload/css/*.css').on('change', browserSync.reload);
    gulp.watch('./Upload/js/*.js').on('change', browserSync.reload);

});
© www.soinside.com 2019 - 2024. All rights reserved.