Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80538 views
1
2
/**
3
* Require the given path.
4
*
5
* @param {String} path
6
* @return {Object} exports
7
* @api public
8
*/
9
10
function require(path, parent, orig) {
11
var resolved = require.resolve(path);
12
13
// lookup failed
14
if (null == resolved) {
15
orig = orig || path;
16
parent = parent || 'root';
17
var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
18
err.path = orig;
19
err.parent = parent;
20
err.require = true;
21
throw err;
22
}
23
24
var module = require.modules[resolved];
25
26
// perform real require()
27
// by invoking the module's
28
// registered function
29
if (!module.exports) {
30
module.exports = {};
31
module.client = module.component = true;
32
module.call(this, module.exports, require.relative(resolved), module);
33
}
34
35
return module.exports;
36
}
37
38
/**
39
* Registered modules.
40
*/
41
42
require.modules = {};
43
44
/**
45
* Registered aliases.
46
*/
47
48
require.aliases = {};
49
50
/**
51
* Resolve `path`.
52
*
53
* Lookup:
54
*
55
* - PATH/index.js
56
* - PATH.js
57
* - PATH
58
*
59
* @param {String} path
60
* @return {String} path or null
61
* @api private
62
*/
63
64
require.resolve = function(path) {
65
if (path.charAt(0) === '/') path = path.slice(1);
66
var index = path + '/index.js';
67
68
var paths = [
69
path,
70
path + '.js',
71
path + '.json',
72
path + '/index.js',
73
path + '/index.json'
74
];
75
76
for (var i = 0; i < paths.length; i++) {
77
var path = paths[i];
78
if (require.modules.hasOwnProperty(path)) return path;
79
}
80
81
if (require.aliases.hasOwnProperty(index)) {
82
return require.aliases[index];
83
}
84
};
85
86
/**
87
* Normalize `path` relative to the current path.
88
*
89
* @param {String} curr
90
* @param {String} path
91
* @return {String}
92
* @api private
93
*/
94
95
require.normalize = function(curr, path) {
96
var segs = [];
97
98
if ('.' != path.charAt(0)) return path;
99
100
curr = curr.split('/');
101
path = path.split('/');
102
103
for (var i = 0; i < path.length; ++i) {
104
if ('..' == path[i]) {
105
curr.pop();
106
} else if ('.' != path[i] && '' != path[i]) {
107
segs.push(path[i]);
108
}
109
}
110
111
return curr.concat(segs).join('/');
112
};
113
114
/**
115
* Register module at `path` with callback `definition`.
116
*
117
* @param {String} path
118
* @param {Function} definition
119
* @api private
120
*/
121
122
require.register = function(path, definition) {
123
require.modules[path] = definition;
124
};
125
126
/**
127
* Alias a module definition.
128
*
129
* @param {String} from
130
* @param {String} to
131
* @api private
132
*/
133
134
require.alias = function(from, to) {
135
if (!require.modules.hasOwnProperty(from)) {
136
throw new Error('Failed to alias "' + from + '", it does not exist');
137
}
138
require.aliases[to] = from;
139
};
140
141
/**
142
* Return a require function relative to the `parent` path.
143
*
144
* @param {String} parent
145
* @return {Function}
146
* @api private
147
*/
148
149
require.relative = function(parent) {
150
var p = require.normalize(parent, '..');
151
152
/**
153
* lastIndexOf helper.
154
*/
155
156
function lastIndexOf(arr, obj) {
157
var i = arr.length;
158
while (i--) {
159
if (arr[i] === obj) return i;
160
}
161
return -1;
162
}
163
164
/**
165
* The relative require() itself.
166
*/
167
168
function localRequire(path) {
169
var resolved = localRequire.resolve(path);
170
return require(resolved, parent, path);
171
}
172
173
/**
174
* Resolve relative to the parent.
175
*/
176
177
localRequire.resolve = function(path) {
178
var c = path.charAt(0);
179
if ('/' == c) return path.slice(1);
180
if ('.' == c) return require.normalize(p, path);
181
182
// resolve deps by returning
183
// the dep in the nearest "deps"
184
// directory
185
var segs = parent.split('/');
186
var i = lastIndexOf(segs, 'deps') + 1;
187
if (!i) i = 0;
188
path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
189
return path;
190
};
191
192
/**
193
* Check if module is defined at `path`.
194
*/
195
196
localRequire.exists = function(path) {
197
return require.modules.hasOwnProperty(localRequire.resolve(path));
198
};
199
200
return localRequire;
201
};
202
require.register("isarray/index.js", function(exports, require, module){
203
module.exports = Array.isArray || function (arr) {
204
return Object.prototype.toString.call(arr) == '[object Array]';
205
};
206
207
});
208
require.alias("isarray/index.js", "isarray/index.js");
209
210
211