Skip to content

Commit

Permalink
fix: error if exists same lib when transfrom dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Jun 30, 2023
1 parent 8cf7d73 commit c187278
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/transform.ts
Expand Up @@ -53,14 +53,20 @@ export function transformDynamicImport(content: string) {
const matchResult = content.match(importReg)

if (matchResult?.[0]) {
matchResult[0].match(importTypesRE)![1].trim().split(',').forEach(importSet.add)
matchResult[0]
.match(importTypesRE)![1]
.trim()
.split(',')
.forEach(type => {
type && importSet.add(type.trim())
})

content = content.replace(
matchResult[0],
`import type { ${Array.from(importSet).join(', ')} } from '${libName}'`
`import { ${Array.from(importSet).join(', ')} } from '${libName}'`
)
} else {
content = `import type { ${Array.from(importSet).join(', ')} } from '${libName}';\n` + content
content = `import { ${Array.from(importSet).join(', ')} } from '${libName}';\n` + content
}
})

Expand Down
12 changes: 8 additions & 4 deletions tests/transform.spec.ts
Expand Up @@ -28,30 +28,34 @@ describe('transform tests', () => {

it('test: transformDynamicImport', () => {
expect(transformDynamicImport('data: import("vexip-ui/lib/tree").InitDataOptions[];')).toEqual(
"import type { InitDataOptions } from 'vexip-ui/lib/tree';\ndata: InitDataOptions[];"
"import { InitDataOptions } from 'vexip-ui/lib/tree';\ndata: InitDataOptions[];"
)

expect(
transformDynamicImport(
'declare const _default: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin>;\nexport default _default;\n'
)
).toEqual(
"import type { DefineComponent, ComponentOptionsMixin } from 'vue';\ndeclare const _default: DefineComponent<{}, {}, {}, {}, {}, ComponentOptionsMixin>;\nexport default _default;\n"
"import { DefineComponent, ComponentOptionsMixin } from 'vue';\ndeclare const _default: DefineComponent<{}, {}, {}, {}, {}, ComponentOptionsMixin>;\nexport default _default;\n"
)

expect(
transformDynamicImport('}> & {} & {} & import("vue").ComponentCustomProperties) | null>;')
).toEqual(
"import type { ComponentCustomProperties } from 'vue';\n}> & {} & {} & ComponentCustomProperties) | null>;"
"import { ComponentCustomProperties } from 'vue';\n}> & {} & {} & ComponentCustomProperties) | null>;"
)

expect(
transformDynamicImport(
'declare const _default: import("./Service").ServiceConstructor<import("./Service").default>;'
)
).toEqual(
"import type { ServiceConstructor, default as __DTS_1__ } from './Service';\ndeclare const _default: ServiceConstructor<__DTS_1__>;"
"import { ServiceConstructor, default as __DTS_1__ } from './Service';\ndeclare const _default: ServiceConstructor<__DTS_1__>;"
)

expect(
transformDynamicImport('import { Type } from "./test";\nconst test: import("./test").Test;')
).toEqual("import { Test, Type } from './test';\nconst test: Test;")
})

it('test: transformAliasImport', () => {
Expand Down

0 comments on commit c187278

Please sign in to comment.