Gulp-sass和浏览器同步

时间:2016-01-20 17:28:46

标签: gulp jekyll browser-sync gulp-sass

我不确定问题是否是浏览器同步,但这是我最好的选择。我的问题是我第一次使用gulp来自动化我的工作流程。当我“吞下东西”它运行时,它打开一个新的网页,我的项目就像它应该的那样,但当我在我的SASS文件中更改任何内容时,它不会更新它,即使我点击刷新键,这是我的唯一方法刷新它是为了重新吞噬一切。

我已更新所有家属。我也在使用杰基尔。还有其他一些gulp-plugins。

继承人我的gulpfile.js:

var gulp               = require('gulp'),
    browserSync = require('browser-sync'),
    sass                   = require('gulp-sass'),
    prefix                = require('gulp-autoprefixer'),
    minifycss        = require('gulp-minify-css'),
    jshint                = require('gulp-jshint'),
    concat              = require('gulp-concat'),
    uglify      = require('gulp-uglify'),
    rename      = require('gulp-rename'),
    cp          = require('child_process'),
    jade        = require('gulp-jade'),
    bourbon     = require('bourbon').includePaths;

var messages = {
    jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};


/**
 * Build the Jekyll Site
 */
gulp.task('jekyll-build', function (done) {
    browserSync.notify(messages.jekyllBuild);
    var pl = process.platform === "win32" ? "jekyll.bat" : "jekyll";
    return cp.spawn(pl, ['build'], {stdio: 'inherit'})
        .on('close', done);
});


/**
 * Rebuild Jekyll & do page reload
 */
gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
    browserSync.reload();
});


/**
 * Wait for jekyll-build, then launch the Server
 */
gulp.task('browser-sync', ['sass', 'js', 'jekyll-build'], function() {
    browserSync({
        server: {
            baseDir: '_site'
        },
        notify: false
    });
});


/**
 * Compile files from _scss into both _site/css (for live injecting) and site (for future jekyll builds)
 */
gulp.task('sass', function () {
    return gulp.src('assets/css/main.scss')
        .pipe(sass({
            includePaths: [bourbon],
            onError: browserSync.notify
        }).on('error', sass.logError))
        .pipe(prefix(['last 15 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true }))
        .pipe(rename({suffix: '.min', prefix : ''}))
        .pipe(minifycss())
        .pipe(gulp.dest('assets/css'))
        .pipe(gulp.dest('_site/css'))
        .pipe(browserSync.reload({stream:true}));
});


/**
 * Compile Jade
 */
gulp.task('jade', function() {
    return gulp.src('_jadefiles/*.jade')
    .pipe(jade())
    .pipe(gulp.dest('_includes'));
});


/*
** JS Task
*/
gulp.task('js', function() {
  return gulp.src('assets/js/common.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default'))
    .pipe(concat('common.js'))
    .pipe(gulp.dest('assets/js'))
    .pipe(rename({suffix: '.min'}))
    .pipe(uglify())
    .pipe(gulp.dest('assets/js'))
    .pipe(gulp.dest('_site/js'));
});


/**
 * Watch scss files for changes & recompile
 * Watch html/md files, run jekyll & reload BrowserSync
 */
gulp.task('watch', function () {
    gulp.watch('assets/js/**/*.js', ['js']).on("change", browserSync.reload);
    gulp.watch('assets/css/**', ['sass']);
    gulp.watch(['*.html', '_layouts/*.html', '_posts/*', '_includes/*'], ['jekyll-rebuild']);
    gulp.watch('_jadefiles/*.jade', ['jade']);
});


/**
 * Default task, running just `gulp` will compile the sass,
 * compile the jekyll site, launch BrowserSync & watch files.
 */
gulp.task('default', ['browser-sync', 'watch']);

2 个答案:

答案 0 :(得分:1)

你的sass文件在哪里?

底部的位 - &#39;观看&#39;可能就是问题所在。

我认为您需要添加sass文件夹的位置。假设它是_sass,那么可能会将它添加到第3个gulp.watch行,就像这样可以。

gulp.watch(['*.html','_sass/*', '_layouts/*.html', '_posts/*', '_includes/*'], ['jekyll-rebuild']);

答案 1 :(得分:1)

此代码设置您的手表。但只有js更改设置为通知浏览器同步:

gulp.task('watch', function () {
    gulp.watch('assets/js/**/*.js', ['js']).on("change", browserSync.reload);
    gulp.watch('assets/css/**', ['sass']);
    gulp.watch(['*.html', '_layouts/*.html', '_posts/*', '_includes/*'], ['jekyll-rebuild']);
    gulp.watch('_jadefiles/*.jade', ['jade']);
});

告诉gulp watch在sass更改时通知浏览器同步:

gulp.watch('assets/css/**/*.sass', ['sass']).on("change", browserSync.reload);

但实际上你想在sass更改中运行'sass',然后在css更改时通知browserSync:

gulp.watch('assets/css/**/*.css').on("change", browserSync.reload);
gulp.watch('assets/css/**/*.sass', ['sass'])