Watchify detecting changes but output doesn't change

678 Views Asked by At

I have the following gulpfile

gulp.task('browserify', function() {
    bundle(false);
});

gulp.task('browserify-watch', function() {
    bundle(true);
});

function bundle (performWatch) {
    var bify = (performWatch === true
        ? watchify(browserify(finalBrowserifyOptions))
        : browserify(finalBrowserifyOptions));

    if (performWatch) {
        bify.on('update', function () {
            console.log('Updating project files...');
            rebundle(bify);
        });
    }

    bify.transform(babelify.configure({
        compact: false
    }));

    function rebundle(bify) {
        return bify.bundle()
            .on('error', function () {
                plugins.util.log('Browserify error');
            })
            .pipe(source('bundle.js'))
            .pipe(buffer())
            .pipe(plugins.sourcemaps.init({loadMaps: true}))
            .pipe(plugins.sourcemaps.write('./')) 
            .pipe(gulp.dest(paths.build + assets.js));
    }

    return rebundle(bify);
}

The trouble is that gulp browserify works just fine. However, gulp browserify-watch detects the changes but the output never gets updated.

What am I doing wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

Looks like everything was fine my IDE, Webstorm, was caching the file. When I looked at the actual file on the box it was fine.

0
On

I encountered the same watchify failures in file-change detection (both with gulp and grunt). CLI watchify works as expected though. For example:

watchify ./src/app.js -t babelify --outfile ./build/bundle.js -v

Currently I switched to browserify-incremental. The perspective is different in regards to watchify approach. Here's the paragraph from their Github page which encopasses it best:

browserify-incremental can detect changes which occured in between runs, which means it can be used as part of build systems which are invoked on demand, without requiring a long lived process. Whereas watchify is slow for the first run upon each startup, browserify-incremental is fast every time after the very first.

Here's the "translated" CLI command to make use of browserify-incremental:

browserifyinc ./src/app.js -t babelify --outfile ./build/bundle.js -v

Here's a simple gulpfile.js script for watching and [re]bundling:

var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var babel = require('babelify');
var browserifyInc = require('browserify-incremental');

var bundleApp = function() {
  var browserifyObj = browserify('./src/app.js', { debug: false, cache: {}, packageCache: {}, fullPaths: true }).transform(babel);
  var bundler = browserifyInc(browserifyObj,  {cacheFile: './browserify-cache.json'});

  bundler.on('time', function (time) {
    console.log('-> Done in ' + time/1000 + ' ms');
  });

  console.log('-> Bundling...');

  bundler.bundle()
    .on('error', function(err) { console.error(err); this.emit('end'); })
    .pipe(source('bundle.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({ loadMaps: true }))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build'));
};


gulp.task('browserify', function () {
  gulp.watch('./src/**/*.js', function () {
    return bundleApp();
  });
});


gulp.task('default', ['browserify']);