I have run into a problem with some weird interplay between watchify and gulp.watch. When we use watchify with polling, which I need to do because it runs in a vagrant environment, then the gulp.watch task only fires the first time the files are changed.
gulpfile:
var gulp = require('gulp');
var util = require('gulp-util');
var eslint = require('gulp-eslint');
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
function bundle(bundler) {
return bundler
.bundle()
.on('error', (error) => util.log(util.colors.red('Error compiling scripts:'), error.message))
.pipe(source('./app.js'))
.pipe(gulp.dest('./'));
}
gulp.task('default', ['build', 'watch']);
gulp.task('build', ['lint', 'scripts']);
gulp.task('lint', function() {
gulp.src('./src/**/*.js')
.pipe(eslint());
});
gulp.task('scripts', ['lint'], function() {
var bundler = browserify({
entries: ['./src/app.js'],
cache: {},
packageCache: {},
fullPaths: true,
});
var watcher = watchify(bundler, {poll: true});
watcher.on( 'update', () => {
util.log( 'Now Updating Scripts...');
return bundle(watcher);
});
return bundle(watcher);
})
gulp.task('watch', ['build'], function() {
gulp.watch(['./src/**/*.js'], ['lint']);
})
Sample repository here: https://github.com/mudetroit/watchify-gulp-watch-issue
When you run gulp and change the file twice after the initial compile you see the following.
[12:02:49] Using gulpfile ~/repo/watchify-gulp-watch-issue/gulpfile.js
[12:02:49] Starting 'lint'...
[12:02:49] Finished 'lint' after 24 ms
[12:02:49] Starting 'scripts'...
[12:02:49] Finished 'scripts' after 98 ms
[12:02:49] Starting 'build'...
[12:02:49] Finished 'build' after 6.17 μs
[12:02:49] Starting 'watch'...
[12:02:49] Finished 'watch' after 8.41 ms
[12:02:49] Starting 'default'...
[12:02:49] Finished 'default' after 11 μs
[12:03:48] Starting 'lint'...
[12:03:48] Finished 'lint' after 3.87 ms
[12:03:48] Now Updating Scripts...
[12:03:55] Now Updating Scripts...
A couple of items worth noting, if I don't use watchify and do full recompiles it works just fine, but this is to slow to be useful in practice. If I take the poll off it works fine, but this stops it from working in the vagrant environment with NFS. Been trying to understand what is going on here all morning and not getting anywhere so hoping I can get some help.