UNPKG

3.5 kBJavaScriptView Raw
1
2/**
3 * Require the given path.
4 *
5 * @param {String} path
6 * @return {Object} exports
7 * @api public
8 */
9
10function 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._resolving && !module.exports) {
30 var mod = {};
31 mod.exports = {};
32 mod.client = mod.component = true;
33 module._resolving = true;
34 module.call(this, mod.exports, require.relative(resolved), mod);
35 delete module._resolving;
36 module.exports = mod.exports;
37 }
38
39 return module.exports;
40}
41
42/**
43 * Registered modules.
44 */
45
46require.modules = {};
47
48/**
49 * Main definitions.
50 */
51
52require.mains = {};
53
54/**
55 * Define a main.
56 */
57
58require.main = function(name, path){
59 require.mains[name] = path;
60};
61
62/**
63 * Resolve `path`.
64 *
65 * Lookup:
66 *
67 * - PATH/index.js
68 * - PATH.js
69 * - PATH
70 *
71 * @param {String} path
72 * @return {String} path or null
73 * @api private
74 */
75
76require.resolve = function(path) {
77 if ('/' == path.charAt(0)) path = path.slice(1);
78
79 var paths = [
80 path,
81 path + '.js',
82 path + '.json',
83 path + '/index.js',
84 path + '/index.json'
85 ];
86
87 if (require.mains[path]) {
88 paths = [path + '/' + require.mains[path]];
89 }
90
91 for (var i = 0, len = paths.length; i < len; i++) {
92 var path = paths[i];
93 if (require.modules.hasOwnProperty(path)) {
94 return path;
95 }
96 }
97};
98
99/**
100 * Normalize `path` relative to the current path.
101 *
102 * @param {String} curr
103 * @param {String} path
104 * @return {String}
105 * @api private
106 */
107
108require.normalize = function(curr, path) {
109 var segs = [];
110
111 if ('.' != path.charAt(0)) return path;
112
113 curr = curr.split('/');
114 path = path.split('/');
115
116 for (var i = 0, len = path.length; i < len; ++i) {
117 if ('..' == path[i]) {
118 curr.pop();
119 } else if ('.' != path[i] && '' != path[i]) {
120 segs.push(path[i]);
121 }
122 }
123
124 return curr.concat(segs).join('/');
125};
126
127/**
128 * Register module at `path` with callback `definition`.
129 *
130 * @param {String} path
131 * @param {Function} definition
132 * @api private
133 */
134
135require.register = function(path, definition) {
136 require.modules[path] = definition;
137};
138
139/**
140 * Return a require function relative to the `parent` path.
141 *
142 * @param {String} parent
143 * @return {Function}
144 * @api private
145 */
146
147require.relative = function(parent) {
148 var root = require.normalize(parent, '..');
149
150 /**
151 * lastIndexOf helper.
152 */
153
154 function lastIndexOf(arr, obj) {
155 var i = arr.length;
156 while (i--) {
157 if (arr[i] === obj) return i;
158 }
159 return -1;
160 }
161
162 /**
163 * The relative require() itself.
164 */
165
166 function localRequire(path) {
167 var resolved = localRequire.resolve(path);
168 return require(resolved, parent, path);
169 }
170
171 /**
172 * Resolve relative to the parent.
173 */
174
175 localRequire.resolve = function(path) {
176 var c = path.charAt(0);
177 if ('/' == c) return path.slice(1);
178 if ('.' == c) return require.normalize(root, path);
179 return path;
180 };
181
182 /**
183 * Check if module is defined at `path`.
184 */
185
186 localRequire.exists = function(path) {
187 return require.modules.hasOwnProperty(localRequire.resolve(path));
188 };
189
190 return localRequire;
191};