@@ -157,7 +157,7 @@ export function processImport(importLine: string, typeSources: Map<string, strin
157
157
/**
158
158
* Filter out unused imports and only keep type imports
159
159
*/
160
- function processImports ( imports : string [ ] ) : string [ ] {
160
+ export function processImports ( imports : string [ ] , usedTypes : Set < string > ) : string [ ] {
161
161
const importMap = new Map < string , Set < string > > ( )
162
162
163
163
// Process each import line to extract module and types
@@ -170,16 +170,27 @@ function processImports(imports: string[]): string[] {
170
170
const types = match [ 1 ] . split ( ',' ) . map ( t => t . trim ( ) )
171
171
const module = match [ 2 ]
172
172
173
- // Add types to the set for this module
174
- if ( ! importMap . has ( module ) ) {
175
- importMap . set ( module , new Set ( ) )
173
+ // Only track types that are actually used
174
+ const usedImports = types . filter ( ( type ) => {
175
+ const baseName = type . split ( ' as ' ) [ 0 ] . trim ( )
176
+ return usedTypes . has ( baseName )
177
+ } )
178
+
179
+ if ( usedImports . length > 0 ) {
180
+ if ( ! importMap . has ( module ) ) {
181
+ importMap . set ( module , new Set ( ) )
182
+ }
183
+ usedImports . forEach ( type => importMap . get ( module ) ! . add ( type ) )
176
184
}
177
- types . forEach ( type => importMap . get ( module ) ! . add ( type ) )
178
185
}
179
186
}
180
187
181
- // Format imports
188
+ // These are the only modules we want to keep imports from
189
+ const allowedModules = [ 'bun' , '@stacksjs/dtsx' ]
190
+
191
+ // Format imports, filtering to allowed modules only
182
192
return Array . from ( importMap . entries ( ) )
193
+ . filter ( ( [ module ] ) => allowedModules . includes ( module ) )
183
194
. map ( ( [ module , types ] ) => {
184
195
const sortedTypes = Array . from ( types ) . sort ( )
185
196
return `import type { ${ sortedTypes . join ( ', ' ) } } from '${ module } ';`
@@ -785,12 +796,15 @@ export function processDeclarationLine(line: string, state: ProcessingState): vo
785
796
}
786
797
}
787
798
788
- export function formatOutput ( state : ProcessingState ) : string {
789
- const uniqueImports = processImports ( state . imports )
799
+ function formatOutput ( state : ProcessingState ) : string {
800
+ const uniqueImports = processImports ( state . imports , state . usedTypes )
790
801
const dynamicImports = Array . from ( state . usedTypes )
791
802
. map ( ( type ) => {
792
803
const source = state . typeSources . get ( type )
793
- return source ? `import type { ${ type } } from '${ source } ';` : ''
804
+ if ( source && [ 'bun' , '@stacksjs/dtsx' ] . includes ( source ) ) {
805
+ return `import type { ${ type } } from '${ source } ';`
806
+ }
807
+ return ''
794
808
} )
795
809
. filter ( Boolean )
796
810
. sort ( )
@@ -805,10 +819,13 @@ export function formatOutput(state: ProcessingState): string {
805
819
const result = [
806
820
...allImports ,
807
821
'' ,
822
+ '' , // Extra newline after imports
808
823
...declarations ,
809
824
] . filter ( Boolean ) . join ( '\n' )
810
825
811
- return state . defaultExport ? `${ result } \n\nexport default ${ state . defaultExport . trim ( ) } ;` : result
826
+ return state . defaultExport
827
+ ? `${ result } \n\nexport default ${ state . defaultExport . trim ( ) } ;\n`
828
+ : `${ result } \n`
812
829
}
813
830
814
831
/**
0 commit comments