/
index.js
48 lines (43 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var fs = require('fs');
module.exports = function (b, opts) {
opts = opts || {};
var cache = {};
b._bresolve = async(b._bresolve, opts.filter);
};
module.exports.async = async;
module.exports.sync = sync;
module.exports.realpath = realpath;
function async(resolve, filter) {
var cache = {};
return function (id, parent, cb) {
resolve(id, parent, function (err, file, pkg) {
if (err) {
return cb(err, file, pkg);
}
return cb(err, realpath(file, filter, cache), pkg);
});
};
}
function sync(resolve, filter) {
var cache = {};
return function (id, parent) {
return realpath(resolve(id, parent), filter, cache);
};
}
function realpath(file, filter, cache) {
cache = cache || {};
filter = [].concat(filter).filter(Boolean);
var moduleDir = file.split('/node_modules/')[1];
moduleDir = moduleDir && moduleDir.split('/')[0];
if (!moduleDir) {
return file;
}
if (!filter.length || filter.indexOf(moduleDir) > -1) {
try {
var rfile = fs.realpathSync(file, cache);
return rfile;
} catch (e) {
}
}
return file;
}