Gulp with watchify/browserify runs twice then stops watching

4.2k views Asked by At

Here is my gulpfile.js

var gulp = require('gulp');
var browserify = require('browserify');
var source = require("vinyl-source-stream");
var reactify = require("reactify");
var watchify = require('watchify');
var gutil = require('gulp-util');

var paths = {
    scripts: ['src/jsx/index.jsx']
};

gulp.task('browserify', function(){


    var bundler = watchify(browserify('./src/jsx/index.jsx', watchify.args));
    bundler.transform(reactify);

    bundler.on('update', rebundle);

    function rebundle() {
        return bundler.bundle()
            // log errors if they happen
            .on('error', gutil.log.bind(gutil, 'Browserify Error'))
            .pipe(source('bundle.js'))
            .pipe(gulp.dest('./public/js'));
    }

    return rebundle();

});

gulp.task('watch', function() {
    gulp.watch(paths.scripts, ['browserify']);
});

Then my commandline output looks like this:

$ gulp watch

[15:10:41] Using gulpfile ~/blizztrack/dashboard/gulpfile.js
[15:10:41] Starting 'watch'...
[15:10:41] Finished 'watch' after 9.95 ms

save index.jsx

[15:10:45] Starting 'browserify'...
[15:10:51] Finished 'browserify' after 5.33 s

save index.jsx the second time

[15:11:08] Starting 'browserify'...
[15:11:10] Finished 'browserify' after 2.02 s

save index.jsx the third time

No output.

This seems to be doing exactly what I want it to the first two times, and then it just stops watching. Can anyone point me in the right direction?

5

There are 5 answers

0
thealexbaron On BEST ANSWER

Here's what my new working gulp file looks like. Hasn't given me any problems and accomplishes the same thing. Pretty sure @Ben was correct - gulp.watch and watchify were conflicting.

var gulp = require('gulp');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var watchify = require('watchify');
var reactify = require('reactify');
var chalk = require('chalk');
var gcallback = require('gulp-callback');
var moment = require('moment');
var gutil = require('gutil');

var jsDest = '../../server/webship/html/html/static/js';
var viewsDir = './js/views';

var watchifyArgs = watchify.args;
watchifyArgs.debug = true;

var bundler = watchify(browserify('./js/views/main.jsx', watchifyArgs));
// add any other browserify options or transforms here
bundler.transform(reactify);

bundler.on('time', function (time) {
    var durationOfBundleBuild = moment.duration(time).asSeconds();
    console.log(chalk.green('Updated'), ' bundle in ', chalk.bold(durationOfBundleBuild + 's'), '\n');
});

gulp.task('watch', function() {

    bundle(true);

    bundler.on('update', function() {
        console.log('updating...');
        bundle(true);
    });
});

gulp.task('build', function() {
    bundle();
    bundler.close();
});

function bundle(watching) {

    console.log(chalk.yellow('Updating') + ' bundle...');

    bundler.bundle()
        .on('error', gutil.log.bind(gutil, 'Browserify Error'))
        .pipe(source('bundle.js'))
        .pipe(gulp.dest(jsDest))
        .pipe(gcallback(function() {

            if (!watching) {
                process.nextTick(function() {
                    process.exit(0);
                });
            }
        }));
}
0
Ben On

I think watchify and gulp.watch are probably conflicting when they both try to rebuild your bundle. I would remove the watch task, and just use the browserify task directly.

2
Jeremy On

Just came across this issue. After browsing the web for answers for a couple hours I tried editing the file in Notepad instead of PHPStorm IDE. It turned out that browserify/watchify continued to work via notepad.

Thats when I tried to play around with some settings. It turned out PHPStorm / System Settings / Use "safe write", if enabled, stops watchify after PHPStorm edits the file.

All the problems magically disappeared when i turned "safe write" off. I am not sure what IDE you are using, but maybe there is a "Safe Write" option too.

1
mike123 On

It wasn't obvious for me either when I first used watchify to speed-up browserify js compile time. When using watchify gulp.watch no longer required. To see the compile/build time in the output/tasks window you can do the following (note the gutil.log command):



    var gulp = require('gulp');
    var gutil = require('gulp-util');
    var source = require('vinyl-source-stream');
    var buffer = require('vinyl-buffer');
    var watchify = require('watchify');
    var browserify = require('browserify');

    var bundler = watchify(browserify('./src/app.js', watchify.args));
    gulp.task('browserify', bundle);

    bundler.on('update', bundle); 

    function bundle() {
        var start = new Date().getTime();

        var b = bundler.bundle()
          .on('error', gutil.log.bind(gutil, 'Browserify Error'))
          .pipe(source('bundle.js'))
          .pipe(buffer())
          .pipe(gulp.dest('./src/dist'));

        var end = new Date().getTime();
        var time = end - start;

        gutil.log('[browserify]', 'rebundle took ', gutil.colors.cyan(time + ' ms'));
        return b;
    }

See if that helps.

0
kellen On

If you're using vim and run in to this issue, and are not using both watchify and gulp.watch, try doing :set noswapfile, or adding set noswapfile in your .vimrc.

I believe this fix works because vim's scheme of creating and editing a .swp file while you work and then replacing the file with the swap file when you actually save interferes with watchify's ability to receive file system events.

Note that :set noswapfile means your edits are not saved to disk until you actually do :w!