| 31 | }) |
| 32 | |
| 33 | async function bundleInputs(specifier: string, target: "browser" | "bun") { |
| 34 | const temporary = await mkdtemp(join(import.meta.dir, ".import-boundary-")) |
| 35 | const entrypoint = join(temporary, "index.ts") |
| 36 | const metafile = join(temporary, "meta.json") |
| 37 | try { |
| 38 | await Bun.write(entrypoint, `export * from ${JSON.stringify(specifier)}`) |
| 39 | const child = Bun.spawn( |
| 40 | [ |
| 41 | process.execPath, |
| 42 | "build", |
| 43 | entrypoint, |
| 44 | `--target=${target}`, |
| 45 | "--format=esm", |
| 46 | "--packages=bundle", |
| 47 | `--metafile=${metafile}`, |
| 48 | `--outdir=${join(temporary, "out")}`, |
| 49 | ], |
| 50 | { cwd: directory, stdout: "pipe", stderr: "pipe" }, |
| 51 | ) |
| 52 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 53 | child.exited, |
| 54 | new Response(child.stdout).text(), |
| 55 | new Response(child.stderr).text(), |
| 56 | ]) |
| 57 | if (exitCode !== 0) throw new Error(stdout + stderr) |
| 58 | const metadata = await Bun.file(metafile).json() |
| 59 | return Object.keys(metadata.inputs).map((input) => resolve(directory, input)) |
| 60 | } finally { |
| 61 | await rm(temporary, { recursive: true, force: true }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function within(inputs: ReadonlyArray<string>, directory: string) { |
| 66 | const prefix = directory.endsWith(sep) ? directory : directory + sep |