Skip to content

Commit 3ca01df

Browse files
committed
feat: add proper monorepo support
1 parent c6e4274 commit 3ca01df

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This Bun plugin generates dts files for your TypeScript project.
1414

1515
- Automatic dts generation based on your entrypoints
1616
- Honors & inherits your `tsconfig.json` settings
17+
- Monorepo support
1718

1819
## Usage
1920

build.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import process from 'node:process'
22
import dts from './src/index'
33

4-
// eslint-disable-next-line no-console
54
console.log('Building...')
65

76
const result = await Bun.build({
87
entrypoints: ['src/index.ts'],
98
outdir: 'dist',
109
target: 'bun',
1110

12-
plugins: [
13-
dts({
14-
cwd: import.meta.dir,
15-
}),
16-
],
11+
plugins: [dts()],
1712
})
1813

1914
if (!result.success) {

src/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
7676
const base = options?.base ?? cwd
7777
const rootDir = `${cwd}/${root}`
7878

79+
// Merge the base tsconfig with the package-specific one
80+
const mergedConfig = {
81+
...configJson,
82+
compilerOptions: {
83+
...configJson.compilerOptions,
84+
...options?.compiler,
85+
},
86+
}
87+
7988
const opts: TsOptions = {
8089
base,
8190
baseUrl: base,
@@ -87,10 +96,14 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
8796
...(options?.include && { include: options.include }),
8897
}
8998

90-
const parsedConfig = ts.parseJsonConfigFileContent(configJson, ts.sys, cwd, opts, path)
99+
const parsedConfig = ts.parseJsonConfigFileContent(mergedConfig, ts.sys, cwd, opts, path)
91100
parsedConfig.options.emitDeclarationOnly = true
92-
// console.log('Root directory:', rootDir)
93-
// console.log('Output directory:', parsedConfig.options.outDir)
101+
102+
// Use the outdir from options if provided, otherwise use the one from tsconfig
103+
const outDir = options?.outdir || parsedConfig.options.outDir || 'dist'
104+
105+
// Ensure outDir is relative to the package root, not the monorepo root
106+
parsedConfig.options.outDir = p.resolve(cwd, outDir)
94107

95108
const host = ts.createCompilerHost(parsedConfig.options)
96109

@@ -102,10 +115,7 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
102115
if ('isDeclarationFile' in sourceFile) {
103116
const originalFileName = sourceFile.fileName
104117
const entryPointName = p.basename(originalFileName, '.ts')
105-
// console.log('originalFileName', originalFileName)
106-
// console.log('entryPointName', entryPointName)
107118
const newFileName = p.join(parsedConfig.options.outDir || 'dist', `${entryPointName}.d.ts`)
108-
// console.log('newFileName', newFileName)
109119

110120
return ts.factory.updateSourceFile(
111121
sourceFile,
@@ -123,27 +133,20 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
123133
],
124134
}
125135

126-
// console.log('Parsed config:', JSON.stringify(parsedConfig.options, null, 2))
127-
128136
const program = ts.createProgram({
129137
rootNames: Array.isArray(entryPoints) ? entryPoints : [entryPoints],
130138
options: parsedConfig.options,
131139
host,
132140
})
133141

134-
// console.log('Program created with root names:', program.getRootFileNames())
135-
136-
// const emitResult = program.emit(
137142
program.emit(
138143
undefined,
139144
(fileName, data) => {
140145
if (fileName.endsWith('.d.ts')) {
141-
// console.log('Emitting declaration file:', fileName)
142146
const outputPath = p.join(parsedConfig.options.outDir || 'dist', p.relative(rootDir, fileName))
143-
// console.log('Attempting to write file:', outputPath)
144147
try {
145148
ts.sys.writeFile(outputPath, data)
146-
// console.log('Successfully wrote file:', outputPath)
149+
console.log('Successfully wrote file:', outputPath)
147150
} catch (error) {
148151
console.error('Error writing file:', outputPath, error)
149152
}
@@ -169,12 +172,13 @@ export function dts(options?: DtsOptions): BunPlugin {
169172

170173
async setup(build) {
171174
const entrypoints = [...build.config.entrypoints].sort()
172-
const root = build.config.root ?? 'src'
175+
const root = options?.root ?? build.config.root ?? 'src'
173176

174177
await generate(entrypoints, {
175178
root,
176179
include: entrypoints,
177-
outdir: build.config.outdir,
180+
outdir: options?.outdir || build.config.outdir,
181+
cwd: options?.cwd || process.cwd(),
178182
...options,
179183
})
180184
},

0 commit comments

Comments
 (0)