Skip to content

Commit 2dcc339

Browse files
igorklopovgoto-bus-stop
authored andcommitted
Add detect option for custom dependency detection
1 parent 27db781 commit 2dcc339

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function Deps (opts) {
5858
this.transforms = [].concat(opts.transform).filter(Boolean);
5959
this.globalTransforms = [].concat(opts.globalTransform).filter(Boolean);
6060
this.resolver = opts.resolve || browserResolve;
61+
this.detective = opts.detect || detective;
6162
this.options = xtend(opts);
6263
if (!this.options.modules) this.options.modules = {};
6364

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

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

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

504-
try { var deps = detective(src) }
506+
try { var deps = self.detective(src) }
505507
catch (ex) {
506508
var message = ex && ex.message ? ex.message : ex;
507509
throw new Error(

readme.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ package.json at all.
7272
`opts.resolve(id, parent, cb)` signature that
7373
[browser-resolve](https://github.com/shtylman/node-browser-resolve) has
7474

75+
* `opts.detect` - a custom dependency detection function. `opts.detect(source)`
76+
should return an array of dependency module names. By default
77+
[detective](https://github.com/browserify/detective) is used.
78+
7579
* `opts.filter` - a function (id) to skip resolution of some module `id` strings.
7680
If defined, `opts.filter(id)` should return truthy for all the ids to include
7781
and falsey for all the ids to skip.

test/detect.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var parser = require('../');
2+
var test = require('tap').test;
3+
var JSONStream = require('JSONStream');
4+
var packer = require('browser-pack');
5+
var path = require('path');
6+
7+
test('detect', function (t) {
8+
t.plan(1);
9+
var p = parser({
10+
detect: function (source) {
11+
var rx = /require\(["'](.*?)["']\)/g;
12+
var m, deps = [];
13+
while (m = rx.exec(source)) {
14+
deps.push(m[1]);
15+
}
16+
return deps;
17+
}
18+
});
19+
p.end(path.join(__dirname, '/files/main.js'));
20+
p.on('error', t.fail.bind(t));
21+
var pack = packer();
22+
23+
p.pipe(JSONStream.stringify()).pipe(pack);
24+
25+
var src = '';
26+
pack.on('data', function (buf) { src += buf });
27+
pack.on('end', function () {
28+
Function('console', src)({
29+
log: function (s) { t.equal(s, 'main: 1055') }
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)