11import { type Options , defineConfig } from 'tsdown'
2- import { copyFile , glob , mkdir } from 'node:fs/promises'
2+ import { copyFile , glob , mkdir , readFile , writeFile } from 'node:fs/promises'
33
44import path from 'node:path'
55import { exists , findUpConfig } from './utils/fs'
@@ -13,7 +13,9 @@ export const baseConfig: Options = {
1313 sourcemap : true ,
1414 hooks ( hooks ) {
1515 hooks . hook ( 'build:done' , async ( ctx ) => {
16- // Get the absolute base path
16+ // Get the absolute output directory
17+ const outDir = ctx . options . outDir ?? 'dist'
18+ // Get the absolute base directory
1719 const base = await findUpConfig ( 'framework' , 'package' , [ 'json' ] )
1820 if ( ! base ) return
1921 // Make globale DTS partern
@@ -29,6 +31,20 @@ export const baseConfig: Options = {
2931 // Copy required files only for current package
3032 if ( await exists ( entry ) && entry . includes ( ctx . options . cwd ) )
3133 copyFile ( entry , target )
34+ // Augment the d.ts file to the index.d.ts
35+ if ( entry . includes ( '.d.ts' ) ) {
36+ for await ( const indexFile of glob ( path . join ( outDir , 'index.d.*ts' ) ) ) {
37+ const reference = `/// <reference path="./${ path . basename ( entry ) } " />\n`
38+ if ( await exists ( indexFile ) ) {
39+ let content = await readFile ( indexFile , 'utf8' )
40+ // Only add if it’s not already there
41+ if ( ! content . includes ( reference . trim ( ) ) ) {
42+ content = reference + content
43+ await writeFile ( indexFile , content , 'utf8' )
44+ }
45+ }
46+ }
47+ }
3248 }
3349 } )
3450 } ,
0 commit comments