Browsersync + Gulp.js
There's no official Browsersync plugin for Gulp, because it's not needed! You simply require
the module, utilise
the API and configure it with options. The following are some common
use-cases as seen in popular projects such as Google's web starter kit
and many others.
Install ^ TOP
First, you'll need to install Browsersync & Gulp as development dependencies.
$ npm install browser-sync gulp --save-dev
Then, use them within your gulpfile.js
:
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
// Static server
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
});
// or...
gulp.task('browser-sync', function() {
browserSync.init({
proxy: "yourlocal.dev"
});
});
SASS + CSS Injecting ^ TOP
Streams are supported in Browsersync, so you can call reload at specific points during your tasks and
all browsers will be informed of the changes. Because Browsersync only cares about your CSS when it's
finished compiling - make sure you call .stream()
after gulp.dest
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {
browserSync.init({
server: "./app"
});
gulp.watch("app/scss/*.scss", ['sass']);
gulp.watch("app/*.html").on('change', browserSync.reload);
});
// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
return gulp.src("app/scss/*.scss")
.pipe(sass())
.pipe(gulp.dest("app/css"))
.pipe(browserSync.stream());
});
gulp.task('default', ['serve']);
Ruby SASS & Source Maps ^ TOP
If you use gulp-ruby-sass with the sourcemap: true
option, additional .map
files will be generated. These files end up being sent down stream and when browserSync.reload()
receives them, it will attempt
a full page reload (as it will not find any .map
files in the DOM).
To fix this problem, use browserSync.stream({match: '**/*.css'})
as seen in the following example.
var gulp = require("gulp");
var sass = require("gulp-ruby-sass");
var browserSync = require("browser-sync").create();
// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {
browserSync.init({
server: "./app"
});
gulp.watch("app/scss/*.scss", ['sass']);
gulp.watch("app/*.html").on('change', browserSync.reload);
});
/**
* Compile with gulp-ruby-sass + source maps
*/
gulp.task('sass', function () {
return sass('app/scss', {sourcemap: true})
.on('error', function (err) {
console.error('Error!', err.message);
})
.pipe(sourcemaps.write('./', {
includeContent: false,
sourceRoot: '/app/scss'
}))
.pipe(browserSync.stream({match: '**/*.css'}));
});
Browser Reloading ^ TOP
Sometimes you might just want to reload the page completely (for example, after processing a bunch of JS files), but
you want the reload to happen after your tasks. This will be easier in gulp 4.x.x
, but for now you can do the following
(make sure you return
the stream from your tasks to ensure that browserSync.reload()
is called at the correct time).
If you are using gulp 4.x.x
now, then you can follow this documentation.
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
// process JS files and return the stream.
gulp.task('js', function () {
return gulp.src('js/*js')
.pipe(browserify())
.pipe(uglify())
.pipe(gulp.dest('dist/js'));
});
// create a task that ensures the `js` task is complete before
// reloading browsers
gulp.task('js-watch', ['js'], function (done) {
browserSync.reload();
done();
});
// use default task to launch Browsersync and watch JS files
gulp.task('default', ['js'], function () {
// Serve files from the root of this project
browserSync.init({
server: {
baseDir: "./"
}
});
// add browserSync.reload to the tasks array to make
// all browsers reload after tasks are complete.
gulp.watch("js/*.js", ['js-watch']);
});
Manual Reloading ^ TOP
If the streams support doesn't suit your needs, you can fire the reload method manually by wrapping it in a task. This example will compile & auto-inject CSS just as before, but when HTML files are changed, the browsers will be reloaded instead.
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
// Save a reference to the `reload` method
// Watch scss AND html files, doing different things with each.
gulp.task('serve', function () {
// Serve files from the root of this project
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch("*.html").on("change", reload);
});
Jump to...