Gulp浏览器同步只能工作一次

Val*_*res 12 javascript gulp gulp-less gulp-watch

我在我的一个项目上尝试了Gulp,我想像Grunt手表一样运行它.这意味着,一旦完成所有操作,它必须观察较少的文件和js文件,lint,合并,编译和刷新浏览器.

我设法使用gulp-browser-sync工作但由于某种原因它只能工作一次.我对我的.less文件进行了更改,浏览器重新加载.然后,第二次更改,它确实编译但没有重新加载发生.

这是日志:

[BS] Serving files from: ./
[09:47:26] Starting 'css-clean'...
[09:47:26] Finished 'css-clean' after 16 ms
[09:47:26] Starting 'styles'...
[BS] 1 file changed (styles.min.css)
[09:47:27] Finished 'styles' after 624 ms
[09:47:27] Starting 'styles-watch'...
[BS] Reloading Browsers...
Run Code Online (Sandbox Code Playgroud)

当我第二次点击保存时:

[09:47:31] Starting 'css-clean'...
[09:47:31] Finished 'css-clean' after 3.39 ms
[09:47:31] Starting 'styles'...
[BS] 1 file changed (styles.min.css)
[09:47:32] Finished 'styles' after 362 ms
Run Code Online (Sandbox Code Playgroud)

至于JS,它一直都在工作.没有任何问题,即使在完成样式任务之后,JS更改仍然会正确地触发重新加载.真的只是有问题的风格.

这是gulpfile.js

var gulp = require('gulp'),
    autoprefixer = require('gulp-autoprefixer'),
    less = require('gulp-less'),
    minifycss = require('gulp-minify-css'),
    concat = require('gulp-concat'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    notify = require('gulp-notify'),
    path = require('path'),
    streamqueue = require('streamqueue'),
    clean = require('gulp-clean'),
    browserSync = require('browser-sync').create(),
    reload = browserSync.reload;


// Clean the CSS folder
gulp.task('css-clean', function () {
    return gulp.src(['dist/css'], {read: false})
        .pipe(clean());
});

// Clean the CSS folder
gulp.task('js-clean', function () {
    return gulp.src(['dist/js'], {read: false})
        .pipe(clean());
});


// Generate the CSS styles, clean before hand
gulp.task('styles', ['css-clean'], function() {
  return streamqueue({ objectMode: true },
            gulp.src(['./bower_components/uikit/less/uikit.less']),
            gulp.src(['./src/less/main.less'])
        )
    .pipe(concat('styles.less'))
    .pipe(less({
      paths: [ path.join(__dirname, 'less', 'includes') ]
    }))
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(gulp.dest('dist/css'))
    .pipe(rename({suffix: '.min'}))
    .pipe(minifycss())
    .pipe(gulp.dest('dist/css'))
    .pipe(browserSync.reload({stream:true}));
});

// create a task that ensures the `styles` task is complete before
// reloading browsers
gulp.task('styles-watch', ['styles'], browserSync.reload);



// Lint Task
gulp.task('lint', function() {
    return gulp.src('src/js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});


// Generate the scripts, clean before hand
gulp.task('scripts', ['js-clean', 'lint'], function() {
  return streamqueue({ objectMode: true },
            gulp.src(['./bower_components/jquery/dist/jquery.js']),
            gulp.src(['./bower_components/modernizer/modernizr.js']),
            gulp.src(['./bower_components/uikit/js/uikit.js']),
            gulp.src(['./src/js/plugin.js']),
            gulp.src(['./src/js/main.js'])
        )
    .pipe(concat('scripts.js'))
    .pipe(gulp.dest('dist/js'))
    .pipe(rename({suffix: '.min'}))
    .pipe(uglify())
    .pipe(gulp.dest('dist/js'))
    .pipe(browserSync.reload({stream:true}));
});

// create a task that ensures the `scripts` task is complete before
// reloading browsers
gulp.task('scripts-watch', ['scripts'], browserSync.reload);


// Lint Task
gulp.task('alldone', ['scripts'], function() {
    return gulp.src('src/js/*.js')
               .pipe(notify({ message: 'Gulp tasks are completed!!' }));
});


// Static server
gulp.task('browsersync', function() {
    browserSync.init({
        server: {
            baseDir: "./"
        }
    });

    gulp.watch("src/less/*.less", ['styles-watch']);
    gulp.watch('src/js/*.js', ['lint', 'scripts-watch']);
    gulp.watch("*.html").on('change', reload);
});


// Default Task
gulp.task('default', ['css-clean', 'js-clean', 'styles', 'lint', 'scripts', 'alldone']);

// Build Task
gulp.task('build',   ['css-clean', 'js-clean', 'styles', 'lint', 'scripts', 'alldone']);
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

Cha*_*ras 23

怎么样直接使用

.pipe(browserSync.stream())
Run Code Online (Sandbox Code Playgroud)

我曾经在更改'.jade'文件时遇到同样的问题,它只重新加载一次.然后我在一个匿名函数中包装了browserSync.reload

gulp.task('templates-watch', ['templates'], function() {
    browserSync.reload();
});
Run Code Online (Sandbox Code Playgroud)

这个对我有用.我不知道browserSync.reload是否有什么特别之处.怎么样试一试.:)

  • 匿名功能完全不同!+1万 (5认同)
  • 那是因为browserSync.reload函数接受了参数.https://github.com/BrowserSync/browser-sync/issues/711#issuecomment-142460409 (2认同)

小智 16

对于我在Gulp 4中,匿名函数没有解决问题,我所做的是将browserSync.reload()包装在带有完成回调的函数中:

function reload(done) {
  browserSync.reload();
  done();
}

gulp.watch(['scripts/**/*.js','!scripts/main.min.js'], gulp.series(buildScripts, reload));
Run Code Online (Sandbox Code Playgroud)


小智 0

你确定浏览器中的 CSS 没有改变吗?加载新样式无需重新加载页面。至少我的 gulp 设置就是这么做的。

尝试更改 .less 文件,并查看更改是否在浏览器中实际可见。