Skip to content

Commit dcce671

Browse files
author
James Halliday
committed
2 parents 558fd0a + a75a19a commit dcce671

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
255255
trOpts = tr[1];
256256
tr = tr[0];
257257
}
258+
trOpts._flags = trOpts.hasOwnProperty('_flags') ? trOpts._flags : self.options;
258259
if (typeof tr === 'function') {
259260
var t = tr(file, trOpts);
260261
self.emit('transform', t, file);

readme.markdown

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ to a module that is expected to follow this format:
150150

151151
``` js
152152
var through = require('through2');
153-
module.exports = function (file) { return through() };
153+
module.exports = function (file, opts) { return through() };
154154
```
155155

156156
You don't necessarily need to use the
@@ -206,6 +206,11 @@ and `ggg` gets `{"y":4}`:
206206
}
207207
```
208208

209+
Options sent to the module-deps constructor are also provided under
210+
`opts._flags`. These options are sometimes required if your transform
211+
needs to do something different when browserify is run in debug mode, for
212+
example.
213+
209214
# usage
210215

211216
```

test/tr_flags.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var through = require('through2');
2+
var mdeps = require('../');
3+
var test = require('tap').test;
4+
5+
test('--debug passed to transforms', function (t) {
6+
var empty = require.resolve('./tr_flags/empty.js');
7+
8+
t.plan(5);
9+
10+
var p
11+
[true, false].forEach(function(debug) {
12+
p = mdeps({
13+
debug: debug,
14+
transform: function (file, opts) {
15+
t.equal(opts._flags.debug, debug, 'debug: ' + debug);
16+
return through();
17+
}
18+
})
19+
p.on('error', function (err) { return t.fail(err.message) })
20+
p.end(empty);
21+
22+
p = mdeps({ debug: debug })
23+
p.write({
24+
transform: function (file, opts) {
25+
t.equal(opts._flags.debug, debug, 'debug: ' + debug);
26+
return through();
27+
},
28+
options: {}
29+
})
30+
p.on('error', function (err) { return t.fail(err.message) })
31+
p.end(empty);
32+
});
33+
34+
p = mdeps({ debug: true })
35+
p.write({
36+
transform: function (file, opts) {
37+
t.equal(opts._flags, Infinity, 'transform arguments are preserved');
38+
return through();
39+
},
40+
options: { _flags: Infinity }
41+
})
42+
p.on('error', function (err) { return t.fail(err.message) })
43+
p.end(empty);
44+
});

test/tr_flags/empty.js

Whitespace-only changes.

0 commit comments

Comments
 (0)