Skip to content

Commit 75a8a9d

Browse files
committed
add test for output as function
1 parent 59ad2a8 commit 75a8a9d

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

index.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ module.exports = function f (b, opts) {
2929

3030
var outopt = defined(opts.outputs, opts.output, opts.o);
3131

32-
function moreOutputs (file) {
33-
if (isarray(outopt)) return [];
34-
if (!outopt) return [];
35-
var xopts = { env: xtend(process.env, { FILE: file }) };
36-
return [ outpipe(outopt, xopts) ];
37-
}
38-
3932
opts.objectMode = true;
4033
opts.raw = true;
4134
opts.rmap = {};
@@ -56,20 +49,27 @@ module.exports = function f (b, opts) {
5649
}
5750
next(null, row);
5851
}, function(next) {
59-
if (outopt && !isarray(outopt) && ! typeof outputs === 'function') outopt = [outopt];
60-
var outputs = defined(outopt, []);
61-
62-
if (typeof outputs === 'function') {
63-
outputs = outputs();
64-
} else if (!isarray(outputs) && isStream(outputs)) {
65-
outputs = [ outputs ];
66-
} else if (!isarray(outputs)) {
67-
outputs = [];
68-
} else {
69-
outputs = outputs.map(function (o) {
52+
var outopt = defined(opts.outputs, opts.output, opts.o);
53+
54+
if (typeof outopt === 'function') {
55+
outopt = outopt();
56+
}
57+
58+
var outputs;
59+
if (isarray(outopt)) {
60+
outputs = outopt.map(function (o) {
7061
if (isStream(o)) return o;
71-
else return fs.createWriteStream(o);
62+
return fs.createWriteStream(o);
7263
});
64+
} else {
65+
outputs = [];
66+
}
67+
68+
function moreOutputs (file) {
69+
if (isarray(outopt)) return [];
70+
if (!outopt) return [];
71+
var xopts = { env: xtend(process.env, { FILE: file }) };
72+
return [ outpipe(outopt, xopts) ];
7373
}
7474

7575
var pipelines = files.reduce(function (acc, x, ix) {

readme.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ where OPTIONS are:
140140
141141
-o Output FILE or CMD that maps to a corresponding entry file at the same
142142
index. CMDs are executed with $FILE set to the corresponding input file.
143-
Optionally specify a function that return a writable stream.
143+
Optionally specify a function that returns a valid value for this argument.
144144
145145
-e Entry file to use, overriding the entry files listed in the original
146146
bundle.

test/outputs.js

+26
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ test('stream outputs', function (t) {
7373
}
7474
});
7575

76+
test('function outputs', function (t) {
77+
var tmpdir = tmp() + '/factor-bundle-' + Math.random();
78+
mkdirp.sync(tmpdir);
79+
80+
t.plan(2);
81+
var b = browserify(files);
82+
b.plugin(factor, {
83+
output: function() { return ' cat > ' + tmpdir + '/`basename $FILE`'; }
84+
});
85+
var w = fs.createWriteStream(path.join(tmpdir, 'common.js'));
86+
b.bundle().pipe(w);
87+
88+
w.on('finish', function () {
89+
var common = fs.readFileSync(tmpdir + '/common.js', 'utf8');
90+
var x = fs.readFileSync(tmpdir + '/x.js', 'utf8');
91+
var y = fs.readFileSync(tmpdir + '/y.js', '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+
});
76102

77103
test('bundle twice', function (t) {
78104
var tmpdir = tmp() + '/factor-bundle-' + Math.random();

0 commit comments

Comments
 (0)