forked from popeindustries/simpler-browser-require
-
Notifications
You must be signed in to change notification settings - Fork 0
/
require.js
73 lines (65 loc) · 1.85 KB
/
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
(function(root) {
/**
* Load or retrieve cached version of requested module with id 'path' or 'path/index'
* @param {String} path
* @returns {Object}
*/
function require (path) {
// Find in cache
var m = require.modules[path] || require.modules[path + '/index'];
if (!m) {
// Handle versioned modules when called without version number
var p, idx;
for (var p in require.modules) {
if ((idx = p.indexOf('#')) != -1) {
if (path == p.slice(0, idx)) {
m = require.modules[p];
break;
}
}
}
if (!m) throw new Error("Couldn't find module for: " + path);
}
// Instantiate the module if it's export object is not yet defined
if (!m.exports) {
// Convert 'lazy' evaluated string to Function
if ('string' == typeof m) {
// 'm' is key to raw source
m = require.modules[path] = new Function('require', 'module', 'exports', require.modules[m]);
}
m.exports = {};
m.filename = path;
m.call(null, require, m, m.exports);
}
// Return the exports object
return m.exports;
}
// Cache of module objects
require.modules = {};
/**
* Retrieve raw 'lazy' module source
* @param {String} path
* @returns {String}
*/
require.raw = function requireRaw (path) {
return require.modules['raw:' + path] || '';
};
/**
* Register a module with id of 'path' and callback of 'fn'
* Alternatively accepts 'fn' string for lazy evaluation
* @param {String} path
* @param {Function|String} fn [signature should be of type (require, module, exports)]
*/
require.register = function requireRegister (path, fn) {
if ('string' == typeof fn) {
// Store raw source
var key = 'raw:' + path;
require.modules[key] = fn;
require.modules[path] = key;
} else {
require.modules[path] = fn;
}
};
// Expose
root.require = require;
})((typeof window !== 'undefined') ? window : global);