我弄的每次修改 js 或 html 文件,浏览器都会刷新页面。而不会重新加载。 这一点都不像网上说的
以下是我的代码:
browserSync = require('browser-sync').create(),
var htmlSrc = ['app/**/*.html', '!app/index.html', '!app/lib/**/*.html', '!app/lib/*.html'];
var jsSrc = ['app/**/*.js', '!app/lib/**/*.js', '!app/assets/**/*.js'];
var cssSrc = ['app/assets/**/*.css', '!app/lib/**/*.css'];
var imgSrc = ['app/assets/img/*', 'app/assets/img/**/*'];
var fontSrc = ['app/assets/fonts/**/*'];
var jsonSrc = ['app/translate/*.json', 'app/translate/**/*.json'];
var libSrc = ['app/lib/**/*'];
var indexSrc = 'app/index.html';
var buildPath = 'dist/';
var debugPath = 'app/';
gulp.task('createIndex', function() {
return gulp.src(indexSrc)
.pipe(replace(/\.js/g, '.min.js'))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(buildPath))
.pipe(browserSync.stream());
});
gulp.task('compressHTML', function() {
return gulp.src(htmlSrc)
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(buildPath))
.pipe(browserSync.stream());
});
gulp.task('compressJS', function() {
var filterIndex = filter(['**/app.config.js', '**/app.module.js', '**/app.route.oclazy.js'], {restore: true});
return gulp.src(jsSrc)
.on('error', function(err) {//语法检查,打印错误
console.log('Error: ', err.message);
})
.pipe(filterIndex)
.pipe(rename({suffix: '.min'}))
.pipe(filterIndex.restore)
.pipe(uglify())//压缩
.pipe(gulp.dest(buildPath))
.pipe(browserSync.stream());
});
gulp.task('compressCSS', function() {
return gulp.src(cssSrc)
//.pipe(concat('index.min.css')) 为了使用 oclazyload 不整合
.pipe(minifycss())
.pipe(gulp.dest(buildPath + 'assets'))
.pipe(browserSync.stream());
});
gulp.task('compressJSON', function () {
return gulp.src(jsonSrc)
.pipe(jsonminify())
.pipe(gulp.dest(buildPath + 'translate'))
.pipe(browserSync.stream());
});
gulp.task('copyImg', function() {
return gulp.src(imgSrc)
.pipe(gulp.dest(buildPath + 'assets/img'))
.pipe(browserSync.stream());
});
gulp.task('copyLib', function() {
return gulp.src(libSrc)
.pipe(gulp.dest(buildPath + 'lib'))
.pipe(browserSync.stream());
});
gulp.task('copyFont', function() {
return gulp.src(fontSrc)
.pipe(gulp.dest(buildPath + 'assets/fonts'))
.pipe(browserSync.stream());
});
gulp.task('clean', function() {
return gulp.src(buildPath)
.pipe(clean());
});
gulp.task('startCreate', ['clean'], function() {
runSequence('createIndex',
['compressHTML', 'compressJS', 'compressCSS', 'compressJSON'],
['copyLib', 'copyImg', 'copyFont']);
});
gulp.task('browserSync', function() {
gulp.start('startCreate');
browserSync.init({
server: {
baseDir: buildPath
}
});
gulp.watch(indexSrc, ['createIndex']);
gulp.watch(htmlSrc, ['compressHTML']);
gulp.watch(jsSrc, ['compressJS']);
gulp.watch(cssSrc, ['compressCSS']);
gulp.watch(jsonSrc, ['compressJSON']);
});
gulp.task('default', ['browserSync']);