{"version":3,"names":["_async","require","_path","data","_url","_semver","_debug","_rewriteStackTrace","_configError","_transformFile","asyncGeneratorStep","gen","resolve","reject","_next","_throw","key","arg","info","value","error","done","Promise","then","_asyncToGenerator","fn","self","args","arguments","apply","err","undefined","debug","buildDebug","import_","_unused","supportsESM","exports","semver","satisfies","process","versions","node","loadCodeDefault","filepath","asyncError","path","extname","loadCjsDefault","loadCtsDefault","e","code","isAsync","waitFor","loadMjsDefault","ConfigError","ext","hasTsSupport","extensions","handler","opts","babelrc","configFile","sourceType","sourceMaps","sourceFileName","basename","presets","getTSPreset","Object","assign","onlyRemoveTypeImports","optimizeConstEnums","allowDeclareFields","m","filename","endsWith","_compile","transformFileSync","packageJson","lt","version","console","LOADING_CJS_FILES","Set","has","module","add","endHiddenCallStack","delete","_module","__esModule","default","_x","_loadMjsDefault","pathToFileURL","message","pnp"],"sources":["../../../src/config/files/module-types.ts"],"sourcesContent":["import { isAsync, waitFor } from \"../../gensync-utils/async.ts\";\nimport type { Handler } from \"gensync\";\nimport path from \"path\";\nimport { pathToFileURL } from \"url\";\nimport { createRequire } from \"module\";\nimport semver from \"semver\";\nimport buildDebug from \"debug\";\n\nimport { endHiddenCallStack } from \"../../errors/rewrite-stack-trace.ts\";\nimport ConfigError from \"../../errors/config-error.ts\";\n\nimport type { InputOptions } from \"../index.ts\";\nimport { transformFileSync } from \"../../transform-file.ts\";\n\nconst debug = buildDebug(\"babel:config:loading:files:module-types\");\n\nconst require = createRequire(import.meta.url);\n\nlet import_: ((specifier: string | URL) => any) | undefined;\ntry {\n  // Old Node.js versions don't support import() syntax.\n  import_ = require(\"./import.cjs\");\n} catch {}\n\nexport const supportsESM = semver.satisfies(\n  process.versions.node,\n  // older versions, starting from 10, support the dynamic\n  // import syntax but always return a rejected promise.\n  \"^12.17 || >=13.2\",\n);\n\nexport default function* loadCodeDefault(\n  filepath: string,\n  asyncError: string,\n): Handler<unknown> {\n  switch (path.extname(filepath)) {\n    case \".cjs\":\n      if (process.env.BABEL_8_BREAKING) {\n        return loadCjsDefault(filepath);\n      } else {\n        return loadCjsDefault(\n          filepath,\n          // @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8\n          /* fallbackToTranspiledModule */ arguments[2],\n        );\n      }\n    case \".mjs\":\n      break;\n    case \".cts\":\n      return loadCtsDefault(filepath);\n    default:\n      try {\n        if (process.env.BABEL_8_BREAKING) {\n          return loadCjsDefault(filepath);\n        } else {\n          return loadCjsDefault(\n            filepath,\n            // @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8\n            /* fallbackToTranspiledModule */ arguments[2],\n          );\n        }\n      } catch (e) {\n        if (e.code !== \"ERR_REQUIRE_ESM\") throw e;\n      }\n  }\n  if (yield* isAsync()) {\n    return yield* waitFor(loadMjsDefault(filepath));\n  }\n  throw new ConfigError(asyncError, filepath);\n}\n\nfunction loadCtsDefault(filepath: string) {\n  const ext = \".cts\";\n  const hasTsSupport = !!(\n    require.extensions[\".ts\"] ||\n    require.extensions[\".cts\"] ||\n    require.extensions[\".mts\"]\n  );\n\n  let handler: NodeJS.RequireExtensions[\"\"];\n\n  if (!hasTsSupport) {\n    const opts: InputOptions = {\n      babelrc: false,\n      configFile: false,\n      sourceType: \"unambiguous\",\n      sourceMaps: \"inline\",\n      sourceFileName: path.basename(filepath),\n      presets: [\n        [\n          getTSPreset(filepath),\n          {\n            onlyRemoveTypeImports: true,\n            optimizeConstEnums: true,\n            ...(process.env.BABEL_8_BREAKING\n              ? {}\n              : { allowDeclareFields: true }),\n          },\n        ],\n      ],\n    };\n\n    handler = function (m, filename) {\n      // If we want to support `.ts`, `.d.ts` must be handled specially.\n      if (handler && filename.endsWith(ext)) {\n        try {\n          // @ts-expect-error Undocumented API\n          return m._compile(\n            transformFileSync(filename, {\n              ...opts,\n              filename,\n            }).code,\n            filename,\n          );\n        } catch (error) {\n          if (!hasTsSupport) {\n            const packageJson = require(\"@babel/preset-typescript/package.json\");\n            if (semver.lt(packageJson.version, \"7.21.4\")) {\n              console.error(\n                \"`.cts` configuration file failed to load, please try to update `@babel/preset-typescript`.\",\n              );\n            }\n          }\n          throw error;\n        }\n      }\n      return require.extensions[\".js\"](m, filename);\n    };\n    require.extensions[ext] = handler;\n  }\n  try {\n    return loadCjsDefault(filepath);\n  } finally {\n    if (!hasTsSupport) {\n      if (require.extensions[ext] === handler) delete require.extensions[ext];\n      handler = undefined;\n    }\n  }\n}\n\nconst LOADING_CJS_FILES = new Set();\n\nfunction loadCjsDefault(filepath: string) {\n  // The `require()` call below can make this code reentrant if a require hook\n  // like @babel/register has been loaded into the system. That would cause\n  // Babel to attempt to compile the `.babelrc.js` file as it loads below. To\n  // cover this case, we auto-ignore re-entrant config processing. ESM loaders\n  // do not have this problem, because loaders do not apply to themselves.\n  if (LOADING_CJS_FILES.has(filepath)) {\n    debug(\"Auto-ignoring usage of config %o.\", filepath);\n    return {};\n  }\n\n  let module;\n  try {\n    LOADING_CJS_FILES.add(filepath);\n    module = endHiddenCallStack(require)(filepath);\n  } finally {\n    LOADING_CJS_FILES.delete(filepath);\n  }\n\n  if (process.env.BABEL_8_BREAKING) {\n    return module?.__esModule ? module.default : module;\n  } else {\n    return module?.__esModule\n      ? module.default ||\n          /* fallbackToTranspiledModule */ (arguments[1] ? module : undefined)\n      : module;\n  }\n}\n\nasync function loadMjsDefault(filepath: string) {\n  if (!import_) {\n    throw new ConfigError(\n      \"Internal error: Native ECMAScript modules aren't supported by this platform.\\n\",\n      filepath,\n    );\n  }\n\n  // import() expects URLs, not file paths.\n  // https://github.com/nodejs/node/issues/31710\n  const module = await endHiddenCallStack(import_)(pathToFileURL(filepath));\n  return module.default;\n}\n\nfunction getTSPreset(filepath: string) {\n  try {\n    // eslint-disable-next-line import/no-extraneous-dependencies\n    return require(\"@babel/preset-typescript\");\n  } catch (error) {\n    if (error.code !== \"MODULE_NOT_FOUND\") throw error;\n\n    let message =\n      \"You appear to be using a .cts file as Babel configuration, but the `@babel/preset-typescript` package was not found: please install it!\";\n\n    if (!process.env.BABEL_8_BREAKING) {\n      if (process.versions.pnp) {\n        // Using Yarn PnP, which doesn't allow requiring packages that are not\n        // explicitly specified as dependencies.\n        message += `\nIf you are using Yarn Plug'n'Play, you may also need to add the following configuration to your .yarnrc.yml file:\n\npackageExtensions:\n\\t\"@babel/core@*\":\n\\t\\tpeerDependencies:\n\\t\\t\\t\"@babel/preset-typescript\": \"*\"\n`;\n      }\n    }\n\n    throw new ConfigError(message, filepath);\n  }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,SAAAC,MAAA;EAAA,MAAAC,IAAA,GAAAF,OAAA;EAAAC,KAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAC,KAAA;EAAA,MAAAD,IAAA,GAAAF,OAAA;EAAAG,IAAA,YAAAA,CAAA;IAAA,OAAAD,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAEA,SAAAE,QAAA;EAAA,MAAAF,IAAA,GAAAF,OAAA;EAAAI,OAAA,YAAAA,CAAA;IAAA,OAAAF,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAG,OAAA;EAAA,MAAAH,IAAA,GAAAF,OAAA;EAAAK,MAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAEA,IAAAI,kBAAA,GAAAN,OAAA;AACA,IAAAO,YAAA,GAAAP,OAAA;AAGA,IAAAQ,cAAA,GAAAR,OAAA;AAA4D,SAAAS,mBAAAC,GAAA,EAAAC,OAAA,EAAAC,MAAA,EAAAC,KAAA,EAAAC,MAAA,EAAAC,GAAA,EAAAC,GAAA,cAAAC,IAAA,GAAAP,GAAA,CAAAK,GAAA,EAAAC,GAAA,OAAAE,KAAA,GAAAD,IAAA,CAAAC,KAAA,WAAAC,KAAA,IAAAP,MAAA,CAAAO,KAAA,iBAAAF,IAAA,CAAAG,IAAA,IAAAT,OAAA,CAAAO,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