-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (34 loc) · 1000 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
34
35
36
37
38
39
40
41
/* jshint node:true */
'use strict';
var assert = require('assert');
var pathLib = require('path');
module.exports = function (key, mod, path) {
var argLen = arguments.length;
if (argLen === 2) {
path = mod;
mod = undefined;
}
var isRel = path.charAt(0) === '.';
assert(typeof key === 'string', 'export key must be a string');
assert(key, 'missing export key');
assert(typeof path === 'string', 'path must be a string');
assert(key, 'missing path');
var moduleExports;
if (isRel) {
assert(mod, 'missing module');
var parent = pathLib.dirname(mod.filename);
var normalized = pathLib.resolve(parent, path);
moduleExports = require(normalized);
} else {
moduleExports = require(path);
}
var res;
if (module.children.some(function (it) {
res = it;
return it.exports === moduleExports;
})) {
return res[key];
} else {
return undefined;
}
};