1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function require(path, parent, orig) {
|
11 | var resolved = require.resolve(path);
|
12 |
|
13 |
|
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 |
|
27 |
|
28 |
|
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 |
|
44 |
|
45 |
|
46 | require.modules = {};
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | require.mains = {};
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | require.main = function(name, path){
|
59 | require.mains[name] = path;
|
60 | };
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | require.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 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | require.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 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | require.register = function(path, definition) {
|
136 | require.modules[path] = definition;
|
137 | };
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | require.relative = function(parent) {
|
148 | var root = require.normalize(parent, '..');
|
149 |
|
150 | |
151 |
|
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 |
|
164 |
|
165 |
|
166 | function localRequire(path) {
|
167 | var resolved = localRequire.resolve(path);
|
168 | return require(resolved, parent, path);
|
169 | }
|
170 |
|
171 | |
172 |
|
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 |
|
184 |
|
185 |
|
186 | localRequire.exists = function(path) {
|
187 | return require.modules.hasOwnProperty(localRequire.resolve(path));
|
188 | };
|
189 |
|
190 | return localRequire;
|
191 | };
|