{"version":3,"file":"index.cjs","names":[],"sources":["../src/config.ts","../src/files.ts","../src/import.ts","../src/index.ts"],"sourcesContent":["export const VALID_FILE_EXTENSIONS = Object.freeze(['.js', '.mjs', '.cjs', '.ts']);\n","import fs from 'fs';\nimport path from 'path';\nimport { VALID_FILE_EXTENSIONS } from './config';\n\n/**\n * Get the file path of the caller function.\n *\n * Implementation inspired by:\n * - https://www.npmjs.com/package/callsite?activeTab=code\n *\n * @returns {string} absolute path or an empty string if no caller\n */\nexport const getCallerDirname = (): string => {\n  const orig = Error.prepareStackTrace;\n  Error.prepareStackTrace = (_, stack) => stack;\n  const err = new Error();\n  Error.captureStackTrace(err, getCallerDirname);\n  const stack = err.stack as any;\n  Error.prepareStackTrace = orig;\n  const callerFilePath = stack[1].getFileName();\n  /* istanbul ignore next */\n  return path.dirname(\n    callerFilePath.startsWith('file://') ? callerFilePath.substring(7) : callerFilePath,\n  );\n};\n\n/**\n * Find the module file path by checking for available extensions\n * as defined by VALID_FILE_EXTENSIONS\n *\n * @param {string} filePath The absolute path to the file\n * @returns {string} The resolved file path with appended extension\n * @throws {Error} If the file path does not match any valid extensions\n */\nconst findFileWithExtensions = (filePath: string): string => {\n  for (const ext of VALID_FILE_EXTENSIONS) {\n    const extFilePath = `${filePath}${ext}`;\n    if (fs.existsSync(extFilePath)) {\n      return extFilePath;\n    }\n  }\n  throw new Error(`No such file '${filePath}' with matching extensions [${VALID_FILE_EXTENSIONS}]`);\n};\n\n/**\n * Find the module file path\n *\n * @param {string} modulePath - The path to the module\n * @param {string} basePath - The base path for the module\n * @returns {string} The resolved file path\n * @throws {Error} If the file is not found\n */\nexport const findModuleFile = (basePath: string, modulePath: string): string => {\n  const filePath = path.join(basePath, modulePath);\n  return fs.existsSync(filePath) ? filePath : findFileWithExtensions(filePath);\n};\n","import esm from 'esm-sync';\n\nimport { findModuleFile, getCallerDirname } from './files';\nimport { Options } from './options';\n\n/**\n * Returns an ESM-imported module\n *\n * @param modulePath absolute path or id of the module to import\n * @param options same options as importSync\n * @returns the esm imported module\n */\nconst esmImport = (modulePath: string, options: Options) => {\n  const esmRequire = esm(module, options.esmOptions);\n  try {\n    return esmRequire(modulePath);\n  } catch (error: unknown) {\n    const message = error instanceof Error ? error.stack : error;\n    // eslint-disable-next-line preserve-caught-error\n    throw new Error(\n      `\nFailed to import from:\n  ${modulePath}\nOptions:\n  ${JSON.stringify(options)}\nRequire error message:\n  ${message}`,\n    );\n  }\n};\n\n/**\n * Imports ES6 modules synchronously similar to require in CommonJS\n * Can be used in both ES6 and CommonJS projects\n *\n * @param relativePath - the name or relative path of the module, e.g. ./arrays\n * @param {Options} [options] - options as defined in types.ts\n * @returns the imported module\n */\nconst importSync = (id: string, options: Options = {}) => {\n  const basePath = options.basePath ?? getCallerDirname();\n  const modulePath = /^\\.\\.?\\//.test(id) ? findModuleFile(basePath, id) : id;\n  const importedModule = esmImport(modulePath, options);\n\n  if (Object.keys(importedModule).length > 0) {\n    return importedModule;\n  }\n  // In case CJS shows up as empty, e.g. when importing CommonJS/CommonTS into Jest\n  try {\n    // eslint-disable-next-line @typescript-eslint/no-require-imports\n    const basicModule = require(modulePath);\n    /* istanbul ignore next */\n    if (Object.keys(basicModule).length > 0) {\n      return basicModule;\n    }\n  } catch (_error) {\n    /* nothing to do */\n  }\n\n  return importedModule;\n};\n\nexport default importSync;\n","import importSync from './import';\n\nexport default importSync;\nexport type { Options, ESMOptions } from './options';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAa,wBAAwB,OAAO,OAAO;CAAC;CAAO;CAAQ;CAAQ;CAAM,CAAC;;;;;;;;;;;;ACYlF,MAAa,yBAAiC;CAC5C,MAAM,OAAO,MAAM;AACnB,OAAM,qBAAqB,GAAG,UAAU;CACxC,MAAM,sBAAM,IAAI,OAAO;AACvB,OAAM,kBAAkB,KAAK,iBAAiB;CAC9C,MAAM,QAAQ,IAAI;AAClB,OAAM,oBAAoB;CAC1B,MAAM,iBAAiB,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