{"version":3,"file":"exports.d.ts","sourceRoot":"","sources":["../../src/exports.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAEhB,eAAe,EAChB,MAAM,gBAAgB,CAAA;AAKvB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAGjD,OAAO,EAAE,OAAO,EAAE,UAAU,EAAc,MAAM,YAAY,CAAA;AAuC5D,eAAO,MAAM,YAAY,sGAG8C,CAAA;AAEvE,eAAO,MAAM,YAAY,sGAUtB,CAAA;AAoIH,eAAO,MAAM,OAAO;;eAoCnB,CAAA;;AAKD,wBAA0B","sourcesContent":["import { relative, resolve } from 'node:path/posix'\nimport type {\n  ConditionalValue,\n  ConditionalValueObject,\n  ExportsSubpaths,\n} from 'resolve-import'\nimport config from './config.js'\nimport dialects from './dialects.js'\nimport fail from './fail.js'\nimport pkg from './package.js'\nimport type { PolyfillSet } from './polyfills.js'\nimport polyfills from './polyfills.js'\nimport { resolveExport } from './resolve-export.js'\nimport { Package, TshyConfig, TshyExport } from './types.js'\nconst { esmDialects = [], commonjsDialects = [] } = config\n\nconst liveDev =\n  config.liveDev &&\n  process.env.npm_command !== 'publish' &&\n  process.env.npm_command !== 'pack'\n\nconst getTargetForDialectCondition = <T extends string>(\n  s: string | TshyExport | undefined | null,\n  dialect: T,\n  condition: T extends 'esm' ? 'import'\n  : T extends 'commonjs' ? 'require'\n  : T,\n  type: T extends 'esm' ? 'esm'\n  : T extends 'commonjs' ? 'commonjs'\n  : 'esm' | 'commonjs',\n  polyfills: Map<string, PolyfillSet> = new Map(),\n): string | undefined | null => {\n  if (s === undefined) return undefined\n  if (typeof s === 'string') {\n    // the excluded filename pattern\n    const xts = type === 'commonjs' ? '.mts' : '.cts'\n    if (s.endsWith(xts)) return undefined\n    const pf = dialect === 'commonjs' ? 'cjs' : dialect\n    const rel = relative(\n      resolve('./src'),\n      resolve(polyfills.get(pf)?.map.get(s) ?? s),\n    )\n    const target = liveDev ? rel : rel.replace(/\\.([mc]?)tsx?$/, '.$1js')\n    return (\n      !s || !s.startsWith('./src/') ? s\n      : dialects.includes(type) ? `./dist/${dialect}/${target}`\n      : undefined\n    )\n  }\n  return resolveExport(s, [condition])\n}\n\nexport const getImpTarget = (\n  s: string | TshyExport | undefined | null,\n  polyfills: Map<string, PolyfillSet> = new Map(),\n) => getTargetForDialectCondition(s, 'esm', 'import', 'esm', polyfills)\n\nexport const getReqTarget = (\n  s: string | TshyExport | undefined | null,\n  polyfills: Map<string, PolyfillSet> = new Map(),\n) =>\n  getTargetForDialectCondition(\n    s,\n    'commonjs',\n    'require',\n    'commonjs',\n    polyfills,\n  )\n\nconst getExports = (\n  c: TshyConfig,\n  pkgType: 'commonjs' | 'module',\n): Record<string, ConditionalValue> => {\n  // by this time it always exports, will get the default if missing\n  /* c8 ignore start */\n  if (!c.exports) {\n    fail('no exports on tshy config (is there code in ./src?)')\n    return process.exit(1)\n  }\n  /* c8 ignore stop */\n  const e: Record<string, ConditionalValue> = {}\n  for (const [sub, s] of Object.entries(c.exports)) {\n    // external export, not built by us\n    if (s !== null && (typeof s !== 'string' || !s.startsWith('./src/'))) {\n      // already been validated, just accept as-is\n      e[sub] = s as ConditionalValue\n      continue\n    }\n\n    /* c8 ignore next - already guarded */\n    if (s === null) continue\n\n    const impTarget = getImpTarget(s, polyfills)\n    const reqTarget = getReqTarget(s, polyfills)\n\n    // should be impossible\n    /* c8 ignore start */\n    if (!impTarget && !reqTarget) continue\n    /* c8 ignore stop */\n\n    const exp: ConditionalValueObject = (e[sub] = {})\n    if (impTarget) {\n      for (const d of esmDialects) {\n        const source = s && (polyfills.get(d)?.map.get(s) ?? s)\n\n        const target = getTargetForDialectCondition(\n          s,\n          d,\n          d,\n          'esm',\n          polyfills,\n        )\n        if (target) {\n          exp[d] =\n            liveDev ?\n              {\n                ...(pkgType === 'commonjs' ?\n                  getSourceDialects(source, c)\n                : {}),\n                default: target,\n              }\n            : {\n                ...(pkgType === 'commonjs' ?\n                  getSourceDialects(source, c)\n                : {}),\n                types: target.replace(/\\.js$/, '.d.ts'),\n                default: target,\n              }\n        }\n      }\n    }\n\n    if (reqTarget) {\n      for (const d of commonjsDialects) {\n        const source = s && (polyfills.get(d)?.map.get(s) ?? s)\n        const target = getTargetForDialectCondition(\n          s,\n          d,\n          d,\n          'commonjs',\n          polyfills,\n        )\n        if (target) {\n          exp[d] =\n            liveDev ?\n              {\n                ...(pkgType === 'module' ?\n                  getSourceDialects(source, c)\n                : {}),\n                default: target,\n              }\n            : {\n                ...(pkgType === 'module' ?\n                  getSourceDialects(source, c)\n                : {}),\n                types: target.replace(/\\.js$/, '.d.ts'),\n                default: target,\n              }\n        }\n      }\n    }\n\n    // put the default import/require after all the other special ones.\n    if (impTarget) {\n      exp.import =\n        liveDev ?\n          {\n            ...(pkgType === 'module' ? getSourceDialects(s, c) : {}),\n            default: impTarget,\n          }\n        : {\n            ...(pkgType === 'module' ? getSourceDialects(s, c) : {}),\n            types: impTarget.replace(/\\.(m?)js$/, '.d.$1ts'),\n            default: impTarget,\n          }\n    }\n    if (reqTarget) {\n      exp.require =\n        liveDev ?\n          {\n            ...(pkgType === 'commonjs' ? getSourceDialects(s, c) : {}),\n            default: reqTarget,\n          }\n        : {\n            ...(pkgType === 'commonjs' ? getSourceDialects(s, c) : {}),\n            types: reqTarget.replace(/\\.(c?)js$/, '.d.$1ts'),\n            default: reqTarget,\n          }\n    }\n  }\n  return e\n}\n\nconst getSourceDialects = (source: string, c: TshyConfig) => {\n  const { sourceDialects } = c\n  if (!sourceDialects) return {}\n  return Object.fromEntries(sourceDialects.map(s => [s, source]))\n}\n\nexport const setMain = (\n  c: TshyConfig | undefined,\n  pkg: Package & { exports: ExportsSubpaths },\n) => {\n  pkg.type = pkg.type === 'commonjs' ? 'commonjs' : 'module'\n  const mod = resolveExport(pkg.exports['.'], ['require'])\n  const main = c?.main ?? !!mod\n  if (main) {\n    if (!mod) {\n      fail(`could not resolve exports['.'] for tshy.main 'require'`)\n      return process.exit(1)\n    }\n    const types = resolveExport(pkg.exports['.'], ['require', 'types'])\n    pkg.main = mod\n    if (types && types !== mod) pkg.types = types\n    else delete pkg.types\n  } else {\n    if (c && c.main !== false) delete c.main\n    delete pkg.main\n    delete pkg.types\n  }\n\n  // Set the package module to exports[\".\"]\n  const importMod = resolveExport(pkg.exports['.'], ['import'])\n  const module = c?.module ?? !!importMod\n  if (module) {\n    if (!importMod) {\n      fail(`could not resolve exports['.'] for tshy.module 'import'`)\n      return process.exit(1)\n    }\n\n    pkg.module = importMod\n  } else {\n    if (c && c.module !== false) delete c.module\n    delete pkg.module\n  }\n}\n\npkg.exports = getExports(config, pkg.type)\n\nsetMain(config, pkg as Package & { exports: ExportsSubpaths })\nexport default pkg.exports\n"]}