Triggering update from gulpfile when file is updated

154 views Asked by At

In my gulpfile, index.js gets processed, pulls in requirements, and spits out bundle.js. The issue is that I need to trigger an update even when requiredfile.js is updated. Here's my code:

var browserify = require('browserify'),
    watchify = require('watchify'),
    gulp = require('gulp'),
    source = require('vinyl-source-stream'),
    sourceFile = './index.js',
    destFolder = './',
    destFile = 'bundle.js';

gulp.task('browserify', function() {
    return browserify(sourceFile, {transform: 'reactify'})
        .bundle()
        .pipe(source(destFile))
        .pipe(gulp.dest(destFolder));
});

gulp.task('watch', function(){
    var bundler = browserify(sourceFile, {
        debug: true,
        cache: {},
        packageCache: {},
        transform: 'reactify'
    });
    var watcher  = watchify(bundler);

    return watcher.on('update', function () { // When any files update
        console.log('Updating!');
        var updateStart = Date.now();
        watcher.bundle()
            .pipe(source(destFile))
            .pipe(gulp.dest(destFolder));
        console.log('Updated!', (Date.now() - updateStart) + 'ms');
    })
        .bundle() // Create the initial bundle when starting the task
        .pipe(source(destFile))
        .pipe(gulp.dest(destFolder));
});

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

How do I add in updating when other files are changed (without causing problems by running requiredfile.js through the same process)?

1

There are 1 answers

0
Linda H On

Figured it out, I added another task to wrap the first, triggered whenever one of the listed files updates.

gulp.task('watchall', function(){
    gulp.watch( ['index.js', 'js/*.js', 'js/**/*.js', 'index.html'], ['browserify']);
});

Update: This is a slower update.