-
Notifications
You must be signed in to change notification settings - Fork 0
/
system-lite.ts
73 lines (55 loc) · 1.68 KB
/
system-lite.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
/**
* license: MIT
* author: [email protected]
*/
namespace System {
export interface ModuleFactory {
setters: Function[];
execute: Function;
}
interface Register {
deps: string[];
factory: (exports, context) => ModuleFactory;
}
interface D<T> {
[s: string]: T;
}
const modules = <D<any>>{ };
const registers = <D<Register>>{ };
export function register(name: string, deps: string[], factory: (exports, context) => ModuleFactory) {
registers[name] = {
deps: deps,
factory: factory
};
}
export function registerExternal(name: string, mod: any) {
modules[name] = mod;
}
export function require(name: string) {
if (name in modules)
return modules[name];
return create(name);
}
function _import(name: string) {
var result = require(name);
return Promise.resolve(result);
}
(<any>window).require = require;
function create(name: string) {
if (!(name in registers))
throw new Error("Cannot resolve '" + name + "'");
const register = registers[name];
const result = {};
modules[name] = result;
function exportImpl(n: string, impl: any) {
result[n] = impl;
}
const innerFactory = register.factory(exportImpl, { id: name, import: _import });
for (var index = 0; index < register.deps.length; index++) {
const dependency = register.deps[index];
innerFactory.setters[index](require(dependency));
}
innerFactory.execute();
return result;
}
}