browserify / factor-bundle

factor browser-pack bundles into common shared bundles
Other
400 stars 27 forks source link

Support piping factored bundles to a stream. #25

Closed terinjokes closed 10 years ago

terinjokes commented 10 years ago

If using factor-bundle as a Browserify plugin via the API, support piping the factored bundles to a stream, in addition to the current write-to-fs approach.

This allows factor-bundle to more easily utilized within streaming environments.

var level = require('level');
var Store = require('level-store');
var browserify = require('browserify');
var factor = require('factor-bundle');

var store = Store(Level('/tmp/bundles'));

var b = browserify(['./entries/a.js', './entries/b.js']);
b.plugin(factor({
  o: [ store.createWriteStream('a'), store.createWriteStream('b') ]
}));
b.bundle().pipe(store.createWriteStream('common'));

While I'm not sure of the practicality of this example, it seemed like a fun little thing to write.

ghost commented 10 years ago

Merged in 2.1.0.

jgoz commented 10 years ago

@terinjokes

While I'm not sure of the practicality of this example, it seemed like a fun little thing to write.

How's this for a practical example? Usage with gulp:

var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var factor = require('factor-bundle');
var gulp = require('gulp');
var merge = require('multistream-merge');
var source = require('vinyl-source-stream');

gulp.task('bundle', function() {
  var b = browserify(['./entries/a.js', './entries/b.js']);
  var o = [ source('a.js'), source('b.js') ];
  b.plugin(factor({ o: o }));
  var common = b.bundle().pipe(source('common.js'));

  return merge.obj(o.concat(common))
    .pipe(buffer()) // not necessary if going straight to gulp.dest
    // ...do some kind of post-processing
    // extract source-maps, etc...
    .pipe(gulp.dest('out'));
});

In conclusion, great change!

terinjokes commented 10 years ago

@jgoz Which do you think was the real reason for introducing this change? :wink:

jgoz commented 10 years ago

I had an inkling :smiley:

honi commented 9 years ago

So how would you leverage @jgoz solution with watchify?