简体   繁体   English

Sass编译和浏览器同步

[英]Sass compiling and browser-sync

I have this code for sass compiling: 我有以下代码用于Sass编译:

gulp.task('scss', function () {
  return gulp.src('./src/scss/**/*.scss')
    .pipe(scss().on('error', scss.logError))
    .pipe(autoprefixer({
        flexbox: true,
        grid: true,
        browsers: ['> 1%', 'last 5 years', 'Firefox > 20']
    }))
    .pipe(gulp.dest('./src/styles'))
    .pipe(browserSync.stream());
});

and this task for watching changes: 此任务用于监视更改:

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

    watch('./src/**/*html', function() {
        browserSync.reload();
    });

    watch('./src/scss/**/*.scss', function() {
        gulp.start('scss');
    })

});

SCSS compiles perfectly, but no streaming and changes on the webpage. SCSS可以完美地编译,但是没有流和网页上的更改。 HTML file watching working 100% nice (reload is working correctly), also no errors in console. HTML文件观看功能100%不错(重新加载正常工作),控制台中也没有错误。 What I need to fix to make streaming work properly? 我需要解决什么才能使流媒体正常工作?

Thank you in advance! 先感谢您!

PS PS

  • solution from the docs I've tried too - with the same result 我也尝试过的文档提供的解决方案 -结果相同
  • Tried localhost and Apache server - in both variants the same result 尝试过localhost和Apache服务器-在两种变体中结果相同

 gulp.task('scss', function () { return gulp.src('./src/scss/**/*.scss') .pipe(scss().on('error', scss.logError)) .pipe(autoprefixer({ flexbox: true, grid: true, browsers: ['> 1%', 'last 5 years', 'Firefox > 20'] })) .pipe(gulp.dest('./src/styles')) .pipe(browserSync.reload({stream: true})); }); 

this should work 这应该工作

EDIT: here is my usual setup 编辑:这是我通常的设置

var browserSync = require('browser-sync').create(),
    reload = browserSync.reload;
/* SCSS / CSS */
var sass = require('gulp-sass'),
  sourcemaps = require('gulp-sourcemaps'),
  autoprefixer = require('gulp-autoprefixer');
/* VARIABLES */
var autoprefixer_config = {
    browsers: [
        'Android 2.3',
        'Android >= 4',
        'Chrome >= 20',
        'Firefox >= 24', // Firefox 24 is the latest ESR
        'Explorer >= 8',
        'iOS >= 6',
        'Opera >= 12',
        'Safari >= 6'
    ],
    cascade: false
};


/* SCSS Task*/
gulp.task('scss', function () {
    return gulp.src(paths.source.scss)
        .pipe(sourcemaps.init())
        .pipe(sass({
            includePaths: ['./node_modules', './bower_components'],
            outputStyle: 'compressed'
        }).on('error', sass.logError))
        .pipe(autoprefixer(autoprefixer_config))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(paths.dest.css))
        .pipe(reload({stream: true}));
});

/* START SERVER */
/* this part is reduced because i'm using brwosersync to proxy my local symfony server */
gulp.task('startServer', [ 'scss'], function () {


    browserSync.init();

    gulp.watch(paths.source.scss, ['scss']);


});

this setup should give you a server on localhost:3000 此设置应为您提供位于localhost:3000上的服务器

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

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