UNPKG

4.91 kBJavaScriptView Raw
1
2
3/**
4 * hasOwnProperty.
5 */
6
7var has = Object.prototype.hasOwnProperty;
8
9/**
10 * Require the given path.
11 *
12 * @param {String} path
13 * @return {Object} exports
14 * @api public
15 */
16
17function require(path, parent, orig) {
18 var resolved = require.resolve(path);
19
20 // lookup failed
21 if (null == resolved) {
22 orig = orig || path;
23 parent = parent || 'root';
24 var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
25 err.path = orig;
26 err.parent = parent;
27 err.require = true;
28 throw err;
29 }
30
31 var module = require.modules[resolved];
32
33 // perform real require()
34 // by invoking the module's
35 // registered function
36 if (!module.exports) {
37 module.exports = {};
38 module.client = module.component = true;
39 module.call(this, module.exports, require.relative(resolved), module);
40 }
41
42 return module.exports;
43}
44
45/**
46 * Registered modules.
47 */
48
49require.modules = {};
50
51/**
52 * Registered aliases.
53 */
54
55require.aliases = {};
56
57/**
58 * Resolve `path`.
59 *
60 * Lookup:
61 *
62 * - PATH/index.js
63 * - PATH.js
64 * - PATH
65 *
66 * @param {String} path
67 * @return {String} path or null
68 * @api private
69 */
70
71require.resolve = function(path) {
72 if (path.charAt(0) === '/') path = path.slice(1);
73 var index = path + '/index.js';
74
75 var paths = [
76 path,
77 path + '.js',
78 path + '.json',
79 path + '/index.js',
80 path + '/index.json'
81 ];
82
83 for (var i = 0; i < paths.length; i++) {
84 var path = paths[i];
85 if (has.call(require.modules, path)) return path;
86 }
87
88 if (has.call(require.aliases, index)) {
89 return require.aliases[index];
90 }
91};
92
93/**
94 * Normalize `path` relative to the current path.
95 *
96 * @param {String} curr
97 * @param {String} path
98 * @return {String}
99 * @api private
100 */
101
102require.normalize = function(curr, path) {
103 var segs = [];
104
105 if ('.' != path.charAt(0)) return path;
106
107 curr = curr.split('/');
108 path = path.split('/');
109
110 for (var i = 0; i < path.length; ++i) {
111 if ('..' == path[i]) {
112 curr.pop();
113 } else if ('.' != path[i] && '' != path[i]) {
114 segs.push(path[i]);
115 }
116 }
117
118 return curr.concat(segs).join('/');
119};
120
121/**
122 * Register module at `path` with callback `definition`.
123 *
124 * @param {String} path
125 * @param {Function} definition
126 * @api private
127 */
128
129require.register = function(path, definition) {
130 require.modules[path] = definition;
131};
132
133/**
134 * Alias a module definition.
135 *
136 * @param {String} from
137 * @param {String} to
138 * @api private
139 */
140
141require.alias = function(from, to) {
142 if (!has.call(require.modules, from)) {
143 throw new Error('Failed to alias "' + from + '", it does not exist');
144 }
145 require.aliases[to] = from;
146};
147
148/**
149 * Return a require function relative to the `parent` path.
150 *
151 * @param {String} parent
152 * @return {Function}
153 * @api private
154 */
155
156require.relative = function(parent) {
157 var p = require.normalize(parent, '..');
158
159 /**
160 * lastIndexOf helper.
161 */
162
163 function lastIndexOf(arr, obj) {
164 var i = arr.length;
165 while (i--) {
166 if (arr[i] === obj) return i;
167 }
168 return -1;
169 }
170
171 /**
172 * The relative require() itself.
173 */
174
175 function localRequire(path) {
176 var resolved = localRequire.resolve(path);
177 return require(resolved, parent, path);
178 }
179
180 /**
181 * Resolve relative to the parent.
182 */
183
184 localRequire.resolve = function(path) {
185 var c = path.charAt(0);
186 if ('/' == c) return path.slice(1);
187 if ('.' == c) return require.normalize(p, path);
188
189 // resolve deps by returning
190 // the dep in the nearest "deps"
191 // directory
192 var segs = parent.split('/');
193 var i = lastIndexOf(segs, 'deps') + 1;
194 if (!i) i = 0;
195 path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
196 return path;
197 };
198
199 /**
200 * Check if module is defined at `path`.
201 */
202
203 localRequire.exists = function(path) {
204 return has.call(require.modules, localRequire.resolve(path));
205 };
206
207 return localRequire;
208};
209require.register("example/index.js", function(exports, require, module){
210
211var template = require('./template');
212
213
214exports.foo = function (locals) {
215 return '\n' + template(locals);
216};
217});
218require.register("example/template.js", function(exports, require, module){
219module.exports =function anonymous(locals, attrs, escape, rethrow, merge) {
220attrs = attrs || jade.attrs; escape = escape || jade.escape; rethrow = rethrow || jade.rethrow; merge = merge || jade.merge;
221var buf = [];
222with (locals || {}) {
223var interp;
224buf.push('<!DOCTYPE html><html lang="en"><head><title>');
225var __val__ = pageTitle
226buf.push(escape(null == __val__ ? "" : __val__));
227buf.push('</title><script type="text/javascript">if (foo) {\n bar()\n}</script></head><body><h1>Jade - node template engine</h1><div id="container">');
228if ( youAreUsingJade)
229{
230buf.push('<p>You are amazing</p>');
231}
232else
233{
234buf.push('<p>Get on it!</p>');
235}
236buf.push('</div></body></html>');
237}
238return buf.join("");
239}
240});
241require.alias("example/index.js", "example/index.js");
242