@@ -148,66 +148,51 @@ export function dedupeImports(imports: Import[], warn: (msg: string) => void) {
148148}
149149
150150export function toExports ( imports : Import [ ] , fileDir ?: string , includeType = false ) {
151- const map = toImportModuleMap ( imports , includeType , ( i ) => {
152- let from = stripFileExtension ( i . from )
153- if ( fileDir && isAbsolute ( from ) ) {
154- from = relative ( fileDir , from )
155- if ( from [ 0 ] !== '.' && from [ 0 ] !== '/' )
156- from = `./${ from } `
157- }
158- return i . with
159- ? `'${ from } ' with ${ stringifyWith ( i . with ) } `
160- : `'${ from } '`
161- } )
162-
151+ const map = toImportModuleMap ( imports , includeType )
163152 return Object . entries ( map )
164- . flatMap ( ( [ from , imports ] ) => {
153+ . flatMap ( ( [ name , imports ] ) => {
154+ if ( isFilePath ( name ) )
155+ name = name . replace ( / \. [ a - z ] + $ / i, '' )
156+
157+ if ( fileDir && isAbsolute ( name ) ) {
158+ name = relative ( fileDir , name )
159+ if ( ! name . match ( / ^ [ . / ] / ) )
160+ name = `./${ name } `
161+ }
165162 const entries : string [ ] = [ ]
166163 const filtered = Array . from ( imports ) . filter ( ( i ) => {
167- if ( i . name === '*' || i . name === '=' ) {
168- entries . push ( `export * as ${ i . as } from ${ from } ;` )
164+ if ( i . name === '*' ) {
165+ entries . push ( `export * as ${ i . as } from ' ${ name } ' ;` )
169166 return false
170167 }
171168 return true
172169 } )
173- if ( filtered . length ) {
174- const items = filtered . map ( i => stringifyImportAlias ( i , false ) ) . join ( ', ' )
175- entries . push ( `export { ${ items } } from ${ from } ;` )
176- }
170+ if ( filtered . length )
171+ entries . push ( `export { ${ filtered . map ( i => stringifyImportAlias ( i , false ) ) . join ( ', ' ) } } from '${ name } ';` )
172+
177173 return entries
178174 } )
179175 . join ( '\n' )
180176}
181177
182178export function stripFileExtension ( path : string ) {
183- return isFilePath ( path )
184- ? path . replace ( / \. [ a - z ] + $ / i, '' )
185- : path
179+ return path . replace ( / \. [ a - z ] + $ / i, '' )
186180}
187181
188182export function toTypeDeclarationItems ( imports : Import [ ] , options ?: TypeDeclarationOptions ) {
189- const map = toImportModuleMap ( imports , true , ( i ) => {
190- const from = options ?. resolvePath ?.( i ) || stripFileExtension ( i . typeFrom || i . from )
191- return i . with
192- ? `import('${ from } ', { with: ${ stringifyWith ( i . with ) } })`
193- : `import('${ from } ')`
194- } )
195-
196- return Object . entries ( map )
197- . flatMap ( ( [ from , imports ] ) => {
198- const entries : string [ ] = [ ]
199- const filtered = Array . from ( imports ) . filter ( ( i ) => {
200- if ( i . name === '*' || i . name === '=' ) {
201- entries . push ( `const ${ i . as } : typeof ${ from } ` )
202- return false
203- }
204- return true
205- } )
206- if ( filtered . length ) {
207- const items = filtered . map ( i => stringifyImportAlias ( i , true ) ) . sort ( ) . join ( ', ' )
208- entries . push ( `const { ${ items } }: typeof ${ from } ` )
209- }
210- return entries
183+ return imports
184+ . map ( ( i ) => {
185+ const from = options ?. resolvePath ?.( i ) || stripFileExtension ( i . typeFrom || i . from )
186+ let typeDef = ''
187+ if ( i . with )
188+ typeDef += `import('${ from } ', { with: ${ stringifyWith ( i . with ) } })`
189+ else
190+ typeDef += `import('${ from } ')`
191+
192+ if ( i . name !== '*' && i . name !== '=' )
193+ typeDef += `['${ i . name } ']`
194+
195+ return `const ${ i . as } : typeof ${ typeDef } `
211196 } )
212197 . sort ( )
213198}
@@ -297,15 +282,16 @@ function stringifyImportAlias(item: Import, isCJS = false) {
297282 : `${ item . name } as ${ item . as } `
298283}
299284
300- function toImportModuleMap ( imports : Import [ ] , includeType = false , getKey ?: ( i : Import ) => string ) {
285+ function toImportModuleMap ( imports : Import [ ] , includeType = false ) {
301286 const map : Record < string , Set < Import > > = { }
302287 for ( const _import of imports ) {
303288 if ( _import . type && ! includeType )
304289 continue
305290
306- const key = getKey ?.( _import ) || _import . from
307- map [ key ] ??= new Set ( )
308- map [ key ] . add ( _import )
291+ if ( ! map [ _import . from ] )
292+ map [ _import . from ] = new Set ( )
293+
294+ map [ _import . from ] . add ( _import )
309295 }
310296 return map
311297}
0 commit comments