1

此函数的返回需要是一个流。在非手表模式下,这很容易;只需返回重新捆绑,browserify 流被转换,等等等等,好的。但是,在监视模式下,每次更新都会运行重新捆绑,并且每次都会创建一个新流。我需要一种方法来整合所有这些流,因为它们被创建成一个我可以返回的无限流,并且实际上可以在生产线上消耗掉。使用组合流,似乎一旦读取数据,流就不再是可写的,所以这是不行的。任何帮助,将不胜感激!

var bundleify = function(watch) {

    var bundler = (watch?watchify:browserify)('main.js');

    var rebundle = function () {
        return bundler.bundle()
            .on('error', console.log)
            .pipe(source('main.js'))
            .pipe(rename('app.js'))
            .pipe(jsTasks()); // lazypipe with other tasks
    };

    // Regular browserify, just return the stream.
    if (!watch) {
        return rebundle();
    }

    // Watchify, rebundle on update.
    bundler.on('update', function() {
        rebundle();
    });

    // return ????
}
4

1 回答 1

0

这是我想出的解决方案。这是一个非常贫民区(诚然我对流没有很好的理解),但看起来它正在工作。仍然有兴趣找到更好的方法。

var outstream = through2.obj();
var interceptor = function(){
    return through2.obj(function(obj, enc, cb) {
        outstream.push(obj);
        cb()
    });
}

bundler.on('update', function() {
    rebundle().pipe(interceptor());
});

rebundle().pipe(interceptor());
return outstream;
于 2014-07-17T20:44:16.440 回答