简体   繁体   English

Gulp浏览器重新加载不起作用-在SASS上使用手表

[英]Gulp Browser Reload Not Working - Using a Watch on SASS

I want to have a watch task that reloads the browser. 我想执行一个监视任务,以重新加载浏览器。 Below watch works fine for sass compilation but the browser doesn't reload - thoughts? 下面的手表可以很好地进行sass编译,但是浏览器无法重新加载-有想法吗?

// Gulp Packages
var gulp  = require('gulp'),
    gutil = require('gulp-util'),
    sass  = require('gulp-sass'),
    sourcemaps = require('gulp-sourcemaps'),
    browserSync = require('browser-sync').create(),
    reload = browserSync.reload;

// Default Task
gulp.task('default', ['copyFiles','styles']);

// Copy Files
gulp.task('copyFiles', function() {
    gulp.src('./source/*.php').pipe(gulp.dest('./public'));
});

// Compile SASS
var sassOptions = {
  errLogToConsole: true,
  outputStyle: 'compressed'
};
gulp.task('styles', function() {
    gulp.src('./scss/**/*.scss')
        .pipe(sourcemaps.init())
        .pipe(sass(sassOptions).on('error', sass.logError))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('./css/'));
});

// Watch Task
gulp.task('watch', function() {
    gulp.watch('./scss/includes/**/*.scss', ['styles']);
    gulp.watch('./scss/includes/**/*.scss').on('change', browserSync.reload);
});

I would suggest a configuration similar to: 我建议配置类似于:

// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {

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

    gulp.watch("app/scss/*.scss", ['sass']);
    gulp.watch("app/*.html").on('change', browserSync.reload);
});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream());
});

gulp.task('default', ['serve']);

Source: https://www.browsersync.io/docs/gulp 资料来源: https : //www.browsersync.io/docs/gulp

You need to initialise a Server and watch changes to your SASS and HTML. 您需要初始化服务器,并观察对SASS和HTML的更改。 Then in your sass compile task you need to add .pipe(browserSync.stream()); 然后在sass编译任务中,您需要添加.pipe(browserSync.stream()); after the dest command so browserSync can pick up your newly compiled SASS and serve it. 在dest命令之后,以便browserSync可以接管您新编译的SASS并将其提供服务。 Finally you need to call your serve task with your default task. 最后,您需要使用默认任务调用服务任务。

Also you can take a look here https://scotch.io/tutorials/how-to-use-browsersync-for-faster-development#using-browsersync-and-sass for another example of Browsersync + Gulp + SASS. 您也可以在这里查看https://scotch.io/tutorials/how-to-use-browsersync-for-faster-development#using-browsersync-and-sass ,以了解Browsersync + Gulp + SASS的另一个示例。

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

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