defaultResolver.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = defaultResolver;
  6. function _path() {
  7. const data = require('path');
  8. _path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestPnpResolver() {
  14. const data = _interopRequireDefault(require('jest-pnp-resolver'));
  15. _jestPnpResolver = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _resolve() {
  21. const data = require('resolve');
  22. _resolve = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. var _resolve2 = require('resolve.exports');
  28. var _fileWalkers = require('./fileWalkers');
  29. function _interopRequireDefault(obj) {
  30. return obj && obj.__esModule ? obj : {default: obj};
  31. }
  32. /**
  33. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  34. *
  35. * This source code is licensed under the MIT license found in the
  36. * LICENSE file in the root directory of this source tree.
  37. */
  38. function defaultResolver(path, options) {
  39. // Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
  40. // needed for Yarn 1 which implements version 1 of the pnp spec
  41. if (process.versions.pnp === '1') {
  42. return (0, _jestPnpResolver().default)(path, options);
  43. }
  44. const result = (0, _resolve().sync)(path, {
  45. ...options,
  46. isDirectory: _fileWalkers.isDirectory,
  47. isFile: _fileWalkers.isFile,
  48. packageFilter: createPackageFilter(
  49. options.conditions,
  50. options.packageFilter
  51. ),
  52. preserveSymlinks: false,
  53. readPackageSync,
  54. realpathSync: _fileWalkers.realpathSync
  55. }); // Dereference symlinks to ensure we don't create a separate
  56. // module instance depending on how it was referenced.
  57. return (0, _fileWalkers.realpathSync)(result);
  58. }
  59. /*
  60. * helper functions
  61. */
  62. function readPackageSync(_, file) {
  63. return (0, _fileWalkers.readPackageCached)(file);
  64. }
  65. function createPackageFilter(conditions, userFilter) {
  66. function attemptExportsFallback(pkg) {
  67. const options = conditions
  68. ? {
  69. conditions,
  70. unsafe: true
  71. } // no conditions were passed - let's assume this is Jest internal and it should be `require`
  72. : {
  73. browser: false,
  74. require: true
  75. };
  76. try {
  77. return (0, _resolve2.resolve)(pkg, '.', options);
  78. } catch {
  79. return undefined;
  80. }
  81. }
  82. return function packageFilter(pkg, packageDir) {
  83. let filteredPkg = pkg;
  84. if (userFilter) {
  85. filteredPkg = userFilter(filteredPkg, packageDir);
  86. }
  87. if (filteredPkg.main != null) {
  88. return filteredPkg;
  89. }
  90. const indexInRoot = (0, _path().resolve)(packageDir, './index.js'); // if the module contains an `index.js` file in root, `resolve` will request
  91. // that if there is no `main`. Since we don't wanna break that, add this
  92. // check
  93. if ((0, _fileWalkers.isFile)(indexInRoot)) {
  94. return filteredPkg;
  95. }
  96. return {...filteredPkg, main: attemptExportsFallback(filteredPkg)};
  97. };
  98. }