1- import { basename , extname } from 'node:path'
1+ import path from 'node:path'
2+ import process from 'node:process'
23import { createResolver } from 'dts-resolver'
34import { getTsconfig } from 'get-tsconfig'
45import { isolatedDeclaration as oxcIsolatedDeclaration } from 'oxc-transform'
@@ -25,25 +26,27 @@ const meta = { dtsFile: true } as const
2526export function createGeneratePlugin ( {
2627 compilerOptions,
2728 isolatedDeclaration,
28- inputAlias,
2929 resolve = false ,
3030 emitDtsOnly = false ,
3131} : Pick <
3232 Options ,
33- | 'isolatedDeclaration'
34- | 'inputAlias'
35- | 'resolve'
36- | 'emitDtsOnly'
37- | 'compilerOptions'
33+ 'isolatedDeclaration' | 'resolve' | 'emitDtsOnly' | 'compilerOptions'
3834> ) : Plugin {
3935 const dtsMap = new Map < string , { code : string ; src : string } > ( )
40- const inputAliasMap = new Map < string , string > (
41- inputAlias && Object . entries ( inputAlias ) ,
42- )
36+
37+ /**
38+ * A map of input id to output file name
39+ *
40+ * @example
41+ *
42+ * inputAlias = new Map([
43+ * ['/absolute/path/to/src/source_file.ts', 'dist/foo/index'],
44+ * ])
45+ */
46+ const inputAliasMap = new Map < string , string > ( )
4347 const resolver = createResolver ( )
4448 let programs : TsProgram [ ] = [ ]
4549
46- let inputOption : Record < string , string > | undefined
4750 return {
4851 name : 'rolldown-plugin-dts:generate' ,
4952
@@ -60,11 +63,13 @@ export function createGeneratePlugin({
6063 if ( ! isolatedDeclaration ) {
6164 initTs ( )
6265 }
63- } ,
6466
65- options ( { input } ) {
66- if ( isPlainObject ( input ) ) {
67- inputOption = { ...input }
67+ if ( ! Array . isArray ( options . input ) ) {
68+ const cwd = options . cwd || process . cwd ( )
69+ for ( const [ fileName , inputFilePath ] of Object . entries ( options . input ) ) {
70+ const id = path . resolve ( cwd , inputFilePath )
71+ inputAliasMap . set ( id , fileName )
72+ }
6873 }
6974 } ,
7075
@@ -138,16 +143,11 @@ export function createGeneratePlugin({
138143 } )
139144
140145 if ( isEntry ) {
141- let name : string | undefined = basename ( dtsId , extname ( dtsId ) )
142- if ( inputAliasMap . has ( name ) ) {
143- name = inputAliasMap . get ( name ) !
144- } else if ( inputAliasMap . has ( dtsId ) ) {
145- name = inputAliasMap . get ( dtsId ) !
146- }
146+ const name = inputAliasMap . get ( id )
147147 this . emitFile ( {
148148 type : 'chunk' ,
149149 id : dtsId ,
150- name,
150+ name : name ? ` ${ name } .d` : undefined ,
151151 } )
152152
153153 if ( emitDtsOnly ) {
@@ -157,7 +157,7 @@ export function createGeneratePlugin({
157157 } ,
158158 } ,
159159
160- async resolveId ( id , importer , extraOptions ) {
160+ async resolveId ( id , importer ) {
161161 // must be entry
162162 if ( dtsMap . has ( id ) ) {
163163 return { id, meta }
@@ -201,22 +201,6 @@ export function createGeneratePlugin({
201201 if ( dtsMap . has ( dtsId ) ) {
202202 return { id : dtsId , meta }
203203 }
204- } else if ( extraOptions . isEntry && inputOption ) {
205- // mapping entry point to dts filename
206- const resolution = await this . resolve ( id , importer , extraOptions )
207- if ( ! resolution ) return
208-
209- const dtsId = filename_ts_to_dts ( resolution . id )
210- if ( inputAliasMap . has ( dtsId ) ) return resolution
211-
212- for ( const [ name , entry ] of Object . entries ( inputOption ) ) {
213- if ( entry === id ) {
214- inputAliasMap . set ( dtsId , `${ name } .d.ts` )
215- break
216- }
217- }
218-
219- return resolution
220204 }
221205 } ,
222206
@@ -252,12 +236,3 @@ export function createGeneratePlugin({
252236 } ,
253237 }
254238}
255-
256- function isPlainObject ( data : unknown ) : data is Record < PropertyKey , unknown > {
257- if ( typeof data !== 'object' || data === null ) {
258- return false
259- }
260-
261- const proto = Object . getPrototypeOf ( data )
262- return proto === null || proto === Object . prototype
263- }
0 commit comments