-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlternativeModuleLoader.ts
128 lines (90 loc) · 2.67 KB
/
AlternativeModuleLoader.ts
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
import * as Module from "module";
import * as NativeModule from "native_module";
import * as util from "util";
interface DependencyInfo {
deps: { [importName: string]: Dependency|DependencyReference };
id?: number;
}
interface PackageInfo extends DependencyInfo {
shared: { [key: string]: Dependency|DependencyReference };
}
interface Dependency extends DependencyInfo {
packageId: string;
}
var globalDependencyInfo: PackageInfo;
var globalId = 1;
interface DependencyReference {
ref: string;
}
interface ParseResult {
isRelative: boolean;
packageName?: string;
path: string; // is empty or starts with "/"
}
function parseModuleName(moduleName: string): ParseResult {
if (moduleName.substr(2) === "./" || moduleName.substr(2) === "..")
return { isRelative: true, path: moduleName };
const parts = moduleName.split("/", 2);
const packageName = parts[0];
const path = parts.length > 1 ? ("/" + parts[1]) : "";
return { isRelative: false, packageName, path };
}
interface IModule {
parent: IModule|undefined;
dependencyInfo: DependencyInfo;
load(filename: string);
}
function isReference(x: any): x is DependencyReference {
return x["ref"] !== undefined;
}
let repoPath: string; // must not end with "/"
export function setRepoPath(path: string) {
repoPath = path;
globalDependencyInfo = require(path + '/deps.json');
Module._load = function(request: string, parent: IModule) {
if (!parent.dependencyInfo) {
parent.dependencyInfo = globalDependencyInfo;
}
const p = parseModuleName(request);
let cacheExtra = 0;
let dep: DependencyInfo|undefined = undefined;
if (!p.isRelative && parent.dependencyInfo.deps[p.packageName] !== undefined) {
let dep2 = parent.dependencyInfo.deps[p.packageName];
while (isReference(dep2)) {
dep2 = globalDependencyInfo.shared[dep2.ref];
}
dep = dep2;
if (dep2.id === undefined) {
dep2.id = globalId++;
}
cacheExtra = dep2.id;
request = repoPath + "/" + dep2.packageId + p.path + "/index.js";
}
else
dep = parent.dependencyInfo;
var filename = Module._resolveFilename(request, parent, false);
const cacheKey = filename + cacheExtra;
var cachedModule = Module._cache[cacheKey];
if (cachedModule) {
return cachedModule.exports;
}
var module = new Module(filename, parent);
module.dependencyInfo = dep;
Module._cache[cacheKey] = module;
tryModuleLoad(module, filename, cacheKey);
return module.exports;
};
}
const originalLoad = Module._load;
function tryModuleLoad(module: IModule, filename: string, cacheKey: string) {
var threw = true;
try {
module.load(filename);
threw = false;
}
finally {
if (threw) {
delete Module._cache[cacheKey];
}
}
}