Last active
February 13, 2019 06:25
-
-
Save jbutko/d36229903d419d721772 to your computer and use it in GitHub Desktop.
BrowserSync + Gulp.js: Simple webServer + liveReload + watching CSS, HTML and SASS files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This example: | |
* Uses the built-in BrowserSync server for HTML files | |
* Watches & compiles SASS files | |
* Watches & injects CSS files | |
* | |
* More details: http://www.browsersync.io/docs/gulp/ | |
* | |
* Install: | |
* npm install browser-sync gulp gulp-sass --save-dev | |
* | |
* Then run it with: | |
* gulp | |
*/ | |
var browserSync = require('browser-sync'); | |
var reload = browserSync.reload; | |
var gulp = require('gulp'); | |
var sass = require('gulp-sass'); | |
// Browser-sync task, only cares about compiled CSS | |
gulp.task('browser-sync', function() { | |
browserSync({ | |
server: { | |
baseDir: "./" | |
} | |
}); | |
}); | |
// Sass task, will run when any SCSS files change & BrowserSync | |
// will auto-update browsers | |
gulp.task('sass', function () { | |
return gulp.src('scss/**/*.scss') | |
.pipe(sass()) | |
.pipe(gulp.dest('css')) | |
.pipe(reload({stream:true})); | |
}); | |
// Reload all Browsers | |
gulp.task('bs-reload', function () { | |
browserSync.reload(); | |
}); | |
// Default task to be run with `gulp` | |
// This default task will run BrowserSync & then use Gulp to watch files. | |
// When a file is changed, an event is emitted to BrowserSync with the filepath. | |
gulp.task('default', ['browser-sync'], function () { | |
gulp.watch('css/*.css', function (file) { | |
if (file.type === "changed") { | |
reload(file.path); | |
} | |
}); | |
gulp.watch("*.html", ['bs-reload']); | |
}); |
you should use arrow functions and const
this should be updated for gulp 4
gulp.task('default', gulp.series('browser-sync', function() {
gulp.watch('css/.css', function (file) {
if (file.type === "changed") {
reload(file.path);
}
});
gulp.watch(".html", ['bs-reload']);
}));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi jbutko,
Is there a way to reload whenever a scss file changes?
Now "gulp" is watching for css and html changes, but I have to write "gulp sass" in order to css been compiled. So "gulp" realizes, and the browser reloads itself.
I've tried some other solutions I've seen on the net, and also I've modified your gulpfile.
My better result worked fine, until the third change on the same scss file, that throws an error message:
events.js:112
throw er; // Unhandled 'error' event
^
Error: src\scss\components_index.scss
Error: File to import not found or unreadable: navbar.
Thanks,