defaultResolver.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = defaultResolver;
  6. exports.clearDefaultResolverCache = clearDefaultResolverCache;
  7. function fs() {
  8. const data = _interopRequireWildcard(require('graceful-fs'));
  9. fs = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _resolve() {
  15. const data = require('resolve');
  16. _resolve = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. function _jestPnpResolver() {
  22. const data = _interopRequireDefault(require('jest-pnp-resolver'));
  23. _jestPnpResolver = function () {
  24. return data;
  25. };
  26. return data;
  27. }
  28. function _jestUtil() {
  29. const data = require('jest-util');
  30. _jestUtil = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. function _interopRequireDefault(obj) {
  36. return obj && obj.__esModule ? obj : {default: obj};
  37. }
  38. function _getRequireWildcardCache() {
  39. if (typeof WeakMap !== 'function') return null;
  40. var cache = new WeakMap();
  41. _getRequireWildcardCache = function () {
  42. return cache;
  43. };
  44. return cache;
  45. }
  46. function _interopRequireWildcard(obj) {
  47. if (obj && obj.__esModule) {
  48. return obj;
  49. }
  50. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  51. return {default: obj};
  52. }
  53. var cache = _getRequireWildcardCache();
  54. if (cache && cache.has(obj)) {
  55. return cache.get(obj);
  56. }
  57. var newObj = {};
  58. var hasPropertyDescriptor =
  59. Object.defineProperty && Object.getOwnPropertyDescriptor;
  60. for (var key in obj) {
  61. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  62. var desc = hasPropertyDescriptor
  63. ? Object.getOwnPropertyDescriptor(obj, key)
  64. : null;
  65. if (desc && (desc.get || desc.set)) {
  66. Object.defineProperty(newObj, key, desc);
  67. } else {
  68. newObj[key] = obj[key];
  69. }
  70. }
  71. }
  72. newObj.default = obj;
  73. if (cache) {
  74. cache.set(obj, newObj);
  75. }
  76. return newObj;
  77. }
  78. /**
  79. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  80. *
  81. * This source code is licensed under the MIT license found in the
  82. * LICENSE file in the root directory of this source tree.
  83. */
  84. function defaultResolver(path, options) {
  85. if (process.versions.pnp && options.allowPnp !== false) {
  86. return (0, _jestPnpResolver().default)(path, options);
  87. }
  88. const result = (0, _resolve().sync)(path, {
  89. basedir: options.basedir,
  90. extensions: options.extensions,
  91. isDirectory,
  92. isFile,
  93. moduleDirectory: options.moduleDirectory,
  94. packageFilter: options.packageFilter,
  95. paths: options.paths,
  96. preserveSymlinks: false,
  97. realpathSync
  98. }); // Dereference symlinks to ensure we don't create a separate
  99. // module instance depending on how it was referenced.
  100. return realpathSync(result);
  101. }
  102. function clearDefaultResolverCache() {
  103. checkedPaths.clear();
  104. checkedRealpathPaths.clear();
  105. }
  106. var IPathType;
  107. (function (IPathType) {
  108. IPathType[(IPathType['FILE'] = 1)] = 'FILE';
  109. IPathType[(IPathType['DIRECTORY'] = 2)] = 'DIRECTORY';
  110. IPathType[(IPathType['OTHER'] = 3)] = 'OTHER';
  111. })(IPathType || (IPathType = {}));
  112. const checkedPaths = new Map();
  113. function statSyncCached(path) {
  114. const result = checkedPaths.get(path);
  115. if (result !== undefined) {
  116. return result;
  117. }
  118. let stat;
  119. try {
  120. stat = fs().statSync(path);
  121. } catch (e) {
  122. if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
  123. throw e;
  124. }
  125. }
  126. if (stat) {
  127. if (stat.isFile() || stat.isFIFO()) {
  128. checkedPaths.set(path, IPathType.FILE);
  129. return IPathType.FILE;
  130. } else if (stat.isDirectory()) {
  131. checkedPaths.set(path, IPathType.DIRECTORY);
  132. return IPathType.DIRECTORY;
  133. }
  134. }
  135. checkedPaths.set(path, IPathType.OTHER);
  136. return IPathType.OTHER;
  137. }
  138. const checkedRealpathPaths = new Map();
  139. function realpathCached(path) {
  140. let result = checkedRealpathPaths.get(path);
  141. if (result !== undefined) {
  142. return result;
  143. }
  144. result = (0, _jestUtil().tryRealpath)(path);
  145. checkedRealpathPaths.set(path, result);
  146. if (path !== result) {
  147. // also cache the result in case it's ever referenced directly - no reason to `realpath` that as well
  148. checkedRealpathPaths.set(result, result);
  149. }
  150. return result;
  151. }
  152. /*
  153. * helper functions
  154. */
  155. function isFile(file) {
  156. return statSyncCached(file) === IPathType.FILE;
  157. }
  158. function isDirectory(dir) {
  159. return statSyncCached(dir) === IPathType.DIRECTORY;
  160. }
  161. function realpathSync(file) {
  162. return realpathCached(file);
  163. }