Skip to content

Commit

Permalink
Expose pipeline for output bundles on event.
Browse files Browse the repository at this point in the history
- browserify instance emits ‘factor.pipeline’ event when the pipeline
  is created
- pipeline uses labeled-stream-splicer and has ‘pack’ and ‘wrap’ stages
  • Loading branch information
jgoz authored and John Gozde committed Sep 11, 2014
1 parent c86968f commit bec04df
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
34 changes: 23 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var fs = require('fs');
var pack = require('browser-pack');
var xtend = require('xtend');
var defined = require('defined');
var splicer = require('labeled-stream-splicer');

module.exports = function f (b, opts) {
if (!opts) opts = {};
Expand All @@ -24,40 +25,51 @@ module.exports = function f (b, opts) {

var needRecords = !files.length;

opts.outputs = opts.outputs || opts.o;
opts.outputs = defined(opts.outputs, opts.o, {});
opts.objectMode = true;
opts.raw = true;
opts.rmap = {};

var packOpts = xtend(b._options, {
raw: true,
hasExports: true
});

b.on('reset', addHooks);
addHooks();

function 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];
var pipelines = files.reduce(function (acc, x, ix) {
var pipeline = splicer.obj([
'pack', [ pack(packOpts) ],
'wrap', []
]);
var output = opts.outputs[ix];
if (output) {
var ws = isStream(output) ? output : fs.createWriteStream(output);
pipeline.push(ws);
}
acc[path.resolve(cwd, x)] = pipeline;
return acc;
}, {});

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

Object.keys(pipelines).forEach(function (id) {
b.emit('factor.pipeline', id, pipelines[id]);
});

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);
bundle.pipe(pipelines[bundle.file]);
});

b.pipeline.get('pack').unshift(s);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"defined": "0.0.0",
"deps-topo-sort": "~0.2.1",
"inherits": "^2.0.1",
"labeled-stream-splicer": "^1.0.0",
"minimist": "~0.2.0",
"nub": "0.0.0",
"reversepoint": "~0.2.0",
Expand Down
11 changes: 11 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ default).

The entry file name is available as `stream.file`.

## b.on('factor.pipeline', function (file, pipeline) {})

Emits the full path to the entry file (`file`) and a [labeled-stream-splicer](https://npmjs.org/package/labeled-stream-splicer) (`pipeline`) for each entry file with these labels:

* `'pack'` - [browser-pack](https://npmjs.org/package/browser-pack)
* `'wrap'` - apply final wrapping

You can call `pipeline.get` with a label name to get a handle on a stream pipeline that you can `push()`, `unshift()`, or `splice()` to insert your own transform streams.

Event handlers must be attached *before* calling `b.plugin`.

# install

With [npm](https://npmjs.org) do:
Expand Down
11 changes: 6 additions & 5 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ test('browserify plugin multiple bundle calls', function(t) {

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.on('factor.pipeline', function(id, pipeline) {
pipeline.pipe(concat(function(data) {
if (/x\.js$/.test(id)) sources.x = data;
else sources.y = data;
}));
});
b.plugin(factor);

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

0 comments on commit bec04df

Please sign in to comment.