1

在我的 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相同的过程导致问题)?

4

1 回答 1

0

想通了,我添加了另一个任务来包装第一个任务,每当列出的文件更新时触发。

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

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

于 2015-06-17T11:59:14.510 回答