|
1 |
| -import { existsSync, mkdirSync } from 'node:fs' |
2 |
| -import { join } from 'path' |
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { |
| 4 | + type CompilationOptions, |
| 5 | + type EntryPointConfig, |
| 6 | + generateDtsBundle, |
| 7 | +} from 'dts-bundle-generator' |
| 8 | +import { getTsconfig } from 'get-tsconfig' |
3 | 9 |
|
4 |
| -import type { BunPlugin } from 'bun' |
5 |
| - |
6 |
| -export const definitionGeneratorPlugin: BunPlugin = { |
7 |
| - name: 'Definition Generator Plugin', |
8 |
| - async setup(build) { |
9 |
| - const entrypoints = [...build.config.entrypoints].sort() |
| 10 | +type Options = Omit<EntryPointConfig, 'filePath'> & { |
| 11 | + compilationOptions?: CompilationOptions |
| 12 | +} |
10 | 13 |
|
11 |
| - const outDir = build.config.outdir || './dist' |
12 |
| - if (!existsSync(outDir)) { |
13 |
| - mkdirSync(outDir) |
14 |
| - } |
| 14 | +export const dts = (options?: Options): import('bun').BunPlugin => { |
| 15 | + return { |
| 16 | + name: 'bun-plugin-dts', |
| 17 | + async setup(build) { |
| 18 | + const { compilationOptions, ...rest } = options || {} |
15 | 19 |
|
16 |
| - await Promise.all( |
17 |
| - entrypoints.map(entry => { |
18 |
| - const srcFile = entry.replace(/^.*\//, '') |
19 |
| - const dtsFile = srcFile.replace(/\.[jtm]s$/, '.d.ts') |
20 |
| - const outFile = join(outDir, dtsFile) |
| 20 | + const entrypoints = [...build.config.entrypoints].sort() |
| 21 | + const entries = entrypoints.map(entry => { |
| 22 | + return { |
| 23 | + filePath: entry, |
| 24 | + ...rest, |
| 25 | + } |
| 26 | + }) |
21 | 27 |
|
22 |
| - return Bun.write(outFile, `export * from "../src/${srcFile}"`) |
| 28 | + const tsconfig = compilationOptions?.preferredConfigPath ?? getTsconfig()?.path |
| 29 | + const result = generateDtsBundle(entries, { |
| 30 | + ...compilationOptions, |
| 31 | + preferredConfigPath: tsconfig, |
23 | 32 | })
|
24 |
| - ) |
25 |
| - }, |
| 33 | + |
| 34 | + const outDir = build.config.outdir || './dist' |
| 35 | + if (!fs.existsSync(outDir)) { |
| 36 | + fs.mkdirSync(outDir) |
| 37 | + } |
| 38 | + |
| 39 | + await Promise.all( |
| 40 | + entrypoints.map((entry, index) => { |
| 41 | + const dtsFile = entry.replace(/^.*\//, '').replace(/\.[jtm]s$/, '.d.ts') |
| 42 | + const outFile = path.join(outDir, dtsFile) |
| 43 | + |
| 44 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 45 | + return Bun.write(outFile, result[index] as any) |
| 46 | + }) |
| 47 | + ) |
| 48 | + }, |
| 49 | + } |
26 | 50 | }
|
0 commit comments