1- import { basename } from 'node:path'
1+ import { basename , extname } from 'node:path'
22import { createResolver } from 'dts-resolver'
33import { isolatedDeclaration as oxcIsolatedDeclaration } from 'oxc-transform'
44import {
55 filename_dts_to ,
66 filename_ts_to_dts ,
77 isRelative ,
88 RE_DTS ,
9+ RE_JS ,
910 RE_NODE_MODULES ,
1011 RE_TS ,
1112} from './utils/filename'
@@ -16,11 +17,19 @@ const meta = { dtsFile: true } as const
1617
1718export function createGeneratePlugin ( {
1819 isolatedDeclaration,
19- inputAlias = { } ,
20+ inputAlias,
2021 resolve = false ,
2122} : Pick < Options , 'isolatedDeclaration' | 'inputAlias' | 'resolve' > ) : Plugin {
22- const dtsMap = new Map < string , string > ( )
23- const inputAliasMap = new Map < string , string > ( Object . entries ( inputAlias ) )
23+ const dtsMap = new Map <
24+ string ,
25+ {
26+ code : string
27+ src : string
28+ }
29+ > ( )
30+ const inputAliasMap = new Map < string , string > (
31+ inputAlias && Object . entries ( inputAlias ) ,
32+ )
2433 const resolver = createResolver ( )
2534
2635 let inputOption : Record < string , string > | undefined
@@ -33,6 +42,22 @@ export function createGeneratePlugin({
3342 }
3443 } ,
3544
45+ outputOptions ( options ) {
46+ return {
47+ ...options ,
48+ entryFileNames ( chunk ) {
49+ const original =
50+ ( typeof options . entryFileNames === 'function'
51+ ? options . entryFileNames ( chunk )
52+ : options . entryFileNames ) || '[name].js'
53+ if ( chunk . name . endsWith ( '.d' ) ) {
54+ return original . replace ( RE_JS , '.$1ts' )
55+ }
56+ return original
57+ } ,
58+ }
59+ } ,
60+
3661 transform : {
3762 order : 'pre' ,
3863 filter : {
@@ -47,27 +72,26 @@ export function createGeneratePlugin({
4772 code ,
4873 isolatedDeclaration ,
4974 )
50- if ( errors . length ) {
51- return this . error ( errors [ 0 ] )
52- }
75+ if ( errors . length ) return this . error ( errors [ 0 ] )
5376
5477 const dtsId = filename_ts_to_dts ( id )
55- dtsMap . set ( dtsId , dtsCode )
78+ dtsMap . set ( dtsId , {
79+ code : dtsCode ,
80+ src : id ,
81+ } )
5682
5783 const mod = this . getModuleInfo ( id )
5884 if ( mod ?. isEntry ) {
59- let fileName = basename ( dtsId )
60-
61- if ( inputAliasMap . has ( fileName ) ) {
62- fileName = inputAliasMap . get ( fileName ) !
85+ let name : string | undefined = basename ( dtsId , extname ( dtsId ) )
86+ if ( inputAliasMap . has ( name ) ) {
87+ name = inputAliasMap . get ( name ) !
6388 } else if ( inputAliasMap . has ( dtsId ) ) {
64- fileName = inputAliasMap . get ( dtsId ) !
89+ name = inputAliasMap . get ( dtsId ) !
6590 }
66-
6791 this . emitFile ( {
6892 type : 'chunk' ,
6993 id : dtsId ,
70- fileName ,
94+ name ,
7195 } )
7296 }
7397 } ,
@@ -146,7 +170,7 @@ export function createGeneratePlugin({
146170 handler ( id ) {
147171 if ( dtsMap . has ( id ) ) {
148172 return {
149- code : dtsMap . get ( id ) ! ,
173+ code : dtsMap . get ( id ) ! . code ,
150174 moduleSideEffects : false ,
151175 }
152176 }
0 commit comments