Skip to content
Merged
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
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function Deps (opts) {
this.pkgFileCache = {};
this.pkgFileCachePending = {};
this._emittedPkg = {};
this._transformDeps = {};
this.visited = {};
this.walking = {};
this.entries = [];
Expand Down Expand Up @@ -261,6 +262,11 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
trOpts._flags = trOpts.hasOwnProperty('_flags') ? trOpts._flags : self.options;
if (typeof tr === 'function') {
var t = tr(file, trOpts);
// allow transforms to `stream.emit('dep', path)` to add dependencies for this file
self._transformDeps[file] = [];
t.on('dep', function (dep) {
self._transformDeps[file].push(dep);
});
self.emit('transform', t, file);
nextTick(cb, null, wrapTransform(t));
}
Expand Down Expand Up @@ -302,6 +308,11 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
}

var trs = r(file, trOpts);
// allow transforms to `stream.emit('dep', path)` to add dependencies for this file
self._transformDeps[file] = [];
trs.on('dep', function (dep) {
self._transformDeps[file].push(dep);
});
self.emit('transform', trs, file);
cb(null, trs);
});
Expand Down Expand Up @@ -422,7 +433,10 @@ Deps.prototype.walk = function (id, parent, cb) {
});

function getDeps (file, src) {
return rec.noparse ? [] : self.parseDeps(file, src);
var deps = rec.noparse ? [] : self.parseDeps(file, src);
// dependencies emitted by transforms
if (self._transformDeps[file]) deps = deps.concat(self._transformDeps[file]);
return deps;
}

function fromSource (file, src, pkg, fakePath) {
Expand Down
4 changes: 4 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ You don't necessarily need to use the
readable/writable filter stream for transforming file contents, but this is an
easy way to do it.

module-deps looks for `require()` calls and adds their arguments as dependencies
of a file. Transform streams can emit `'dep'` events to include additional
dependencies that are not consumed with `require()`.

When you call `mdeps()` with an `opts.transform`, the transformations you
specify will not be run for any files in node_modules/. This is because modules
you include should be self-contained and not need to worry about guarding
Expand Down
1 change: 1 addition & 0 deletions test/files/transformdeps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// dependencies added by transform
60 changes: 60 additions & 0 deletions test/tr_deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var parser = require('../');
var through = require('through2');
var test = require('tap').test;
var fs = require('fs');
var path = require('path');

var files = {
transformdeps: path.join(__dirname, '/files/transformdeps.js'),
foo: path.join(__dirname, '/files/foo.js'),
bar: path.join(__dirname, '/files/bar.js')
};

var sources = Object.keys(files).reduce(function (acc, file) {
acc[file] = fs.readFileSync(files[file], 'utf8');
return acc;
}, {});

test('deps added by transforms', function (t) {
t.plan(1);
var p = parser();
p.write({ transform: transform, options: {} });
p.end({ file: files.transformdeps, entry: true });
function transform (file) {
if (file === files.transformdeps) return through(function(chunk, enc, cb) {
cb(null, chunk);
}, function (cb) {
this.emit('dep', './foo');
cb();
});
return through();
}

var rows = [];
p.on('data', function (row) { rows.push(row) });
p.on('end', function () {
t.same(rows.sort(cmp), [
{
id: files.transformdeps,
file: files.transformdeps,
source: sources.transformdeps,
entry: true,
deps: { './foo': files.foo }
},
{
id: files.foo,
file: files.foo,
source: sources.foo,
deps: { './bar': files.bar }
},
{
id: files.bar,
file: files.bar,
source: sources.bar,
deps: {}
}
].sort(cmp));
});
});

function cmp (a, b) { return a.id < b.id ? -1 : 1 }