-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-loader.js
74 lines (61 loc) · 1.52 KB
/
module-loader.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
var Module = module.constructor;
var fs = require('graceful-fs');
var path = require('path');
var anymatch = require('anymatch');
// Modules
var errors = require('./errors');
// Variables
var depsTree = {},
loading = {},
absCacheMap = {};
// Not Pure
function recursiveBurst(filename) {
delete Module._cache[require.resolve(filename)];
if (!depsTree[filename]) return;
return depsTree[filename].forEach(recursiveBurst);
}
// Not Pure
function burst(filename) {
delete Module._cache[require.resolve(filename)];
return recursiveBurst(filename);
}
// Not Pure
function addDependent(dependent, filename) {
if (!dependent) {
return
}
if (!depsTree[filename]) {
depsTree[filename] = [];
}
if (depsTree[filename].indexOf(dependent) === -1) {
depsTree[filename].push(dependent);
}
}
exports.burst = burst;
exports.default = function(config) {
return function warmRequire(request, parent) {
if ( (arguments.length < 1) || (arguments.length > 2) )
throw errors.INVALID_ARGUMENTS;
var filename = Module._resolveFilename(request, parent);
if (!filename) {
throw errors.FILE_DOES_NOT_EXIST;
}
if (!anymatch(config.paths, filename)) {
return require(filename);
}
if (parent) {
addDependent(parent.filename, filename);
}
var cachedModule = Module._cache[filename];
if (cachedModule) {
return cachedModule.exports;
}
var m = new Module(filename);
Module._cache[filename] = m;
m.require = function(targetPath) {
return warmRequire(targetPath, m);
}
m.load(filename);
return m.exports;
};
};