Skip to content

Commit 3ca6044

Browse files
fix: aliases in namespace imports (#418) (#443)
* fix: aliases in namespace imports (#418) Issue #418 appeared to boil down to missing support for _namespace imports_ (i.e., those of the form `import * from Xyz in 'xyz'`). This commit adds that support, along with a unit test and a change in the `react-vite` example that relies on the new functionality. * fix: linting issues Fixing linting issues occurring during CI build.
1 parent 46cfca4 commit 3ca6044

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './HelloWorld';

examples/react-vite/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import * as Components from '@/components'
2+
13
import { HelloWorld } from './components/HelloWorld'
24

35
import type { ReactDOM as MyReactDOM } from 'react'
46

57
export { HelloWorld }
8+
export { Components }
69
export { App } from './App'
710
export { useCount } from '@/hooks/useCount'
811
export * from './modules'

packages/unplugin-dts/src/core/transform.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ export function transformCode(options: {
163163
}
164164

165165
s.remove(node.pos, node.end)
166+
++importCount
167+
} else if (
168+
ts.isStringLiteral(node.moduleSpecifier) &&
169+
node.importClause.namedBindings && ts.isNamespaceImport(node.importClause.namedBindings)
170+
) {
171+
const libName = toLibName(node.moduleSpecifier.text)
172+
173+
if (libName !== node.moduleSpecifier.text) {
174+
s.update(node.moduleSpecifier.pos, node.moduleSpecifier.end, ` '${libName}'`)
175+
}
176+
166177
++importCount
167178
}
168179

packages/unplugin-dts/tests/transform.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ describe('transform tests', () => {
216216
content: 'import { nothingReal } from "someDependency";',
217217
output: "import { nothingReal } from 'someDependency';\n",
218218
},
219+
{
220+
description: 'alias in namespace import',
221+
content: 'import * as Components from "@/test/components";\n',
222+
output: "import * as Components from '../test/components';\n",
223+
},
219224
]
220225

221226
tests.forEach(({ description, content, filePath, aliases, output }) => {

0 commit comments

Comments
 (0)