shouldLoadAsEsm.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.clearCachedLookups = clearCachedLookups;
  6. exports.default = cachedShouldLoadAsEsm;
  7. function _path() {
  8. const data = require('path');
  9. _path = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _vm() {
  15. const data = require('vm');
  16. _vm = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. function _readPkgUp() {
  22. const data = _interopRequireDefault(require('read-pkg-up'));
  23. _readPkgUp = function () {
  24. return data;
  25. };
  26. return data;
  27. }
  28. function _interopRequireDefault(obj) {
  29. return obj && obj.__esModule ? obj : {default: obj};
  30. }
  31. /**
  32. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  33. *
  34. * This source code is licensed under the MIT license found in the
  35. * LICENSE file in the root directory of this source tree.
  36. */
  37. // @ts-expect-error: experimental, not added to the types
  38. const runtimeSupportsVmModules = typeof _vm().SyntheticModule === 'function';
  39. const cachedFileLookups = new Map();
  40. const cachedDirLookups = new Map();
  41. function clearCachedLookups() {
  42. cachedFileLookups.clear();
  43. cachedDirLookups.clear();
  44. }
  45. function cachedShouldLoadAsEsm(path) {
  46. let cachedLookup = cachedFileLookups.get(path);
  47. if (cachedLookup === undefined) {
  48. cachedLookup = shouldLoadAsEsm(path);
  49. cachedFileLookups.set(path, cachedLookup);
  50. }
  51. return cachedLookup;
  52. } // this is a bad version of what https://github.com/nodejs/modules/issues/393 would provide
  53. function shouldLoadAsEsm(path) {
  54. if (!runtimeSupportsVmModules) {
  55. return false;
  56. }
  57. const extension = (0, _path().extname)(path);
  58. if (extension === '.mjs') {
  59. return true;
  60. }
  61. if (extension === '.cjs') {
  62. return false;
  63. } // this isn't correct - we might wanna load any file as a module (using synthetic module)
  64. // do we need an option to Jest so people can opt in to ESM for non-js?
  65. if (extension !== '.js') {
  66. return false;
  67. }
  68. const cwd = (0, _path().dirname)(path);
  69. let cachedLookup = cachedDirLookups.get(cwd);
  70. if (cachedLookup === undefined) {
  71. cachedLookup = cachedPkgCheck(cwd);
  72. cachedFileLookups.set(cwd, cachedLookup);
  73. }
  74. return cachedLookup;
  75. }
  76. function cachedPkgCheck(cwd) {
  77. // TODO: can we cache lookups somehow?
  78. const pkg = _readPkgUp().default.sync({
  79. cwd,
  80. normalize: false
  81. });
  82. if (!pkg) {
  83. return false;
  84. }
  85. return pkg.packageJson.type === 'module';
  86. }