Skip to content

Commit

Permalink
Merge pull request #141 from browserify/dep-event
Browse files Browse the repository at this point in the history
add 'dep' event to transforms
  • Loading branch information
goto-bus-stop authored Feb 7, 2018
2 parents 41a126b + 6ddd5a9 commit 9fe46d5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
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);

This comment has been minimized.

Copy link
@mattdesl

mattdesl Mar 26, 2018

Hey @goto-bus-stop I've noticed in latest browserify I'm getting a build error (stops dev server / watchify) with this commit. It happens when using babel + babelify and getting a regular build error, like having a duplicate cocnst declaration.

Error: Parsing file /projects/2017/foobar/client/test.js: Identifier 'box' has already been declared (51:6)
/projects/2017/foobar/node_modules/module-deps/index.js:438
        if (self._transformDeps[file]) deps = deps.concat(self._transformDeps[file]);
                                                   ^

TypeError: Cannot read property 'concat' of undefined
    at getDeps (/projects/2017/foobar/node_modules/module-deps/index.js:438:52)
    at /projects/2017/foobar/node_modules/module-deps/index.js:420:32
    at ConcatStream.<anonymous> (/projects/2017/foobar/node_modules/concat-stream/index.js:37:43)
    at emitNone (events.js:111:20)
    at ConcatStream.emit (events.js:208:7)
    at finishMaybe (/projects/2017/foobar/node_modules/readable-stream/lib/_stream_writable.js:620:14)
    at endWritable (/projects/2017/foobar/node_modules/readable-stream/lib/_stream_writable.js:628:3)
    at ConcatStream.Writable.end (/projects/2017/foobar/node_modules/readable-stream/lib/_stream_writable.js:584:41)
    at DuplexWrapper.onend (/projects/2017/foobar/node_modules/readable-stream/lib/_stream_readable.js:577:10)
    at Object.onceWrapper (events.js:313:30)

This comment has been minimized.

Copy link
@goto-bus-stop

goto-bus-stop Mar 26, 2018

Author Member

Oh thanks, that makes sense. Spotted a few places where this commit messed up :)

This comment has been minimized.

Copy link
@goto-bus-stop

goto-bus-stop Mar 26, 2018

Author Member
// 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 }

0 comments on commit 9fe46d5

Please sign in to comment.