-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from browserify/dep-event
add 'dep' event to transforms
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ function Deps (opts) { | |
this.pkgFileCache = {}; | ||
this.pkgFileCachePending = {}; | ||
this._emittedPkg = {}; | ||
this._transformDeps = {}; | ||
this.visited = {}; | ||
this.walking = {}; | ||
this.entries = []; | ||
|
@@ -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)); | ||
} | ||
|
@@ -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); | ||
}); | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
goto-bus-stop
Author
Member
|
||
// dependencies emitted by transforms | ||
if (self._transformDeps[file]) deps = deps.concat(self._transformDeps[file]); | ||
return deps; | ||
} | ||
|
||
function fromSource (file, src, pkg, fakePath) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// dependencies added by transform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
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.