GULP:如何在不刷新的情况下更新浏览器(仅适用于 CSS 更改)
gulp: how to update the browser without refresh (for css changes only)
我已经设置了gulp,以便在我进行更改时浏览器重新加载。但是,对于 css 更改,我希望浏览器在不刷新的情况下更新,但我不确定如何做到这一点。对于我当前的设置,我使用了本教程:
var debug = require('gulp-debug'),
connect = require('gulp-connect'),
watch = require('gulp-watch'),
livereload = require('gulp-livereload');
gulp.task('webserver', function () {
connect.server({
livereload: true,
root: ['demo/']
});
});
gulp.task('livereload', function () {
gulp.src(['index.html', 'resources/**/*.js'])
.pipe(watch(['resources/**/*.html', 'index.html']))
//.pipe(debug({verbose: true}))
.pipe(connect.reload());
});
gulp.task('watch', function () {
livereload.listen();
gulp.watch('resources/**/*.scss', ['css']);
});
在我的 css 任务中,我也调用
.pipe(livereload());
任何建议我需要修改,以便 css 更新无需刷新即可发生?
更新:这是我的css任务(有点简化(:
gulp.task('css', function () {
...
return gulp.src('demo.scss')
.pipe($.sass({
errLogToConsole: true
}))
.pipe(gulp.dest('./target/')
.pipe(livereload());
});
当发生变化时,您需要调用livereload.changed(files)
。为此,请参阅gulp-watch文档。
watch('**/*.js', function (files) {
livereload.changed(files)
});
您可能需要一个 chrome 扩展程序来获取 gulp-livereload
我和你有同样的问题,我解决了。
我发现在gulp-connect中使用嵌入式实时加载支持是不受控制的。
因此,我尝试使livereload独立工作。
只需设置您的gulp-connect配置而无需livereload,并使用指定的主机选项调用livereload.listen(大多数情况下它应该是"localhost"(。喜欢
livereload.listen({ host: 'localhost' });
然后监视要检测的文件。喜欢
var changedFile = null;
gulp.task("livereload", function(cb){
return gulp.src(changedFile)
.pipe(plumber())
.pipe(livereload());
});
gulp.watch(['*.css']).on('change', function(file) {
changedFile = file.path;
// run specific task according to the given file's extension
// gulp.start(['livereload']);
});
相关文章:
- Angularjs ng-options 问题仅适用于 Chrome 浏览器
- Backbone Project适用于除Firefox之外的所有浏览器
- 是否有适用于iOS和Android浏览器的性能指南
- jquery在页面加载时淡入适用于除IE以外的所有浏览器
- html 选择列表更改无法在谷歌浏览器上调用方法(适用于 IE)
- GULP:如何在不刷新的情况下更新浏览器(仅适用于 CSS 更改)
- Jquery适用于IE以外的所有浏览器
- 未在火狐浏览器的选择列表中选择值(适用于chrome,safari,opera)
- Android WebView 不适用于默认浏览器,但适用于 Chrome
- 在上传之前在页面上预览图像,它适用于除IE之外的所有浏览器
- 如何设置适用于HTML5浏览器和旧浏览器的type=date输入的默认值
- 谷歌地图V3 JavaScript仅适用于Chrome浏览器
- JavaScript for Each循环适用于除Internet Explorer之外的所有Web浏览器
- 这适用于IE,但不适用于chrome.如何使它在所有浏览器中都能工作
- 它适用于jsFiddle,但不是在我的浏览器(FF, Chrome)
- jQuery滑块适用于除Chrome 15以外的所有浏览器.调整Chrome 15浏览器窗口大小时再次工作
- keyup()适用于webkit浏览器,但不适用于Firefox或IE
- CSS仅适用于iPhone和普通浏览器
- JQuery .animate()仅适用于Chrome浏览器
- Bookmarklet适用于除Firefox以外的所有浏览器