更新文件时从gulpfile触发更新

时间:2015-06-17 11:49:22

标签: javascript gulp browserify watchify

在我的gulpfile中,index.js被处理,提取要求并吐出bundle.js。问题是即使requiredfile.js更新,我也需要触发更新。这是我的代码:

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']);

如何在更改其他文件时添加更新(通过同一过程运行requiredfile.js不会导致问题)?

1 个答案:

答案 0 :(得分:0)

想出来,我添加了另一个任务来包装第一个任务,只要其中一个列出的文件更新就会被触发。

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

更新:这是一个较慢的更新。