So I seem to have gulp working with browsersync. When I start gulp it opens my browser and gives me the following:
[13:01:41] Using gulpfile D:\Test\gulpfile.js
[13:01:41] Starting 'browser-sync'...
[13:01:41] Finished 'browser-sync' after 46 ms
[13:01:41] Starting 'default'...
[13:01:41] Finished 'default' after 12 µs
[BS] Access URLs:
-------------------------------------
Local: http://localhost:3000
External: http://10.127.127.1:3000
-------------------------------------
UI: http://localhost:3001
UI External: http://10.127.127.1:3001
-------------------------------------
[BS] Serving files from: ./
Here is my gulpfile
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
// Static server
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
});
gulp.task('default', ['browser-sync']);
I would it to just watch all the files in my project and reload when there is a change.
Try to modify your task (just add one line)
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch("**/*").on('change', browserSync.reload); // add this line
});
or maybe you can try my gulp configuration https://github.com/infernalmaster/gulp-happy-starter (it has many code examples inside)
You can try making and array after browserSync.init :
browserSync.init([ "./assets/styles/**/*.css" , "./*.html" ],{
server: {
baseDir: "./"
}
});
Add your file paths in the array and after that just watch for changes:
gulp.watch("*.css").on("change", browserSync.reload));
gulp.watch("*.html").on("change", browserSync.reload));
Related
I serve a site in development via browser-sync and have tried to wire up the sub-directories, so that the browser on files changes reloads. -- However the browser does not reload when I kick of the server with
$ gulp serve
and load the site in a browser and then change index.html. After this the display in the browser does not update by itself.
Do I do something wrong in my gulpfile?:
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var site = "site"
function browser_sync_init() {
browserSync.init({
server: {
baseDir: site,
index: "index.html"
},
port: 8080,
ui: false,
open: false,
});
}
gulp.task('serve', function() {
browser_sync_init();
gulp.watch(site+"/scripts/*.js").on('change', browserSync.reload);
gulp.watch(site+"/*.html").on('change', browserSync.reload);
});
gulp.task('default', gulp.series('serve'));
Thanks for any pointers!
I am building a website on gulp, this code snippet basically contains 3 tasks browser-sync jekyll-build stylus, browser-sync task depends upon jekyll-build.
browser-sync works fine but the stylus function in which I am returning a stream is giving a error which is not expected.
Below is my code snippet.
I am returning a stream here which is one of the solutions mentioned in the doc Async Completion but still I get the error.
var gulp = require('gulp'),
plumber = require('gulp-plumber'),
browserSync = require('browser-sync'),
stylus = require('gulp-stylus'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
jeet = require('jeet'),
rupture = require('rupture'),
koutoSwiss = require('kouto-swiss'),
prefixer = require('autoprefixer-stylus'),
imagemin = require('gulp-imagemin'),
cp = require('child_process');
var messages = {
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};
var jekyllCommand = (/^win/.test(process.platform)) ? 'jekyll.bat' : 'jekyll';
/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn(jekyllCommand, ['build'], {stdio: 'inherit'})
.on('close', done);
});
/**
* Wait for jekyll-build, then launch the Server
*/
gulp.task('browser-sync', gulp.series('jekyll-build'), function() {
browserSync({
server: {
baseDir: '_site'
}
});
done();
});
/**
* Stylus task
*/
gulp.task('stylus', function() {
return gulp.src('src/styl/main.styl').pipe(plumber())
.pipe(
stylus({
use: [koutoSwiss(), prefixer(), jeet(), rupture()],
compress: true,
})
)
.pipe(gulp.dest('_site/assets/css/'))
.pipe(gulp.dest('assets/css'))
.pipe(browserSync.stream());
});
ERROR SHOWN BELOW
[23:28:52] Using gulpfile ~/berserker1.github.io/gulpfile.js
[23:28:52] Starting 'default'...
[23:28:52] Starting 'browser-sync'...
[23:28:52] Starting 'jekyll-build'...
Configuration file: /home/aaryan/berserker1.github.io/_config.yml
Source: /home/aaryan/berserker1.github.io
Destination: /home/aaryan/berserker1.github.io/_site
Incremental build: disabled. Enable with --incremental
Generating...
done in 0.234 seconds.
Auto-regeneration: disabled. Use --watch to enable.
[23:28:52] Finished 'jekyll-build' after 818 ms
[23:28:52] Finished 'browser-sync' after 819 ms
[23:28:52] Starting 'stylus'...
[23:28:53] The following tasks did not complete: default, stylus
[23:28:53] Did you forget to signal async completion?
There is an issue here (whether it is the same as the one in your question we'll see if it helps):
gulp.task('browser-sync', gulp.series('jekyll-build'), function() {
browserSync({
server: {
baseDir: '_site'
}
});
done();
});
I gulp v4 task takes only two arguments - the code above has three. This should be correct:
gulp.task('browser-sync', gulp.series('jekyll-build', function(done) {
browserSync({
server: {
baseDir: '_site'
}
});
done();
}));
Plus, I added the done parameter in the first line. See if this helps.
I am scratching my head around but can't seem to figure out whats wrong with the following gulpfile which simply watch and compile less file.
This simply won't watch less changes, I have tried all gulp, gulp watch. I have to manually run gulp after each change to compile them. Is there something wrong that causing watch to not work as expected?
Gulp Version
CLI version 1.4.0
Local version 3.9.1
NPM 4.1.2
Node v7.7.2
var gulp = require('gulp');
var less = require('gulp-less');
// gulp compile paths
var paths = {
dist: 'assets/dist'
};
gulp.task('css', function() {
return gulp.src('assets/less/styles.less')
.pipe(less({
compress: true
}))
.pipe(gulp.dest(paths.dist))
});
gulp.task('watch', function() {
gulp.watch('assets/less/*.less', ['css']); // Watch all the .less files, then run the less task
});
gulp.task('default', ['watch']);
Make sure you have correct path to the .less files also you need to add bowersync.reload for it to work.
gulp.task('watch', function () {
gulp.watch('assets/less/*.less', ['css']);
// init server
browserSync.init({
server: {
proxy: "local.build",
baseDir: appPath.root
}
});
gulp.watch([appPath.root + '**'], browserSync.reload);
});
gulp.task('default', ['watch']);
So I'm trying to compile pug (jade), javascript and sass files with Gulp V4, and using browserSync to reload the browser when any of these files change. There are a lot of guides out there, but no matter which way I write the code, it just doesn't seem to get the task order right. Im using gulp.series to set the task order, but browserSync doesn't want to play ball.
Here's a print of my Gulp file:
var gulp = require('gulp'),
... etc.
var paths = {
sass: ['src/scss/**/*.scss'],
js: ['src/js/**/*.js'],
pug: ['src/**/*.pug']
};
// Shared Tasks:
//*------------------------------------*/
gulp.task('clean', function(done){
del(['dist/assets/**/*.css', 'dist/assets/**/*.map', 'dist/assets/**/*.js']);
done();
});
// App Specific Tasks:
//*------------------------------------*/
// Sass
gulp.task('build-sass', function(){
gulp.src(paths.sass)
return sass(paths.sass, {
style: 'expanded'
})
.on('error', sass.logError)
.pipe(prefix('last 2 version', '> 1%', 'ie 8', 'ie 9'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist/assets/css'))
.pipe(cleanCSS({ advanced: false, keepSpecialComments: 0 }))
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('dist/assets/css'));
});
// JS
gulp.task('build-js', function(done){
gulp.src(paths.js)
.pipe(concat('all.js'))
.pipe(gulp.dest('dist/assets/js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('dist/assets/js'))
done();
// Pug
gulp.task('build-pug', function buildHTML(done){
gulp.src(['src/**/*.pug'], {
base: 'src'
})
.pipe(pug())
.pipe(gulp.dest('./dist/'))
done();
});
// Service Tasks:
//*------------------------------------*/
// Delete the compiled views dir.
gulp.task('remove', function(done){
del(['dist/views/**/']);
done();
});
// Serve the site locally and watch for file changes
gulp.task('serve', function(done){
browserSync.init({
server: {
baseDir: './dist/',
notify: false,
open: false
}
});
// Watch & Reload Pug Files
gulp.watch([paths.pug], gulp.series('build-pug')).on('change', browserSync.reload);
// Watch & Reload Sass Files
gulp.watch([paths.sass], gulp.series('build-sass')).on('change', browserSync.reload);
});
gulp.task('default', gulp.series('clean', 'build-sass', 'build-js', 'build-pug', 'remove', 'serve', function(done){
done();
}));
And the terminal console log outputs this when starting gulp:
$ gulp
[13:09:33] Using gulpfile ~/Dropbox Aaron/Dropbox (Personal)/01_Me/01_Dev/01_My Work/*Template/gulpfile.js
[13:09:33] Starting 'default'...
[13:09:33] Starting 'clean'...
[13:09:33] Finished 'clean' after 2.64 ms
[13:09:33] Starting 'build-sass'...
[13:09:33] Finished 'build-sass' after 443 ms
[13:09:33] Starting 'build-js'...
[13:09:33] Finished 'build-js' after 4.42 ms
[13:09:33] Starting 'build-pug'...
[13:09:33] Finished 'build-pug' after 3.15 ms
[13:09:33] Starting 'remove'...
[13:09:33] Finished 'remove' after 656 μs
[13:09:33] Starting 'serve'...
[BS] Access URLs:
-------------------------------------
Local: http://localhost:3000
External: http://10.130.91.51:3000
-------------------------------------
UI: http://localhost:3001
UI External: http://10.130.91.51:3001
-------------------------------------
[BS] Serving files from: ./dist/
And this when I change a file within the watched files:
[BS] File changed: src/scss/4_elements/_elements.page.scss
[13:12:33] Starting 'build-sass'...
[13:12:34] write ./style.css
[13:12:34] Finished 'build-sass' after 366 ms
So what seems to me to be happening in this instance, the file is changing, browserSync reloads the page, and then 'build-sass' starts. Which is the wrong way round. I obviously want to build the sass, then reload the page. I have to save 2 times for the browser to refresh the code I want.
Some of the code I've used is directly from the browserSync documentation:
https://www.browsersync.io/docs/gulp#gulp-reload
This doesn't seem to work.
Your log output looks right. First it runs 'default' and then your series of tasks. Later when you modify a sass file, it starts 'build-sass'. The only problem is it doesn't reload via browserSync.reload(). So I believe the problem may be here:
gulp.watch([paths.sass], gulp.series('build-sass')).on('change', browserSync.reload);
I highly recommend
https://github.com/gulpjs/gulp/blob/4.0/docs/recipes/minimal-browsersync-setup-with-gulp4.md
that is the gulp4.0 recipe to do exactly what you are trying to do. I am using pretty much what they have there, so if you have problems getting their code to work I can help. Here is my working code:
function serve (done) {
browserSync.init({
server: {
baseDir: "./",
index: "home.html"
},
ghostMode: false
});
done();
}
function reload(done) {
browserSync.reload();
done();
}
var paths = {
styles: {
src: "./scss/*.scss",
dest: "./css"
},
scripts: {
src: "./js/*.js",
dest: "./js"
}
};
function watch() {
gulp.watch(paths.scripts.src, gulp.series(processJS, reload));
gulp.watch(paths.styles.src, gulp.series(sass2css, reload));
gulp.watch("./*.html").on("change", browserSync.reload);
}
var build = gulp.series(serve, watch);
gulp.task("sync", build);
function sass2css() {
return gulp.src("./scss/*.scss")
.pipe(cached("removing scss cached"))
// .pipe(sourcemaps.init())
.pipe(sass().on("error", sass.logError))
// .pipe(sourcemaps.write("./css/sourceMaps"))
.pipe(gulp.dest("./css"));
}
function processJS() {
return gulp.src("./js/*.js")
.pipe(sourcemaps.init())
// .pipe(concat("concat.js"))
// .pipe(gulp.dest("./concats/"))
// .pipe(rename({ suffix: ".min" }))
// .pipe(uglify())
.pipe(sourcemaps.write("./js/sourceMaps/"))
.pipe(gulp.dest("./js/maps/"));
}
Note the general use of functions instead of 'tasks' and particularly the reload(done) function. You might try simply adding that to your watch statements like
gulp.watch([paths.sass], gulp.series('build-sass', reload);
after you have added the reload function, that might be enough to fix your problem without making all the other changes as in my code. Or you might simply have to add a return statement to your 'build-sass' task so gulp knows that task has completed. Good luck.
I start it off with
gulp sync
and get
[17:14:23] Starting 'scripts'...
[17:14:23] Finished 'scripts' after 43 ms
[17:14:23] Starting 'sync'...
[17:14:23] Finished 'sync' after 20 ms
[BS] Access URLs:
--------------------------------------
Local: http://localhost:(my port)
External: http://(my internal IP address)
--------------------------------------
UI: http://localhost:(myport+n)
UI External: http://(my internal IP address)
--------------------------------------
[BS] Serving files from: public
The browser loads up the page.
I save a file and it works. Terminal output :
[17:17:22] Starting 'scripts'...
[17:17:22] Finished 'scripts' after 17 ms
[17:17:22] Starting 'scripts-watch'...
[BS] Reloading Browsers...
I save the file again (or save a different file)
[17:18:38] Starting 'scripts'...
[17:18:38] Finished 'scripts' after 8.99 ms
Note theres no Reloading Browsers message. The browser does not reload. Stopping gulp and manually reloading the page DOES reflect the change.
My gulpfle :
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var less = require('gulp-less');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var react = require('gulp-react');
var minifyCSS = require('gulp-minify-css');
var browserSync = require('browser-sync').create();
gulp.task('less', function() {
return gulp.src('dev/less/*.less')
.pipe(less())
.pipe(minifyCSS())
.pipe(rename('styles.min.css'))
.pipe(gulp.dest('public/css'))
});
gulp.task('scripts', function() {
return gulp.src('dev/js/*.js')
.pipe(rename('scripts.min.js'))
.pipe(uglify())
.pipe(gulp.dest('public/js'));
});
gulp.task('build', function() {
return gulp.src('dev/components/**')
.pipe(concat('components.min.js'))
.pipe(react())
.pipe(uglify())
.pipe(gulp.dest('public/js'))
});
// Default Task
gulp.task('default', function() {
gulp.start('less');
gulp.start('scripts');
gulp.start('build');
});
gulp.task('scripts-watch', ['scripts'], browserSync.reload);
gulp.task('less-watch', ['less'], browserSync.reload);
gulp.task('build-watch', ['build'], browserSync.reload);
gulp.task('sync', ['scripts'], function() {
browserSync.init({
server: {
baseDir: "public",
index : "index.htm"
}
});
gulp.watch('dev/js/*.js', ['scripts-watch', browserSync.reload]);
gulp.watch('dev/less/*.less', ['less-watch', browserSync.reload]);
gulp.watch('dev/components/**', ['build-watch', browserSync.reload]);
});
Using chrome on ubuntu if that matters.