{"version":3,"file":"build-commonjs.d.ts","sourceRoot":"","sources":["../../src/build-commonjs.ts"],"names":[],"mappings":"AASA,OAAO,eAAe,CAAA;AAMtB,eAAO,MAAM,aAAa,iBAgCzB,CAAA","sourcesContent":["import chalk from 'chalk'\nimport { spawnSync } from 'node:child_process'\nimport { relative, resolve } from 'node:path/posix'\nimport buildFail from './build-fail.js'\nimport config from './config.js'\nimport * as console from './console.js'\nimport ifExist from './if-exist.js'\nimport polyfills from './polyfills.js'\nimport setFolderDialect from './set-folder-dialect.js'\nimport './tsconfig.js'\nimport tsc from './which-tsc.js'\n\nconst node = process.execPath\nconst { commonjsDialects = [] } = config\n\nexport const buildCommonJS = () => {\n  setFolderDialect('src', 'commonjs')\n  for (const d of ['commonjs', ...commonjsDialects]) {\n    const pf = polyfills.get(d === 'commonjs' ? 'cjs' : d)\n    console.debug(chalk.cyan.dim('building ' + d))\n    const res = spawnSync(node, [tsc, '-p', `.tshy/${d}.json`], {\n      stdio: 'inherit',\n    })\n    if (res.status || res.signal) {\n      setFolderDialect('src')\n      return buildFail(res)\n    }\n    setFolderDialect('.tshy-build/' + d, 'commonjs')\n    for (const [override, orig] of pf?.map.entries() ?? []) {\n      const stemFrom = resolve(\n        `.tshy-build/${d}`,\n        relative(resolve('src'), resolve(override)),\n      ).replace(/\\.cts$/, '')\n      const stemTo = resolve(\n        `.tshy-build/${d}`,\n        relative(resolve('src'), resolve(orig)),\n      ).replace(/\\.tsx?$/, '')\n      const stemToPath = `${stemTo}.js.map`\n      const stemToDtsPath = `${stemTo}.d.ts.map`\n      ifExist.unlink(stemToPath)\n      ifExist.unlink(stemToDtsPath)\n      ifExist.rename(`${stemFrom}.cjs`, `${stemTo}.js`)\n      ifExist.rename(`${stemFrom}.d.cts`, `${stemTo}.d.ts`)\n    }\n    console.error(chalk.cyan.bold('built commonjs'))\n  }\n  setFolderDialect('src')\n}\n"]}