UNPKG

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