Skip to content

API enhancements #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 55 additions & 36 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,47 +25,65 @@ module.exports = function f (b, opts) {

var needRecords = !files.length;

opts.outputs = defined(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();
}));

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

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 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;

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

var s = createStream(files, opts);
s.on('stream', function (bundle) {
bundle.pipe(pipelines[bundle.file]);
});

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
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
16 changes: 15 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 Expand Up @@ -161,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
34 changes: 34 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,37 @@ test('browserify plugin streams', function(t) {
} } });
}));
});

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

var b = browserify(files);
var sources = {};
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);

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);
} } });
}
});