-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakerequire.js
62 lines (59 loc) · 1.59 KB
/
fakerequire.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
/**
* [Attempt at a] drop-in replacement for requireJS
* for when you control the loading of all the files
* and can guarantee that all needed dependencies exist.
*
* define( name, dependencies, callback );
* ....
* require( [someModuleName], function(someModule) { } );
*/
var fakerequire = (function() {
var modules = {};
var define = function(name, deps, callback) {
modules[name] = { dependencies: deps, callback: callback, exports: undefined };
};
var resolveDeps = function(depNames, overrides) {
var deps = [];
for( var d in depNames ) {
var depName = depNames[d];
var v;
if( overrides[depName] !== undefined ) {
v = overrides[depName];
} else {
v = getModuleExports(depName);
}
deps.push(v);
}
return deps;
};
var getModuleExports = function(name) {
var module = modules[name];
if( !module ) throw new Error("No module '"+name+"' defined");
if( module.exports === undefined ) {
module.exports = {};
var deps = resolveDeps( module.dependencies, {exports: module.exports, require: null} );
module.callback.apply(module.callback, deps);
}
return module.exports;
};
var require = function(depNames, callback, errback) {
var exports = {};
var args;
if( errback ) {
try {
args = resolveDeps(depNames, { exports: exports, require: null });
} catch( e ) {
errback(e);
}
} else {
args = resolveDeps(depNames, { exports: exports, require: null });
}
callback.apply(callback, args);
};
return {
define: define,
require: require
};
})();
window.define = fakerequire.define;
window.require = fakerequire.require;