nodeModulesPaths.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = nodeModulesPaths;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestUtil() {
  14. const data = require('jest-util');
  15. _jestUtil = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _getRequireWildcardCache() {
  21. if (typeof WeakMap !== 'function') return null;
  22. var cache = new WeakMap();
  23. _getRequireWildcardCache = function () {
  24. return cache;
  25. };
  26. return cache;
  27. }
  28. function _interopRequireWildcard(obj) {
  29. if (obj && obj.__esModule) {
  30. return obj;
  31. }
  32. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  33. return {default: obj};
  34. }
  35. var cache = _getRequireWildcardCache();
  36. if (cache && cache.has(obj)) {
  37. return cache.get(obj);
  38. }
  39. var newObj = {};
  40. var hasPropertyDescriptor =
  41. Object.defineProperty && Object.getOwnPropertyDescriptor;
  42. for (var key in obj) {
  43. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  44. var desc = hasPropertyDescriptor
  45. ? Object.getOwnPropertyDescriptor(obj, key)
  46. : null;
  47. if (desc && (desc.get || desc.set)) {
  48. Object.defineProperty(newObj, key, desc);
  49. } else {
  50. newObj[key] = obj[key];
  51. }
  52. }
  53. }
  54. newObj.default = obj;
  55. if (cache) {
  56. cache.set(obj, newObj);
  57. }
  58. return newObj;
  59. }
  60. /**
  61. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  62. *
  63. * This source code is licensed under the MIT license found in the
  64. * LICENSE file in the root directory of this source tree.
  65. *
  66. * Adapted from: https://github.com/substack/node-resolve
  67. */
  68. function nodeModulesPaths(basedir, options) {
  69. const modules =
  70. options && options.moduleDirectory
  71. ? Array.from(options.moduleDirectory)
  72. : ['node_modules']; // ensure that `basedir` is an absolute path at this point,
  73. // resolving against the process' current working directory
  74. const basedirAbs = path().resolve(basedir);
  75. let prefix = '/';
  76. if (/^([A-Za-z]:)/.test(basedirAbs)) {
  77. prefix = '';
  78. } else if (/^\\\\/.test(basedirAbs)) {
  79. prefix = '\\\\';
  80. } // The node resolution algorithm (as implemented by NodeJS and TypeScript)
  81. // traverses parents of the physical path, not the symlinked path
  82. let physicalBasedir;
  83. try {
  84. physicalBasedir = (0, _jestUtil().tryRealpath)(basedirAbs);
  85. } catch {
  86. // realpath can throw, e.g. on mapped drives
  87. physicalBasedir = basedirAbs;
  88. }
  89. const paths = [physicalBasedir];
  90. let parsed = path().parse(physicalBasedir);
  91. while (parsed.dir !== paths[paths.length - 1]) {
  92. paths.push(parsed.dir);
  93. parsed = path().parse(parsed.dir);
  94. }
  95. const dirs = paths
  96. .reduce(
  97. (dirs, aPath) =>
  98. dirs.concat(
  99. modules.map(moduleDir =>
  100. path().isAbsolute(moduleDir)
  101. ? aPath === basedirAbs
  102. ? moduleDir
  103. : ''
  104. : path().join(prefix, aPath, moduleDir)
  105. )
  106. ),
  107. []
  108. )
  109. .filter(dir => dir !== '');
  110. return options.paths ? dirs.concat(options.paths) : dirs;
  111. }