Every time I modify a js or html file, the browser will refresh the page. without reloading. This is nothing like what is said on the Internet
The following is my code:
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']);
New answer: // The comment area doesn’t fit
I’m very sorry that I’ve never seen your way of writing. This is my enhancement to the
ionic 1.0
version. You can refer to:I have never seen this usage, please take a look at the official documentation http://www.browsersync.cn/doc...
Are you still monitoring the server?
Then reload without change?
gulp.watch("*.html").on("change", browserSync.reload);
@红泽 @小_u_ Jiang Thank you very much for your reply. I will modify it as follows (disappointingly, it still has no effect. Every time I modify the js file, the browser will still refresh the page)
In addition, my gulp version is 3.9.1