Skip to content

Commit 9fe46d5

Browse files
Merge pull request #141 from browserify/dep-event
add 'dep' event to transforms
2 parents 41a126b + 6ddd5a9 commit 9fe46d5

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function Deps (opts) {
3838
this.pkgFileCache = {};
3939
this.pkgFileCachePending = {};
4040
this._emittedPkg = {};
41+
this._transformDeps = {};
4142
this.visited = {};
4243
this.walking = {};
4344
this.entries = [];
@@ -261,6 +262,11 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
261262
trOpts._flags = trOpts.hasOwnProperty('_flags') ? trOpts._flags : self.options;
262263
if (typeof tr === 'function') {
263264
var t = tr(file, trOpts);
265+
// allow transforms to `stream.emit('dep', path)` to add dependencies for this file
266+
self._transformDeps[file] = [];
267+
t.on('dep', function (dep) {
268+
self._transformDeps[file].push(dep);
269+
});
264270
self.emit('transform', t, file);
265271
nextTick(cb, null, wrapTransform(t));
266272
}
@@ -302,6 +308,11 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
302308
}
303309

304310
var trs = r(file, trOpts);
311+
// allow transforms to `stream.emit('dep', path)` to add dependencies for this file
312+
self._transformDeps[file] = [];
313+
trs.on('dep', function (dep) {
314+
self._transformDeps[file].push(dep);
315+
});
305316
self.emit('transform', trs, file);
306317
cb(null, trs);
307318
});
@@ -422,7 +433,10 @@ Deps.prototype.walk = function (id, parent, cb) {
422433
});
423434

424435
function getDeps (file, src) {
425-
return rec.noparse ? [] : self.parseDeps(file, src);
436+
var deps = rec.noparse ? [] : self.parseDeps(file, src);
437+
// dependencies emitted by transforms
438+
if (self._transformDeps[file]) deps = deps.concat(self._transformDeps[file]);
439+
return deps;
426440
}
427441

428442
function fromSource (file, src, pkg, fakePath) {

readme.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ You don't necessarily need to use the
203203
readable/writable filter stream for transforming file contents, but this is an
204204
easy way to do it.
205205

206+
module-deps looks for `require()` calls and adds their arguments as dependencies
207+
of a file. Transform streams can emit `'dep'` events to include additional
208+
dependencies that are not consumed with `require()`.
209+
206210
When you call `mdeps()` with an `opts.transform`, the transformations you
207211
specify will not be run for any files in node_modules/. This is because modules
208212
you include should be self-contained and not need to worry about guarding

test/files/transformdeps.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// dependencies added by transform

test/tr_deps.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var parser = require('../');
2+
var through = require('through2');
3+
var test = require('tap').test;
4+
var fs = require('fs');
5+
var path = require('path');
6+
7+
var files = {
8+
transformdeps: path.join(__dirname, '/files/transformdeps.js'),
9+
foo: path.join(__dirname, '/files/foo.js'),
10+
bar: path.join(__dirname, '/files/bar.js')
11+
};
12+
13+
var sources = Object.keys(files).reduce(function (acc, file) {
14+
acc[file] = fs.readFileSync(files[file], 'utf8');
15+
return acc;
16+
}, {});
17+
18+
test('deps added by transforms', function (t) {
19+
t.plan(1);
20+
var p = parser();
21+
p.write({ transform: transform, options: {} });
22+
p.end({ file: files.transformdeps, entry: true });
23+
function transform (file) {
24+
if (file === files.transformdeps) return through(function(chunk, enc, cb) {
25+
cb(null, chunk);
26+
}, function (cb) {
27+
this.emit('dep', './foo');
28+
cb();
29+
});
30+
return through();
31+
}
32+
33+
var rows = [];
34+
p.on('data', function (row) { rows.push(row) });
35+
p.on('end', function () {
36+
t.same(rows.sort(cmp), [
37+
{
38+
id: files.transformdeps,
39+
file: files.transformdeps,
40+
source: sources.transformdeps,
41+
entry: true,
42+
deps: { './foo': files.foo }
43+
},
44+
{
45+
id: files.foo,
46+
file: files.foo,
47+
source: sources.foo,
48+
deps: { './bar': files.bar }
49+
},
50+
{
51+
id: files.bar,
52+
file: files.bar,
53+
source: sources.bar,
54+
deps: {}
55+
}
56+
].sort(cmp));
57+
});
58+
});
59+
60+
function cmp (a, b) { return a.id < b.id ? -1 : 1 }

0 commit comments

Comments
 (0)