forked from DEADB17/require-from
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (27 loc) · 833 Bytes
/
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
/* jshint node:true */
var assert = require('assert');
var pathLib = require('path');
module.exports = function (key, path) {
assert(typeof key === 'string', 'key must be a string');
assert(key, 'missing key');
assert(typeof path === 'string', 'path must be a string');
assert(key, 'missing path');
var isRel = path.charAt(0) === '.';
var moduleExports;
if (isRel) {
var parent = pathLib.dirname(module.parent.filename);
var normalized = pathLib.resolve(parent, path);
moduleExports = require(normalized);
} else {
moduleExports = require(path);
}
var res;
if (module.parent.children.some(function (it) {
res = it;
return it.exports === moduleExports;
})) {
return res[key];
} else {
return undefined;
}
};