@@ -23,12 +23,8 @@ import {
2323 TOKENIZE_RE ,
2424 TYPE_WORD_RE ,
2525} from '../re'
26- import {
27- generateRandomString ,
28- generateVarName ,
29- isNullOrUndefined ,
30- } from '../utils'
31- import { escapeNewlinesAndTabs } from './utils'
26+ import { generateVarName , isNullOrUndefined } from '../utils'
27+ import { escapeNewlinesAndTabs , generateFixedStringFromString } from './utils'
3228
3329export async function dtsToFakeJs ( dtsContent : string ) : Promise < string > {
3430 const parsed = parse ( dtsContent , {
@@ -38,6 +34,7 @@ export async function dtsToFakeJs(dtsContent: string): Promise<string> {
3834
3935 const referencedNames = new Set < string > ( )
4036 const exportedNames = new Set < string > ( )
37+ const staticImportedVars = new Set < string > ( )
4138 const result = [ ]
4239
4340 for ( const name of getAllImportNames ( parsed . program . body ) ) {
@@ -109,6 +106,7 @@ export async function dtsToFakeJs(dtsContent: string): Promise<string> {
109106 const { tokens, extras } = tokenizeText (
110107 statementTextWithCommentsAttached ,
111108 referencedNames ,
109+ staticImportedVars ,
112110 )
113111
114112 for ( const extra of extras ) {
@@ -164,6 +162,7 @@ function jsifyImportExport(text: string): string {
164162function tokenizeText (
165163 text : string ,
166164 referencedNames : Set < string > ,
165+ staticImportedVars : Set < string > ,
167166) : { tokens : string [ ] ; extras : string [ ] } {
168167 const tokens = [ ]
169168 const extras = [ ]
@@ -180,8 +179,11 @@ function tokenizeText(
180179
181180 if ( token . startsWith ( 'import(' ) ) {
182181 const staticImport = convertDynamicImportToStatic ( token )
183- extras . push ( staticImport . declarations )
184182 tokens . push ( staticImport . variableName )
183+ if ( ! staticImportedVars . has ( staticImport . variableName ) ) {
184+ extras . push ( staticImport . declarations )
185+ staticImportedVars . add ( staticImport . variableName )
186+ }
185187 } else if (
186188 isLikelyVariableOrTypeName ( token ) ||
187189 referencedNames . has ( token )
@@ -209,7 +211,7 @@ function convertDynamicImportToStatic(dynamicImport: string): {
209211 const propertyAccess = importMatch [ 3 ] || ''
210212
211213 if ( ! propertyAccess ) {
212- const importIdentifier = `import_${ generateRandomString ( ) } `
214+ const importIdentifier = `import_${ generateFixedStringFromString ( modulePath ?? 'import' ) } `
213215 return {
214216 declarations : `import * as ${ importIdentifier } from '${ modulePath } ';` ,
215217 variableName : importIdentifier ,
@@ -220,13 +222,13 @@ function convertDynamicImportToStatic(dynamicImport: string): {
220222 const remainingAccess = propertyAccess . slice ( firstProperty . accessLength )
221223
222224 if ( firstProperty . isValidIdentifier ) {
223- const uniqueName = `${ createValidIdentifier ( firstProperty . name ) } _${ generateRandomString ( ) } `
225+ const uniqueName = `${ createValidIdentifier ( firstProperty . name ) } _${ generateFixedStringFromString ( firstProperty . name ) } `
224226 let declarations = `import { ${ firstProperty . name } as ${ uniqueName } } from '${ modulePath } ';`
225227 let finalVariable = uniqueName
226228
227229 if ( remainingAccess ) {
228230 const lastProperty = extractLastProperty ( remainingAccess )
229- const varName = `${ createValidIdentifier ( lastProperty ) } _${ generateRandomString ( ) } `
231+ const varName = `${ createValidIdentifier ( lastProperty ) } _${ generateFixedStringFromString ( lastProperty ) } `
230232 declarations += `\nvar ${ varName } = ${ uniqueName } ${ remainingAccess } ;`
231233 finalVariable = varName
232234 }
@@ -236,9 +238,9 @@ function convertDynamicImportToStatic(dynamicImport: string): {
236238 variableName : finalVariable ,
237239 }
238240 } else {
239- const importIdentifier = `import_${ generateRandomString ( ) } `
241+ const importIdentifier = `import_${ generateFixedStringFromString ( modulePath ?? 'import' ) } `
240242 const lastProperty = extractLastProperty ( propertyAccess )
241- const varName = `${ createValidIdentifier ( lastProperty ) } _${ generateRandomString ( ) } `
243+ const varName = `${ createValidIdentifier ( lastProperty ) } _${ generateFixedStringFromString ( lastProperty ) } `
242244 const declarations = `import * as ${ importIdentifier } from '${ modulePath } ';\nvar ${ varName } = ${ importIdentifier } ${ propertyAccess } ;`
243245
244246 return {
0 commit comments