Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

browserSync.reload and browserSync.stream()) - what are the difference?

I have this gulpfile.js file:

var gulp = require('gulp'),
sass = require('gulp-sass'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
browserSync = require('browser-sync').create();

gulp.task('sass', function() {
    gulp.src('assets/src/sass/*.scss')
    .pipe(sass())
    .pipe(gulp.dest('assets/dist/css'))
    .pipe(browserSync.stream());
});

gulp.task('scripts', function() {
    gulp.src('assets/src/js/*.js')
    .pipe(concat('main.js'))
    .pipe(uglify())
    .pipe(gulp.dest('assets/dist/js'));
}); 

gulp.task('server', ['sass','scripts'], function() {
    browserSync.init({
        proxy: 'http://localhost/example/',
    });
    gulp.watch('assets/src/sass/*.scss', ['sass']);
    gulp.watch('assets/src/js/*.js', ['scripts']);
    gulp.watch('./**/*.php').on('change', browserSync.reload);
});

gulp.task('server', ['run']);

Please tell me what are the difference between:

.pipe(browserSync.stream());

and:

gulp.watch('./**/*.php').on('change', browserSync.reload);

I need both of them? they have different role?

Thanks.

like image 310
ronjacob Avatar asked Sep 07 '17 21:09

ronjacob


People also ask

What is Browsersync used for?

The BrowserSync is used to watch all HTML and CSS files in the css directory and performs the live reload to the page in all browsers whenever files were changed. BrowserSync makes workflow faster by synchronizing URL's, interactions and code changes across multiple devices.


1 Answers

You probably must have got your answer till now but I'll leave the answer here just in case anyone needs to know.

browserSync.reload

is used to do a page refresh. Ideally, it's used on HTML & JS files.

browserSync.stream

is used to inject changes without refreshing the page. Ideally, this is used on CSS and other stylesheet formats. This command is useful because it keeps the scroll position intact and doesn't take you to the top of the page like a page refresh usually would.

like image 133
Yash Ghelani Avatar answered Oct 12 '22 08:10

Yash Ghelani