Skip to content

Commit

Permalink
Add detect option for custom dependency detection
Browse files Browse the repository at this point in the history
  • Loading branch information
igorklopov authored and goto-bus-stop committed May 16, 2018
1 parent 27db781 commit 2dcc339
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function Deps (opts) {
this.transforms = [].concat(opts.transform).filter(Boolean);
this.globalTransforms = [].concat(opts.globalTransform).filter(Boolean);
this.resolver = opts.resolve || browserResolve;
this.detective = opts.detect || detective;
this.options = xtend(opts);
if (!this.options.modules) this.options.modules = {};

Expand Down Expand Up @@ -493,6 +494,7 @@ Deps.prototype.walk = function (id, parent, cb) {
};

Deps.prototype.parseDeps = function (file, src, cb) {
var self = this;
if (this.options.noParse === true) return [];
if (/\.json$/.test(file)) return [];

Expand All @@ -501,7 +503,7 @@ Deps.prototype.parseDeps = function (file, src, cb) {
return [];
}

try { var deps = detective(src) }
try { var deps = self.detective(src) }
catch (ex) {
var message = ex && ex.message ? ex.message : ex;
throw new Error(
Expand Down
4 changes: 4 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ package.json at all.
`opts.resolve(id, parent, cb)` signature that
[browser-resolve](https://github.com/shtylman/node-browser-resolve) has

* `opts.detect` - a custom dependency detection function. `opts.detect(source)`
should return an array of dependency module names. By default
[detective](https://github.com/browserify/detective) is used.

* `opts.filter` - a function (id) to skip resolution of some module `id` strings.
If defined, `opts.filter(id)` should return truthy for all the ids to include
and falsey for all the ids to skip.
Expand Down
32 changes: 32 additions & 0 deletions test/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var parser = require('../');
var test = require('tap').test;
var JSONStream = require('JSONStream');
var packer = require('browser-pack');
var path = require('path');

test('detect', function (t) {
t.plan(1);
var p = parser({
detect: function (source) {
var rx = /require\(["'](.*?)["']\)/g;
var m, deps = [];
while (m = rx.exec(source)) {
deps.push(m[1]);
}
return deps;
}
});
p.end(path.join(__dirname, '/files/main.js'));
p.on('error', t.fail.bind(t));
var pack = packer();

p.pipe(JSONStream.stringify()).pipe(pack);

var src = '';
pack.on('data', function (buf) { src += buf });
pack.on('end', function () {
Function('console', src)({
log: function (s) { t.equal(s, 'main: 1055') }
});
});
});

0 comments on commit 2dcc339

Please sign in to comment.