shouldLoadAsEsm.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. var _fileWalkers = require('./fileWalkers');
  22. /**
  23. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  24. *
  25. * This source code is licensed under the MIT license found in the
  26. * LICENSE file in the root directory of this source tree.
  27. */
  28. // @ts-expect-error: experimental, not added to the types
  29. const runtimeSupportsVmModules = typeof _vm().SyntheticModule === 'function';
  30. const cachedFileLookups = new Map();
  31. const cachedDirLookups = new Map();
  32. const cachedChecks = new Map();
  33. function clearCachedLookups() {
  34. cachedFileLookups.clear();
  35. cachedDirLookups.clear();
  36. cachedChecks.clear();
  37. }
  38. function cachedShouldLoadAsEsm(path, extensionsToTreatAsEsm) {
  39. if (!runtimeSupportsVmModules) {
  40. return false;
  41. }
  42. let cachedLookup = cachedFileLookups.get(path);
  43. if (cachedLookup === undefined) {
  44. cachedLookup = shouldLoadAsEsm(path, extensionsToTreatAsEsm);
  45. cachedFileLookups.set(path, cachedLookup);
  46. }
  47. return cachedLookup;
  48. } // this is a bad version of what https://github.com/nodejs/modules/issues/393 would provide
  49. function shouldLoadAsEsm(path, extensionsToTreatAsEsm) {
  50. const extension = (0, _path().extname)(path);
  51. if (extension === '.mjs') {
  52. return true;
  53. }
  54. if (extension === '.cjs') {
  55. return false;
  56. }
  57. if (extension !== '.js') {
  58. return extensionsToTreatAsEsm.includes(extension);
  59. }
  60. const cwd = (0, _path().dirname)(path);
  61. let cachedLookup = cachedDirLookups.get(cwd);
  62. if (cachedLookup === undefined) {
  63. cachedLookup = cachedPkgCheck(cwd);
  64. cachedFileLookups.set(cwd, cachedLookup);
  65. }
  66. return cachedLookup;
  67. }
  68. function cachedPkgCheck(cwd) {
  69. const pkgPath = (0, _fileWalkers.findClosestPackageJson)(cwd);
  70. if (!pkgPath) {
  71. return false;
  72. }
  73. let hasModuleField = cachedChecks.get(pkgPath);
  74. if (hasModuleField != null) {
  75. return hasModuleField;
  76. }
  77. try {
  78. const pkg = (0, _fileWalkers.readPackageCached)(pkgPath);
  79. hasModuleField = pkg.type === 'module';
  80. } catch {
  81. hasModuleField = false;
  82. }
  83. cachedChecks.set(pkgPath, hasModuleField);
  84. return hasModuleField;
  85. }