-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
195 lines (155 loc) · 5.47 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// builtin
var priv_module = require('module');
var natives = process.binding('natives');
var path = require('path');
var fs = require('fs');
// 3rd party
var detective = require('detective');
// inspect the source for dependencies
function from_source(source, parent, opt, cb) {
var cache = opt.cache;
var ignore_missing = false || opt.ignoreMissing;
var requires = (opt.detective || detective)(source);
var result = [];
// deduplicate requires with the same name
// this avoids trying to process the require twice
requires = requires.filter(function(elem, idx) {
return requires.indexOf(elem) === idx;
});
(function next() {
var req = requires.shift();
if (!req) {
return cb(null, result);
}
// short require name
var id = req;
var native = natives[id];
var resolve = opt.resolve(req, parent, function(err, full_path, ignore) {
if (err) {
return cb(err);
}
if (!full_path) {
// if resolver ignored the native
// we just push it manually
if (native) {
result.push({
id: id,
core: true,
// native is deprecated in favor of `core`
// kept here for backwards compat
native: true
});
return next();
}
// skip the dependency if we can't find it
if (ignore_missing) {
if (typeof ignore_missing === 'function') {
ignore_missing(req, parent.filename)
}
return next();
}
return cb(new Error('Cannot find module: \'' + req + '\' ' +
'required from ' + parent.filename));
}
// ignore indicates we should not process dependencies for this file
// this is useful if we don't care about certain files being handled further
// we still want the dependency added to the deps of the file we processed
// but do not process this file or it's deps
if (ignore) {
result.push({
id: id,
filename: full_path
});
return next();
}
var paths = parent.paths.concat(node_module_paths(full_path));
var new_parent = {
id: id,
filename: full_path,
paths: paths
}
from_filename(full_path, new_parent, opt, function(err, deps, src) {
if (err) {
return cb(err);
}
var res = {
id: id,
filename: full_path,
deps: deps
};
if (opt.includeSource) {
res.source = src;
}
result.push(res);
next();
});
});
})();
}
function from_filename(filename, parent, opt, cb) {
var cache = opt.cache;
// wtf is this cache?
// appears to be the list of dependencies for this filename
// what it really should be is the info
var cached = cache[filename];
if (cached) {
return cb(null, cached.deps, cached.src);
}
fs.readFile(filename, 'utf8', function(err, content) {
if (err) {
return cb(err);
}
// must be set before the compile call to handle circular references
var result = cache[filename] = { deps: [] };
try {
from_source(content, parent, opt, function(err, deps) {
if (err) {
return cb(err);
}
result.deps = deps;
// only cache source if caller will want the source
if (opt.includeSource) {
result.src = content;
}
return cb(err, deps, content);
});
} catch (err) {
err.message = filename + ': ' + err.message;
throw err;
};
});
}
/// lookup the full path to our module with local name 'name'
function lookup_path(name, parent) {
var resolved_module = priv_module.Module._resolveLookupPaths(name, parent);
var paths = resolved_module[1];
return priv_module.Module._findPath(name, paths);
}
/// return an array of node_module paths given a filename
function node_module_paths(filename) {
return priv_module.Module._nodeModulePaths(path.dirname(filename));
}
/// process filename and callback with tree of dependencies
/// the tree does have circular references when a child requires a parent
module.exports = function(filename, opt, cb) {
opt = opt || {};
if (typeof opt === 'function') {
cb = opt;
opt = {};
}
// add the cache storage
opt.cache = opt.cache || {};
// default resolver if none specified just resolves as node would
opt.resolve = opt.resolve || function(id, parent, cb) {
// TODO(shtylman) async
cb(null, lookup_path(id, parent));
};
var paths = node_module_paths(filename);
// entry parent specifies the base node modules path
var entry_parent = {
id: filename,
filename: filename,
paths: paths
};
from_filename(filename, entry_parent, opt, cb);
};