Skip to content

Commit 53dd1bf

Browse files
jgozJohn Gozde
authored and
John Gozde
committed
Add support for multiple bundle() calls.
- works in watchify - re-adds pipeline hooks on 'reset' - add opts.outputs alias for opts.o + docs
1 parent 27fb386 commit 53dd1bf

File tree

3 files changed

+84
-39
lines changed

3 files changed

+84
-39
lines changed

Diff for: index.js

+44-37
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,54 @@ module.exports = function f (b, opts) {
2424

2525
var needRecords = !files.length;
2626

27+
opts.outputs = opts.outputs || opts.o;
2728
opts.objectMode = true;
2829
opts.raw = true;
2930
opts.rmap = {};
3031

31-
b.pipeline.get('record').push(through.obj(function(row, enc, next) {
32-
if (needRecords) {
33-
files.push(row.file);
34-
}
35-
next(null, row);
36-
}, function(next) {
37-
var cwd = defined(opts.basedir, b._options.basedir, process.cwd());
38-
var fileMap = files.reduce(function (acc, x, ix) {
39-
acc[path.resolve(cwd, x)] = opts.o[ix];
40-
return acc;
41-
}, {});
42-
43-
// Force browser-pack to wrap the common bundle
44-
b._bpack.hasExports = true;
45-
var packOpts = xtend(b._options, {
46-
raw: true,
47-
hasExports: true
48-
});
49-
50-
var s = createStream(files, opts);
51-
s.on('stream', function (bundle) {
52-
var output = fileMap[bundle.file];
53-
var ws = isStream(output) ? output : fs.createWriteStream(output);
54-
55-
bundle.pipe(pack(packOpts)).pipe(ws);
56-
});
57-
58-
b.pipeline.get('pack').unshift(s);
59-
60-
next();
61-
}));
62-
63-
64-
b.pipeline.get('label').push(through.obj(function(row, enc, next) {
65-
opts.rmap[row.id] = row.file;
66-
next(null, row);
67-
}));
32+
b.on('reset', addHooks);
33+
addHooks();
34+
35+
function addHooks() {
36+
b.pipeline.get('record').push(through.obj(function(row, enc, next) {
37+
if (needRecords) {
38+
files.push(row.file);
39+
}
40+
next(null, row);
41+
}, function(next) {
42+
var cwd = defined(opts.basedir, b._options.basedir, process.cwd());
43+
var fileMap = files.reduce(function (acc, x, ix) {
44+
acc[path.resolve(cwd, x)] = opts.outputs[ix];
45+
return acc;
46+
}, {});
47+
48+
// Force browser-pack to wrap the common bundle
49+
b._bpack.hasExports = true;
50+
var packOpts = xtend(b._options, {
51+
raw: true,
52+
hasExports: true
53+
});
54+
55+
var s = createStream(files, opts);
56+
s.on('stream', function (bundle) {
57+
var output = fileMap[bundle.file];
58+
var ws = isStream(output) ? output : fs.createWriteStream(output);
59+
60+
bundle.pipe(pack(packOpts)).pipe(ws);
61+
});
62+
63+
b.pipeline.get('pack').unshift(s);
64+
65+
if (needRecords) files = [];
66+
67+
next();
68+
}));
69+
70+
b.pipeline.get('label').push(through.obj(function(row, enc, next) {
71+
opts.rmap[row.id] = row.file;
72+
next(null, row);
73+
}));
74+
}
6875

6976
return b;
7077

Diff for: readme.markdown

+7-2
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,14 @@ The output format for `fr` and each of the `fr` sub-streams given by each
129129
`'stream'` event is also in the
130130
[module-deps](https://npmjs.org/package/module-deps) format.
131131

132-
`opts.o` should be an array that pairs up with the `files` array to specify
132+
`opts.o` or `opts.outputs` should be an array that pairs up with the `files` array to specify
133133
where each bundle output for each entry file should be written. The elements in
134-
`opts.o` can be string filenames or writable streams.
134+
`opts.o` can be string filenames, writable streams,
135+
or functions that return filenames or streams. Passing functions that return
136+
streams may be useful when calling `b.bundle()` multiple times.
137+
138+
`opts.entries` or `opts.e` is an optional array of entries that may be used
139+
instead of passing entries to browserify.
135140

136141
The files held in common among `> opts.threshold` (default: 1) bundles will be
137142
output on the `fr` stream itself. The entry-specific bundles are diverted into

Diff for: test/plugin.js

+33
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,36 @@ test('browserify plugin streams', function(t) {
6666
} } });
6767
}));
6868
});
69+
70+
test('browserify plugin multiple bundle calls', function(t) {
71+
t.plan(4);
72+
73+
var b = browserify(files);
74+
var sources = {};
75+
b.plugin(factor, {
76+
o: [
77+
function() { return concat(function(data) { sources.x = data }); },
78+
function() { return concat(function(data) { sources.y = data }); }
79+
]
80+
});
81+
82+
b.bundle().pipe(concat(function(data) {
83+
checkBundle(data);
84+
85+
b.bundle().pipe(concat(checkBundle));
86+
}));
87+
88+
function checkBundle(data) {
89+
var common = data.toString('utf8');
90+
var x = sources.x.toString('utf8');
91+
var y = sources.y.toString('utf8');
92+
93+
vm.runInNewContext(common + x, { console: { log: function (msg) {
94+
t.equal(msg, 55500);
95+
} } });
96+
97+
vm.runInNewContext(common + y, { console: { log: function (msg) {
98+
t.equal(msg, 333);
99+
} } });
100+
}
101+
});

0 commit comments

Comments
 (0)