@@ -25,22 +25,23 @@ export async function build(options: build.Options): Promise<build.ReturnType> {
2525 await checkInput ( { cwd, outDir } )
2626
2727 const { assets, sources } = getEntries ( { cwd, pkgJson } )
28+ const sourceDir = getSourceDir ( { cwd, sources } )
2829
2930 if ( ! link ) {
3031 const result = await transpile ( { cwd, sources, tsgo } )
3132 tsConfig = result . tsConfig
3233 }
3334
34- await copyAssets ( { assets, cwd, outDir } )
35+ await copyAssets ( { assets, cwd, outDir, sourceDir } )
3536
3637 if ( link ) {
3738 await fs . rm ( outDir , { recursive : true } ) . catch ( ( ) => { } )
3839 await fs . mkdir ( outDir , { recursive : true } )
3940 }
4041
41- const sourceDir = getSourceDir ( { cwd, sources } )
4242 const packageJson = await decoratePackageJson ( pkgJson , { cwd, link, outDir, sourceDir, assets } )
4343
44+
4445 await writePackageJson ( cwd , packageJson )
4546
4647 return { packageJson, tsConfig }
@@ -159,10 +160,11 @@ export declare namespace checkPackageJson {
159160 * @returns Promise that resolves when assets are copied.
160161 */
161162export async function copyAssets ( options : copyAssets . Options ) : Promise < void > {
162- const { assets, cwd, outDir } = options
163+ const { assets, cwd, outDir, sourceDir } = options
164+ const relativeSourceDir = path . relative ( cwd , sourceDir )
163165
164166 for ( const asset of assets ) {
165- const relativePath = path . relative ( cwd , asset )
167+ const relativePath = path . relative ( cwd , asset ) . replace ( ` ${ relativeSourceDir } /` , '' )
166168 const destPath = path . resolve ( outDir , relativePath )
167169 await fs . mkdir ( path . dirname ( destPath ) , { recursive : true } )
168170 await fs . copyFile ( asset , destPath )
@@ -177,6 +179,8 @@ export declare namespace copyAssets {
177179 cwd : string
178180 /** Output directory. */
179181 outDir : string
182+ /** Source directory. */
183+ sourceDir : string
180184 }
181185}
182186
@@ -196,7 +200,8 @@ export async function decoratePackageJson(
196200 const relativeOutDir = `./${ path . relative ( cwd , outDir ) } `
197201 const relativeSourceDir = `./${ path . relative ( cwd , sourceDir ) } `
198202
199- const outAsset = ( name : string ) => `./${ path . join ( relativeOutDir , path . relative ( cwd , name ) ) } `
203+ const outAsset = ( name : string ) =>
204+ `./${ path . join ( relativeOutDir , path . relative ( cwd , name ) . replace ( `${ relativeSourceDir . slice ( 2 ) } /` , '' ) ) } `
200205 const outFile = ( name : string , ext : string = '' ) =>
201206 './' +
202207 path . join (
@@ -279,6 +284,20 @@ export async function decoratePackageJson(
279284 } catch { }
280285 }
281286
287+ function linkDtsExport ( entry : string ) {
288+ try {
289+ const destDtsAbsolute = path . resolve ( cwd , outAsset ( path . resolve ( cwd , entry ) ) )
290+ const dir = path . dirname ( destDtsAbsolute )
291+
292+ if ( ! fsSync . existsSync ( dir ) ) fsSync . mkdirSync ( dir , { recursive : true } )
293+
294+ const srcAbsolute = path . resolve ( cwd , entry )
295+ const srcRelativeDts = path . relative ( path . dirname ( destDtsAbsolute ) , srcAbsolute )
296+
297+ fsSync . symlinkSync ( srcRelativeDts , destDtsAbsolute , 'file' )
298+ } catch { }
299+ }
300+
282301 // Transform single `package.json#exports` entrypoints. They
283302 // must point to the source file. Otherwise, an error is thrown.
284303 //
@@ -291,6 +310,14 @@ export async function decoratePackageJson(
291310 // }
292311 if ( typeof value === 'string' ) {
293312 if ( value . startsWith ( relativeOutDir ) ) return [ key , value ]
313+ // Handle .d.ts files (type definitions only, no JS output)
314+ if ( / \. d \. [ m c ] ? t s $ / . test ( value ) ) {
315+ const absolutePath = path . resolve ( cwd , value )
316+ if ( link ) linkDtsExport ( value )
317+ if ( assets . includes ( absolutePath ) )
318+ return [ key , { types : outAsset ( absolutePath ) , src : value } ]
319+ return [ key , { types : outAsset ( absolutePath ) , src : value } ]
320+ }
294321 // Handle asset files (non-source files)
295322 if ( ! / \. ( m | c ) ? [ j t ] s x ? $ / . test ( value ) ) {
296323 const absolutePath = path . resolve ( cwd , value )
@@ -326,6 +353,14 @@ export async function decoratePackageJson(
326353 typeof value . src === 'string'
327354 ) {
328355 if ( value . src . startsWith ( relativeOutDir ) ) return [ key , value ]
356+ // Handle .d.ts files (type definitions only, no JS output)
357+ if ( / \. d \. [ m c ] ? t s $ / . test ( value . src ) ) {
358+ const absolutePath = path . resolve ( cwd , value . src )
359+ if ( link ) linkDtsExport ( value . src )
360+ if ( assets . includes ( absolutePath ) )
361+ return [ key , { types : outAsset ( absolutePath ) , src : value . src } ]
362+ return [ key , { types : outAsset ( absolutePath ) , src : value . src } ]
363+ }
329364 // Handle asset files (non-source files)
330365 if ( ! / \. ( m | c ) ? [ j t ] s x ? $ / . test ( value . src ) ) {
331366 if ( link ) return [ key , value ]
@@ -422,7 +457,9 @@ export function getEntries(options: getEntries.Options): getEntries.ReturnType {
422457 else throw new Error ( '`exports` field in package.json must have a `src` field' )
423458
424459 const absolutePath = path . resolve ( cwd , entryPath )
425- if ( / \. ( m | c ) ? [ j t ] s x ? $ / . test ( absolutePath ) ) sources . push ( absolutePath )
460+ // .d.ts files are type definitions (assets), not source files to transpile
461+ if ( / \. d \. [ m c ] ? t s $ / . test ( absolutePath ) ) assets . push ( absolutePath )
462+ else if ( / \. ( m | c ) ? [ j t ] s x ? $ / . test ( absolutePath ) ) sources . push ( absolutePath )
426463 else assets . push ( absolutePath )
427464 }
428465 } else if ( pkgJson . main ) sources . push ( path . resolve ( cwd , pkgJson . main ) )
0 commit comments