-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.js
86 lines (70 loc) · 2.4 KB
/
module.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
window.module = {
modules: {},
check_modules: function () {
var changes = false;
to_next_module: for (var mod_ix in this.modules) {
cur_module = this.modules[mod_ix];
if (cur_module.processed) continue;
for (var req_ix = 0; req_ix < cur_module.dependencies.length; req_ix++) {
var req_name = cur_module.dependencies[req_ix];
if (!this.modules[req_name]) {
console.log("module: Dependency '" + req_name + "' for '" + cur_module.name + "' not found.");
continue to_next_module;
}
if (!this.modules[req_name].processed) {
console.log("module: Dependency '" + req_name + "' for '" + cur_module.name + "' not processed.");
continue to_next_module;
}
}
console.log("module: Processing '" + cur_module.name + "'.");
try {
var before = [];
for (var prop in window) {
before.push(prop);
}
// Run the module.
cur_module.exports = cur_module.callback.apply(window);
var after = [];
for (var prop in window) {
after.push(prop);
}
var polluted_variables = [];
for (var i = 0; i < after.length; i++) {
if (before.indexOf(after[i]) == -1) {
polluted_variables.push(after[i]);
}
}
if (polluted_variables.length > 0) {
console.warn("module: Global namespace pollution found in '" + cur_module.name + "':\n " + (polluted_variables.join("\n ")));
}
} catch (e) {
if (e.stack) {
console.error("module: Processing '" + cur_module.name + "' failed:\nStack:\n" + e.stack.toString() + "\nError:\n" + e.toString());
} else {
console.error("module: Processing '" + cur_module.name + "' failed:");
console.error(e);
}
continue to_next_module;
};
changes = true;
cur_module.processed = true;
}
if (changes) this.check_modules();
},
declare: function (name, dependencies, callback) {
this.modules[name] = {
name: name,
dependencies: dependencies,
callback: callback,
exports: {},
processed: false
};
this.check_modules();
},
require: function (name) {
if (!this.modules[name]) {
throw new Error("module: Unsatisfied dependency (" + name + ") requested!");
}
return this.modules[name].exports;
}
};