400902d6e8495c62783a64b99f2cb9b11ccba3424b2de56db80dec31643cbe67d12e3d9518a06722bdcb9b026d03b0dbf9ac9bba497f14e3e957b3ef9fbdfc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loadCodeDefault;
  6. exports.supportsESM = void 0;
  7. var _async = require("../../gensync-utils/async.js");
  8. function _path() {
  9. const data = require("path");
  10. _path = function () {
  11. return data;
  12. };
  13. return data;
  14. }
  15. function _url() {
  16. const data = require("url");
  17. _url = function () {
  18. return data;
  19. };
  20. return data;
  21. }
  22. require("module");
  23. function _semver() {
  24. const data = require("semver");
  25. _semver = function () {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _debug() {
  31. const data = require("debug");
  32. _debug = function () {
  33. return data;
  34. };
  35. return data;
  36. }
  37. var _rewriteStackTrace = require("../../errors/rewrite-stack-trace.js");
  38. var _configError = require("../../errors/config-error.js");
  39. var _transformFile = require("../../transform-file.js");
  40. function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
  41. function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
  42. const debug = _debug()("babel:config:loading:files:module-types");
  43. {
  44. try {
  45. var import_ = require("./import.cjs");
  46. } catch (_unused) {}
  47. }
  48. const supportsESM = exports.supportsESM = _semver().satisfies(process.versions.node, "^12.17 || >=13.2");
  49. const LOADING_CJS_FILES = new Set();
  50. function loadCjsDefault(filepath) {
  51. if (LOADING_CJS_FILES.has(filepath)) {
  52. debug("Auto-ignoring usage of config %o.", filepath);
  53. return {};
  54. }
  55. let module;
  56. try {
  57. LOADING_CJS_FILES.add(filepath);
  58. module = (0, _rewriteStackTrace.endHiddenCallStack)(require)(filepath);
  59. } finally {
  60. LOADING_CJS_FILES.delete(filepath);
  61. }
  62. {
  63. return module != null && (module.__esModule || module[Symbol.toStringTag] === "Module") ? module.default || (arguments[1] ? module : undefined) : module;
  64. }
  65. }
  66. const loadMjsFromPath = (0, _rewriteStackTrace.endHiddenCallStack)(function () {
  67. var _loadMjsFromPath = _asyncToGenerator(function* (filepath) {
  68. const url = (0, _url().pathToFileURL)(filepath).toString() + "?import";
  69. {
  70. if (!import_) {
  71. throw new _configError.default("Internal error: Native ECMAScript modules aren't supported by this platform.\n", filepath);
  72. }
  73. return yield import_(url);
  74. }
  75. });
  76. function loadMjsFromPath(_x) {
  77. return _loadMjsFromPath.apply(this, arguments);
  78. }
  79. return loadMjsFromPath;
  80. }());
  81. const SUPPORTED_EXTENSIONS = {
  82. ".js": "unknown",
  83. ".mjs": "esm",
  84. ".cjs": "cjs",
  85. ".ts": "unknown",
  86. ".mts": "esm",
  87. ".cts": "cjs"
  88. };
  89. const asyncModules = new Set();
  90. function* loadCodeDefault(filepath, loader, esmError, tlaError) {
  91. let async;
  92. const ext = _path().extname(filepath);
  93. const isTS = ext === ".ts" || ext === ".cts" || ext === ".mts";
  94. const type = SUPPORTED_EXTENSIONS[hasOwnProperty.call(SUPPORTED_EXTENSIONS, ext) ? ext : ".js"];
  95. const pattern = `${loader} ${type}`;
  96. switch (pattern) {
  97. case "require cjs":
  98. case "auto cjs":
  99. if (isTS) {
  100. return ensureTsSupport(filepath, ext, () => loadCjsDefault(filepath));
  101. } else {
  102. return loadCjsDefault(filepath, arguments[2]);
  103. }
  104. case "auto unknown":
  105. case "require unknown":
  106. case "require esm":
  107. try {
  108. if (isTS) {
  109. return ensureTsSupport(filepath, ext, () => loadCjsDefault(filepath));
  110. } else {
  111. return loadCjsDefault(filepath, arguments[2]);
  112. }
  113. } catch (e) {
  114. if (e.code === "ERR_REQUIRE_ASYNC_MODULE" || e.code === "ERR_REQUIRE_CYCLE_MODULE" && asyncModules.has(filepath)) {
  115. asyncModules.add(filepath);
  116. if (!(async != null ? async : async = yield* (0, _async.isAsync)())) {
  117. throw new _configError.default(tlaError, filepath);
  118. }
  119. } else if (e.code === "ERR_REQUIRE_ESM" || type === "esm") {} else {
  120. throw e;
  121. }
  122. }
  123. case "auto esm":
  124. if (async != null ? async : async = yield* (0, _async.isAsync)()) {
  125. const promise = isTS ? ensureTsSupport(filepath, ext, () => loadMjsFromPath(filepath)) : loadMjsFromPath(filepath);
  126. return (yield* (0, _async.waitFor)(promise)).default;
  127. }
  128. throw new _configError.default(esmError, filepath);
  129. default:
  130. throw new Error("Internal Babel error: unreachable code.");
  131. }
  132. }
  133. function ensureTsSupport(filepath, ext, callback) {
  134. if (require.extensions[".ts"] || require.extensions[".cts"] || require.extensions[".mts"]) {
  135. return callback();
  136. }
  137. if (ext !== ".cts") {
  138. throw new _configError.default(`\
  139. You are using a ${ext} config file, but Babel only supports transpiling .cts configs. Either:
  140. - Use a .cts config file
  141. - Update to Node.js 23.6.0, which has native TypeScript support
  142. - Install ts-node to transpile ${ext} files on the fly\
  143. `, filepath);
  144. }
  145. const opts = {
  146. babelrc: false,
  147. configFile: false,
  148. sourceType: "unambiguous",
  149. sourceMaps: "inline",
  150. sourceFileName: _path().basename(filepath),
  151. presets: [[getTSPreset(filepath), Object.assign({
  152. onlyRemoveTypeImports: true,
  153. optimizeConstEnums: true
  154. }, {
  155. allowDeclareFields: true
  156. })]]
  157. };
  158. let handler = function (m, filename) {
  159. if (handler && filename.endsWith(".cts")) {
  160. try {
  161. return m._compile((0, _transformFile.transformFileSync)(filename, Object.assign({}, opts, {
  162. filename
  163. })).code, filename);
  164. } catch (error) {
  165. const packageJson = require("@babel/preset-typescript/package.json");
  166. if (_semver().lt(packageJson.version, "7.21.4")) {
  167. console.error("`.cts` configuration file failed to load, please try to update `@babel/preset-typescript`.");
  168. }
  169. throw error;
  170. }
  171. }
  172. return require.extensions[".js"](m, filename);
  173. };
  174. require.extensions[ext] = handler;
  175. try {
  176. return callback();
  177. } finally {
  178. if (require.extensions[ext] === handler) delete require.extensions[ext];
  179. handler = undefined;
  180. }
  181. }
  182. function getTSPreset(filepath) {
  183. try {
  184. return require("@babel/preset-typescript");
  185. } catch (error) {
  186. if (error.code !== "MODULE_NOT_FOUND") throw error;
  187. let message = "You appear to be using a .cts file as Babel configuration, but the `@babel/preset-typescript` package was not found: please install it!";
  188. {
  189. if (process.versions.pnp) {
  190. message += `
  191. If you are using Yarn Plug'n'Play, you may also need to add the following configuration to your .yarnrc.yml file:
  192. packageExtensions:
  193. \t"@babel/core@*":
  194. \t\tpeerDependencies:
  195. \t\t\t"@babel/preset-typescript": "*"
  196. `;
  197. }
  198. }
  199. throw new _configError.default(message, filepath);
  200. }
  201. }
  202. 0 && 0;
  203. //# sourceMappingURL=module-types.js.map