default_resolver.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = defaultResolver;
  6. var _browserResolve;
  7. function _load_browserResolve() {
  8. return (_browserResolve = _interopRequireDefault(require('browser-resolve')));
  9. }
  10. var _fs;
  11. function _load_fs() {
  12. return (_fs = _interopRequireDefault(require('fs')));
  13. }
  14. var _path;
  15. function _load_path() {
  16. return (_path = _interopRequireDefault(require('path')));
  17. }
  18. var _is_builtin_module;
  19. function _load_is_builtin_module() {
  20. return (_is_builtin_module = _interopRequireDefault(
  21. require('./is_builtin_module')
  22. ));
  23. }
  24. var _node_modules_paths;
  25. function _load_node_modules_paths() {
  26. return (_node_modules_paths = _interopRequireDefault(
  27. require('./node_modules_paths')
  28. ));
  29. }
  30. function _interopRequireDefault(obj) {
  31. return obj && obj.__esModule ? obj : {default: obj};
  32. }
  33. function defaultResolver(path, options) {
  34. const resolve = options.browser
  35. ? (_browserResolve || _load_browserResolve()).default.sync
  36. : resolveSync;
  37. return resolve(path, {
  38. basedir: options.basedir,
  39. extensions: options.extensions,
  40. moduleDirectory: options.moduleDirectory,
  41. paths: options.paths,
  42. rootDir: options.rootDir
  43. });
  44. }
  45. /**
  46. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  47. *
  48. * This source code is licensed under the MIT license found in the
  49. * LICENSE file in the root directory of this source tree.
  50. *
  51. *
  52. */
  53. const REGEX_RELATIVE_IMPORT = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/;
  54. function resolveSync(target, options) {
  55. const basedir = options.basedir;
  56. const extensions = options.extensions || ['.js'];
  57. const paths = options.paths || [];
  58. if (REGEX_RELATIVE_IMPORT.test(target)) {
  59. // resolve relative import
  60. const resolveTarget = (_path || _load_path()).default.resolve(
  61. basedir,
  62. target
  63. );
  64. const result = tryResolve(resolveTarget);
  65. if (result) {
  66. return result;
  67. }
  68. } else {
  69. // otherwise search for node_modules
  70. const dirs = (0,
  71. (_node_modules_paths || _load_node_modules_paths()).default)(basedir, {
  72. moduleDirectory: options.moduleDirectory,
  73. paths: paths
  74. });
  75. for (let i = 0; i < dirs.length; i++) {
  76. const resolveTarget = (_path || _load_path()).default.join(
  77. dirs[i],
  78. target
  79. );
  80. const result = tryResolve(resolveTarget);
  81. if (result) {
  82. return result;
  83. }
  84. }
  85. }
  86. if ((0, (_is_builtin_module || _load_is_builtin_module()).default)(target)) {
  87. return target;
  88. }
  89. const err = new Error(
  90. "Cannot find module '" + target + "' from '" + basedir + "'"
  91. );
  92. err.code = 'MODULE_NOT_FOUND';
  93. throw err;
  94. /*
  95. * contextual helper functions
  96. */
  97. function tryResolve(name) {
  98. const dir = (_path || _load_path()).default.dirname(name);
  99. let result;
  100. if (isDirectory(dir)) {
  101. result = resolveAsFile(name) || resolveAsDirectory(name);
  102. }
  103. if (result) {
  104. // Dereference symlinks to ensure we don't create a separate
  105. // module instance depending on how it was referenced.
  106. result = (_fs || _load_fs()).default.realpathSync(result);
  107. }
  108. return result;
  109. }
  110. function resolveAsFile(name) {
  111. if (isFile(name)) {
  112. return name;
  113. }
  114. for (let i = 0; i < extensions.length; i++) {
  115. const file = name + extensions[i];
  116. if (isFile(file)) {
  117. return file;
  118. }
  119. }
  120. return undefined;
  121. }
  122. function resolveAsDirectory(name) {
  123. if (!isDirectory(name)) {
  124. return undefined;
  125. }
  126. const pkgfile = (_path || _load_path()).default.join(name, 'package.json');
  127. let pkgmain;
  128. try {
  129. const body = (_fs || _load_fs()).default.readFileSync(pkgfile, 'utf8');
  130. pkgmain = JSON.parse(body).main;
  131. } catch (e) {}
  132. if (pkgmain && !isCurrentDirectory(pkgmain)) {
  133. const resolveTarget = (_path || _load_path()).default.resolve(
  134. name,
  135. pkgmain
  136. );
  137. const result = tryResolve(resolveTarget);
  138. if (result) {
  139. return result;
  140. }
  141. }
  142. return resolveAsFile((_path || _load_path()).default.join(name, 'index'));
  143. }
  144. }
  145. /*
  146. * helper functions
  147. */
  148. function isFile(file) {
  149. let result;
  150. try {
  151. const stat = (_fs || _load_fs()).default.statSync(file);
  152. result = stat.isFile() || stat.isFIFO();
  153. } catch (e) {
  154. if (!(e && e.code === 'ENOENT')) {
  155. throw e;
  156. }
  157. result = false;
  158. }
  159. return result;
  160. }
  161. function isDirectory(dir) {
  162. let result;
  163. try {
  164. const stat = (_fs || _load_fs()).default.statSync(dir);
  165. result = stat.isDirectory();
  166. } catch (e) {
  167. if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
  168. throw e;
  169. }
  170. result = false;
  171. }
  172. return result;
  173. }
  174. function isCurrentDirectory(testPath) {
  175. return (
  176. (_path || _load_path()).default.resolve('.') ===
  177. (_path || _load_path()).default.resolve(testPath)
  178. );
  179. }