defaultResolver.js 5.5 KB

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