更新文件时,不会重新加载Gulp浏览器同步

时间:2016-10-04 08:13:14

标签: javascript gulp browser-sync

当我在html,css和js文件中进行更改并保存时,浏览器同步不会重新加载页面。我还在手表中加入了浏览器同步功能。我已经安装了我导入的插件。我无法找出根本原因。

'use strict';

/*** Import Plugins ***/
var fileinclude = require('gulp-file-include'),
    gulp = require('gulp'),
    watch = require('gulp-watch'),
    sass = require('gulp-sass'),
    cleanCSS = require('gulp-clean-css'),
    concat = require('gulp-concat'),
    concatCss = require('gulp-concat-css'),
    uglify = require('gulp-uglify'),
    pump = require('pump'),
    runSequence = require('run-sequence'),
    htmlmin = require('gulp-htmlmin'),
    imageOptim = require('gulp-imageoptim'),
    inject = require('gulp-inject'),
    browserSync = require('browser-sync').create();

const del = require('del');


/*** Define task ***/

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

gulp.task('concat-css', function () {
    return gulp.src('./build/css/compiled/*.css')
        .pipe(concatCss("bundle.css"))
        .pipe(gulp.dest('./build/css/'));
});

gulp.task('minify-css', ['sass'], function () {
    del.sync(['./build/css/compiled/**']);
    return gulp.src('./build/css/bundle.css')
        .pipe(cleanCSS({
            compatibility: 'ie8'
        }))
        .pipe(gulp.dest('./build/css'));
});


/** Javascript **/
gulp.task('concat-scripts', function () {
    return gulp.src('./src/js/*.js')
        .pipe(concat('bundle.js'))
        .pipe(gulp.dest('./build/js'));
});

gulp.task('minify-scripts', function () {
    pump([
        gulp.src('build/js/bundle.js'),
        uglify(),
        gulp.dest('build/js/')
    ]);
});


/** HTML **/
gulp.task('include-html-templates', function () {
    return gulp.src(['src/*.html'])
        .pipe(fileinclude({
            prefix: '@@',
            basepath: '@file'
        }))
        .pipe(gulp.dest('./build/'));
});

gulp.task('inject-css-js-in-html', function () {
    return gulp.src('./build/*.html')
        .pipe(inject(gulp.src(['./build/js/bundle.js', './build/css/bundle.css'], {
            read: false
        }), {
            relative: true
        }))
        .pipe(gulp.dest('./build'));
});

gulp.task('minify-html', function () {
    return gulp.src('build/*.html')
        .pipe(htmlmin({
            collapseWhitespace: true
        }))
        .pipe(gulp.dest('./build/'));
});

/* Images */
gulp.task('optimize-images', () =>
    gulp.src('src/images/*')
    .pipe(imageOptim.optimize())
    .pipe(gulp.dest('./build/images/'))
);

/* Reloading page on update */
gulp.task('browser-sync', function () {
    browserSync.init({
        server: {
            baseDir: "./"
        },
        startPath: "build/1.html"
    });
});


/*** Watch task ***/
gulp.task('watch', function () {
    gulp.watch('src/scss/*.scss', ['sass']);
    gulp.watch('build/css/compiled/*.css', function () {
        runSequence('concat-css', 'minify-css');
    });
    gulp.watch('src/js/*.js', function () {
        runSequence('concat-scripts', 'minify-scripts');
    });
    gulp.watch('src/**/*.html', function () {
        runSequence('include-html-templates', 'minify-html', 'inject-css-js-in-html');
    });
    gulp.watch('src/images/*', ['optimize-images']);
});



/* Default task sequence */
gulp.task('default', function () {
    gulp.watch(runSequence('browser-sync', 'sass', 'concat-css', 'minify-css', 'concat-scripts', 'minify-scripts', 'include-html-templates', 'minify-html', 'inject-css-js-in-html', 'optimize-images', 'watch'));
});

1 个答案:

答案 0 :(得分:1)

您可以调用这样的方法流(基于optimize-images任务的示例)

gulp.task('optimize-images', () =>
    gulp.src('src/images/*')
    .pipe(imageOptim.optimize())
    .pipe(gulp.dest('./build/images/'))
    .pipe(browserSync.stream({once: true});
);

参数once用于最后一次重装资源。

通过这种方式,browserSync重新加载所有图像文件,如果您只想重新加载已更改的文件,可以使用插件过滤流上的文件,如gulp-changed