| 5 | import path from "node:path" |
| 6 | |
| 7 | export async function pack() { |
| 8 | const original = await Bun.file("package.json").text() |
| 9 | const pkg = JSON.parse(original) as { |
| 10 | name: string |
| 11 | version: string |
| 12 | exports: Record<string, string | { types: string; import: string }> |
| 13 | } |
| 14 | const tarball = path.resolve(`${pkg.name.replace("@", "").replace("/", "-")}-${pkg.version}.tgz`) |
| 15 | |
| 16 | await $`bun run build` |
| 17 | pkg.exports = Object.fromEntries( |
| 18 | Object.entries(pkg.exports).map(([key, value]) => { |
| 19 | if (typeof value !== "string" || (!value.endsWith(".ts") && !value.endsWith(".tsx"))) return [key, value] |
| 20 | return [ |
| 21 | key, |
| 22 | { |
| 23 | types: value.replace("./src/", "./dist/").replace(/\.tsx?$/, ".d.ts"), |
| 24 | import: value, |
| 25 | }, |
| 26 | ] |
| 27 | }), |
| 28 | ) |
| 29 | |
| 30 | await rm(tarball, { force: true }) |
| 31 | await Bun.write("package.json", JSON.stringify(pkg, null, 2) + "\n") |
| 32 | try { |
| 33 | await $`bun pm pack` |
| 34 | return tarball |
| 35 | } finally { |
| 36 | await Bun.write("package.json", original) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if (import.meta.main) console.log(await pack()) |