UNPKG

8.4 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.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
42require.modules = {};
43
44/**
45 * Registered aliases.
46 */
47
48require.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
64require.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
95require.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
122require.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
134require.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
149require.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};
202require.register("example/index.js", function(exports, require, module){
203var template = require('./template')
204 , html = template({ youAreUsingJade : true });
205
206document.body.innerHTML = html;
207});
208require.register("example/jade-runtime.js", function(exports, require, module){
209
210jade = (function(exports){
211/*!
212 * Jade - runtime
213 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
214 * MIT Licensed
215 */
216
217/**
218 * Lame Array.isArray() polyfill for now.
219 */
220
221if (!Array.isArray) {
222 Array.isArray = function(arr){
223 return '[object Array]' == Object.prototype.toString.call(arr);
224 };
225}
226
227/**
228 * Lame Object.keys() polyfill for now.
229 */
230
231if (!Object.keys) {
232 Object.keys = function(obj){
233 var arr = [];
234 for (var key in obj) {
235 if (obj.hasOwnProperty(key)) {
236 arr.push(key);
237 }
238 }
239 return arr;
240 }
241}
242
243/**
244 * Merge two attribute objects giving precedence
245 * to values in object `b`. Classes are special-cased
246 * allowing for arrays and merging/joining appropriately
247 * resulting in a string.
248 *
249 * @param {Object} a
250 * @param {Object} b
251 * @return {Object} a
252 * @api private
253 */
254
255exports.merge = function merge(a, b) {
256 var ac = a['class'];
257 var bc = b['class'];
258
259 if (ac || bc) {
260 ac = ac || [];
261 bc = bc || [];
262 if (!Array.isArray(ac)) ac = [ac];
263 if (!Array.isArray(bc)) bc = [bc];
264 ac = ac.filter(nulls);
265 bc = bc.filter(nulls);
266 a['class'] = ac.concat(bc).join(' ');
267 }
268
269 for (var key in b) {
270 if (key != 'class') {
271 a[key] = b[key];
272 }
273 }
274
275 return a;
276};
277
278/**
279 * Filter null `val`s.
280 *
281 * @param {Mixed} val
282 * @return {Mixed}
283 * @api private
284 */
285
286function nulls(val) {
287 return val != null;
288}
289
290/**
291 * Render the given attributes object.
292 *
293 * @param {Object} obj
294 * @param {Object} escaped
295 * @return {String}
296 * @api private
297 */
298
299exports.attrs = function attrs(obj, escaped){
300 var buf = []
301 , terse = obj.terse;
302
303 delete obj.terse;
304 var keys = Object.keys(obj)
305 , len = keys.length;
306
307 if (len) {
308 buf.push('');
309 for (var i = 0; i < len; ++i) {
310 var key = keys[i]
311 , val = obj[key];
312
313 if ('boolean' == typeof val || null == val) {
314 if (val) {
315 terse
316 ? buf.push(key)
317 : buf.push(key + '="' + key + '"');
318 }
319 } else if (0 == key.indexOf('data') && 'string' != typeof val) {
320 buf.push(key + "='" + JSON.stringify(val) + "'");
321 } else if ('class' == key && Array.isArray(val)) {
322 buf.push(key + '="' + exports.escape(val.join(' ')) + '"');
323 } else if (escaped && escaped[key]) {
324 buf.push(key + '="' + exports.escape(val) + '"');
325 } else {
326 buf.push(key + '="' + val + '"');
327 }
328 }
329 }
330
331 return buf.join(' ');
332};
333
334/**
335 * Escape the given string of `html`.
336 *
337 * @param {String} html
338 * @return {String}
339 * @api private
340 */
341
342exports.escape = function escape(html){
343 return String(html)
344 .replace(/&(?!(\w+|\#\d+);)/g, '&amp;')
345 .replace(/</g, '&lt;')
346 .replace(/>/g, '&gt;')
347 .replace(/"/g, '&quot;');
348};
349
350/**
351 * Re-throw the given `err` in context to the
352 * the jade in `filename` at the given `lineno`.
353 *
354 * @param {Error} err
355 * @param {String} filename
356 * @param {String} lineno
357 * @api private
358 */
359
360exports.rethrow = function rethrow(err, filename, lineno){
361 if (!filename) throw err;
362
363 var context = 3
364 , str = require('fs').readFileSync(filename, 'utf8')
365 , lines = str.split('\n')
366 , start = Math.max(lineno - context, 0)
367 , end = Math.min(lines.length, lineno + context);
368
369 // Error context
370 var context = lines.slice(start, end).map(function(line, i){
371 var curr = i + start + 1;
372 return (curr == lineno ? ' > ' : ' ')
373 + curr
374 + '| '
375 + line;
376 }).join('\n');
377
378 // Alter exception message
379 err.path = filename;
380 err.message = (filename || 'Jade') + ':' + lineno
381 + '\n' + context + '\n\n' + err.message;
382 throw err;
383};
384
385 return exports;
386
387})({});
388});
389require.register("example/template.js", function(exports, require, module){
390module.exports = function anonymous(locals, attrs, escape, rethrow, merge) {
391attrs = attrs || jade.attrs; escape = escape || jade.escape; rethrow = rethrow || jade.rethrow; merge = merge || jade.merge;
392var buf = [];
393with (locals || {}) {
394var interp;
395buf.push('<h1>Jade - node template engine</h1><div id="container">');
396if ( youAreUsingJade)
397{
398buf.push('<p>You are amazing</p>');
399}
400else
401{
402buf.push('<p>Get on it!</p>');
403}
404buf.push('</div>');
405}
406return buf.join("");
407}
408});
409require.alias("example/index.js", "example/index.js");
410
411require("example/jade-runtime");