You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
4.0 KiB

  1. /**
  2. * Require the given path.
  3. *
  4. * @param {String} path
  5. * @return {Object} exports
  6. * @api public
  7. */
  8. function require(path, parent, orig) {
  9. var resolved = require.resolve(path);
  10. // lookup failed
  11. if (null == resolved) {
  12. orig = orig || path;
  13. parent = parent || 'root';
  14. var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
  15. err.path = orig;
  16. err.parent = parent;
  17. err.require = true;
  18. throw err;
  19. }
  20. var module = require.modules[resolved];
  21. // perform real require()
  22. // by invoking the module's
  23. // registered function
  24. if (!module.exports) {
  25. module.exports = {};
  26. module.client = module.component = true;
  27. module.call(this, module.exports, require.relative(resolved), module);
  28. }
  29. return module.exports;
  30. }
  31. /**
  32. * Registered modules.
  33. */
  34. require.modules = {};
  35. /**
  36. * Registered aliases.
  37. */
  38. require.aliases = {};
  39. /**
  40. * Resolve `path`.
  41. *
  42. * Lookup:
  43. *
  44. * - PATH/index.js
  45. * - PATH.js
  46. * - PATH
  47. *
  48. * @param {String} path
  49. * @return {String} path or null
  50. * @api private
  51. */
  52. require.resolve = function(path) {
  53. if (path.charAt(0) === '/') path = path.slice(1);
  54. var index = path + '/index.js';
  55. var paths = [
  56. path,
  57. path + '.js',
  58. path + '.json',
  59. path + '/index.js',
  60. path + '/index.json'
  61. ];
  62. for (var i = 0; i < paths.length; i++) {
  63. var path = paths[i];
  64. if (require.modules.hasOwnProperty(path)) return path;
  65. }
  66. if (require.aliases.hasOwnProperty(index)) {
  67. return require.aliases[index];
  68. }
  69. };
  70. /**
  71. * Normalize `path` relative to the current path.
  72. *
  73. * @param {String} curr
  74. * @param {String} path
  75. * @return {String}
  76. * @api private
  77. */
  78. require.normalize = function(curr, path) {
  79. var segs = [];
  80. if ('.' != path.charAt(0)) return path;
  81. curr = curr.split('/');
  82. path = path.split('/');
  83. for (var i = 0; i < path.length; ++i) {
  84. if ('..' == path[i]) {
  85. curr.pop();
  86. } else if ('.' != path[i] && '' != path[i]) {
  87. segs.push(path[i]);
  88. }
  89. }
  90. return curr.concat(segs).join('/');
  91. };
  92. /**
  93. * Register module at `path` with callback `definition`.
  94. *
  95. * @param {String} path
  96. * @param {Function} definition
  97. * @api private
  98. */
  99. require.register = function(path, definition) {
  100. require.modules[path] = definition;
  101. };
  102. /**
  103. * Alias a module definition.
  104. *
  105. * @param {String} from
  106. * @param {String} to
  107. * @api private
  108. */
  109. require.alias = function(from, to) {
  110. if (!require.modules.hasOwnProperty(from)) {
  111. throw new Error('Failed to alias "' + from + '", it does not exist');
  112. }
  113. require.aliases[to] = from;
  114. };
  115. /**
  116. * Return a require function relative to the `parent` path.
  117. *
  118. * @param {String} parent
  119. * @return {Function}
  120. * @api private
  121. */
  122. require.relative = function(parent) {
  123. var p = require.normalize(parent, '..');
  124. /**
  125. * lastIndexOf helper.
  126. */
  127. function lastIndexOf(arr, obj) {
  128. var i = arr.length;
  129. while (i--) {
  130. if (arr[i] === obj) return i;
  131. }
  132. return -1;
  133. }
  134. /**
  135. * The relative require() itself.
  136. */
  137. function localRequire(path) {
  138. var resolved = localRequire.resolve(path);
  139. return require(resolved, parent, path);
  140. }
  141. /**
  142. * Resolve relative to the parent.
  143. */
  144. localRequire.resolve = function(path) {
  145. var c = path.charAt(0);
  146. if ('/' == c) return path.slice(1);
  147. if ('.' == c) return require.normalize(p, path);
  148. // resolve deps by returning
  149. // the dep in the nearest "deps"
  150. // directory
  151. var segs = parent.split('/');
  152. var i = lastIndexOf(segs, 'deps') + 1;
  153. if (!i) i = 0;
  154. path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
  155. return path;
  156. };
  157. /**
  158. * Check if module is defined at `path`.
  159. */
  160. localRequire.exists = function(path) {
  161. return require.modules.hasOwnProperty(localRequire.resolve(path));
  162. };
  163. return localRequire;
  164. };
  165. require.register("isarray/index.js", function(exports, require, module){
  166. module.exports = Array.isArray || function (arr) {
  167. return Object.prototype.toString.call(arr) == '[object Array]';
  168. };
  169. });
  170. require.alias("isarray/index.js", "isarray/index.js");