Skip to content

Commit

Permalink
Add support for multiple bundle() calls.
Browse files Browse the repository at this point in the history
- works in watchify
- re-adds pipeline hooks on 'reset'
- add opts.outputs alias for opts.o + docs
  • Loading branch information
jgoz authored and John Gozde committed Sep 11, 2014
1 parent 27fb386 commit c86968f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 38 deletions.
81 changes: 44 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,54 @@ module.exports = function f (b, opts) {

var needRecords = !files.length;

opts.outputs = opts.outputs || opts.o;
opts.objectMode = true;
opts.raw = true;
opts.rmap = {};

b.pipeline.get('record').push(through.obj(function(row, enc, next) {
if (needRecords) {
files.push(row.file);
}
next(null, row);
}, function(next) {
var cwd = defined(opts.basedir, b._options.basedir, process.cwd());
var fileMap = files.reduce(function (acc, x, ix) {
acc[path.resolve(cwd, x)] = opts.o[ix];
return acc;
}, {});

// Force browser-pack to wrap the common bundle
b._bpack.hasExports = true;
var packOpts = xtend(b._options, {
raw: true,
hasExports: true
});

var s = createStream(files, opts);
s.on('stream', function (bundle) {
var output = fileMap[bundle.file];
var ws = isStream(output) ? output : fs.createWriteStream(output);

bundle.pipe(pack(packOpts)).pipe(ws);
});

b.pipeline.get('pack').unshift(s);

next();
}));


b.pipeline.get('label').push(through.obj(function(row, enc, next) {
opts.rmap[row.id] = row.file;
next(null, row);
}));
b.on('reset', addHooks);
addHooks();

function addHooks() {
b.pipeline.get('record').push(through.obj(function(row, enc, next) {
if (needRecords) {
files.push(row.file);
}
next(null, row);
}, function(next) {
var cwd = defined(opts.basedir, b._options.basedir, process.cwd());
var fileMap = files.reduce(function (acc, x, ix) {
acc[path.resolve(cwd, x)] = opts.outputs[ix];
return acc;
}, {});

// Force browser-pack to wrap the common bundle
b._bpack.hasExports = true;
var packOpts = xtend(b._options, {
raw: true,
hasExports: true
});

var s = createStream(files, opts);
s.on('stream', function (bundle) {
var output = fileMap[bundle.file];
var ws = isStream(output) ? output : fs.createWriteStream(output);

bundle.pipe(pack(packOpts)).pipe(ws);
});

b.pipeline.get('pack').unshift(s);

if (needRecords) files = [];

next();
}));

b.pipeline.get('label').push(through.obj(function(row, enc, next) {
opts.rmap[row.id] = row.file;
next(null, row);
}));
}

return b;

Expand Down
5 changes: 4 additions & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ The output format for `fr` and each of the `fr` sub-streams given by each
`'stream'` event is also in the
[module-deps](https://npmjs.org/package/module-deps) format.

`opts.o` should be an array that pairs up with the `files` array to specify
`opts.o` or `opts.outputs` should be an array that pairs up with the `files` array to specify
where each bundle output for each entry file should be written. The elements in
`opts.o` can be string filenames or writable streams.

`opts.entries` or `opts.e` is an optional array of entries that may be used
instead of passing entries to browserify.

The files held in common among `> opts.threshold` (default: 1) bundles will be
output on the `fr` stream itself. The entry-specific bundles are diverted into
each `'stream'` event's output. `opts.threshold` can be a number or a function
Expand Down
33 changes: 33 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,36 @@ test('browserify plugin streams', function(t) {
} } });
}));
});

test('browserify plugin multiple bundle calls', function(t) {
t.plan(4);

var b = browserify(files);
var sources = {};
b.plugin(factor, {
o: [
function() { return concat(function(data) { sources.x = data }); },
function() { return concat(function(data) { sources.y = data }); }
]
});

b.bundle().pipe(concat(function(data) {
checkBundle(data);

b.bundle().pipe(concat(checkBundle));
}));

function checkBundle(data) {
var common = data.toString('utf8');
var x = sources.x.toString('utf8');
var y = sources.y.toString('utf8');

vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, 55500);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, 333);
} } });
}
});

0 comments on commit c86968f

Please sign in to comment.