Brosersync Usage
Using with Gulp
Make sure to 📑 install gulp-cli
globally
Simple
Install the required npm packages
npm install browser-sync gulp --save-dev
Create gulpfile.js
in the project root, then paste the following code
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
// Static server
.task('browser-sync', function() {
gulp.init({
browserSyncserver: {
baseDir: "./"
};
});
})
// or...
.task('browser-sync', function() {
gulp.init({
browserSyncproxy: "yourlocal.dev"
;
}); })
Then, run the following to start server
gulp
Ref:
With Sass
Install the required npm packages
npm install --save-dev gulp gulp-sass sass gulp-postcss cssnano gulp-terser browser-sync
Create gulpfile.js
in the project root, then paste the following code
const { src, dest, watch, series } = require('gulp');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const cssnano = require('cssnano');
const terser = require('gulp-terser');
const browsersync = require('browser-sync').create();
// Sass Task
function scssTask(){
return src('app/scss/*.scss', { sourcemaps: true })
.pipe(sass())
.pipe(postcss([cssnano()]))
.pipe(dest('dist', { sourcemaps: '.' }));
}
// JavaScript Task
function jsTask(){
return src('app/js/*.js', { sourcemaps: true })
.pipe(terser())
.pipe(dest('dist', { sourcemaps: '.' }));
}
// Browsersync Tasks
function browsersyncServe(cb){
.init({
browsersyncserver: {
baseDir: '.'
};
})cb();
}
function browsersyncReload(cb){
.reload();
browsersynccb();
}
// Watch Task
function watchTask(){
watch('*.html', browsersyncReload);
watch(['app/scss/**/*.scss', 'app/js/**/*.js'], series(scssTask, jsTask, browsersyncReload));
}
// Default Gulp task
.default = series(
exports,
scssTask,
jsTask,
browsersyncServe
watchTask; )
Finally, to run the local server to host your webpages, simply run
gulp
Ref: