/
browser_require.js
132 lines (126 loc) · 3.75 KB
/
browser_require.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
function fluentRequire (path) {
var mod = fluentRequire.modules[path];
if (mod && mod.compiled) {
return mod.compiled;
}
throw new Error("Missing module " + path);
}
fluentRequire.modules = {};
/**
* Invoked by JavaScript sent from the server back to the browser.
* @param {String} module is the fluent-normalized module name
* @param {String} src is the JavaScript content to eval
* @param {Array} deps is an Array of dependencies (strings pointing to modules) that
* must load before module can be loaded.
*/
fluentRequire.load = function (module, src, deps) {
var i = deps.length
, depModule
, target = ModulePromise.from(module);
target.src = src;
while (i--) {
depModule = ModulePromise.from(deps[i], target);
if (!depModule.compiled) {
target.dependsOn(depModule);
fluentRequire.modules[depModule.name] = depModule;
depModule.load();
}
}
if (target.deps.length === 0) {
target.loaded();
}
};
var ModulePaths = {
isNormalized: function (name) {
return (/^\//).test(name);
},
isRel: function (name) {
return (/^\./).test(name);
},
isNpm: function (name) {
return (/^\/NPM\//).test(name) || !this.isRel(name);
},
normalizeRelToParent: function (name, parent) {
if (!parent) return name;
var pathparts = parent.basedir.split('/');
return this.normalizeRelToDir(name, pathparts);
},
normalizeRelToDir: function (name, dirparts) {
var parts = name.split('/')
for (var i = 0, l = parts.length, part; i < l; i++) {
part = parts[i];
if (part === '.') continue;
else if (part === '..') dirparts.pop();
else dirparts.push(part);
}
return dirparts.join('/') + '.js';
}
};
/**
* @param {String} name
* @param {ModulePromise} parent
*/
function ModulePromise (name, parent) {
this.parents = [];
this.name = ModulePaths.isNormalized(name)
? name
: ModulePaths.isRel(name)
? ModulePaths.normalizeRelToParent(name, parent)
: "/NPM/" + name + '.js';
this.basedir = ModulePaths.isNpm(name)
? this.name.split('/').length > 2
? this.name.split('/').slice(0, -1).join('/')
: this.name.replace(/\.js$/, '')
: parent
? parent.basedir
: name.split('/').slice(0, -1).join('/');
this.deps = [];
}
ModulePromise.from = function (module, parent) {
if (!parent) {
return fluentRequire.modules[module] || (fluentRequire.modules[module] = new ModulePromise(module));
} else {
var name = ModulePaths.isNormalized(module)
? module
: ModulePaths.isRel(module)
? ModulePaths.normalizeRelToParent(module, parent)
: "/NPM/" + module + '.js';
return fluentRequire.modules[name] || (fluentRequire.modules[name] = new ModulePromise(module, parent));
}
};
ModulePromise.prototype = {
dependsOn: function (module) {
this.deps.push(module.name);
module.parents.push(this);
},
load: function () {
var priorScript = document.getElementsByTagName('script')[0]
, script = document.createElement('SCRIPT');
script.async = true;
script.src = this.name;
script.onerror = function () {};
priorScript.parentNode.insertBefore(script, priorScript);
},
loaded: function (dep) {
if (this.compiled) return;
var deps = this.deps;
if (dep) deps.splice(deps.indexOf(dep), 1);
if (!deps.length) {
var module = { exports: {} }
, exports = module.exports
, self = this;
(function () {
try {
return eval(self.src);
} catch (e) {
throw new Error("Something wrong with...\n" + self.src);
}
}).call(module.exports);
this.compiled = module.exports;
var i = this.parents.length;
while (i--) {
this.parents[i].loaded(this.name);
}
}
}
};