Skip to content

Commit 8fa67c1

Browse files
committed
fix: type-only imports
closes #67
1 parent e24bdc1 commit 8fa67c1

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

src/core/ast.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ export type OxcImport = (
1414

1515
export function filterImports(program: OxcTypes.Program): OxcImport[] {
1616
return program.body.filter(
17-
(node): node is OxcImport =>
18-
(node.type === 'ImportDeclaration' ||
19-
node.type === 'ExportAllDeclaration' ||
20-
node.type === 'ExportNamedDeclaration') &&
21-
!!node.source,
17+
(node): node is OxcImport => !!('source' in node && node.source),
2218
)
2319
}
2420

src/index.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,8 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
173173

174174
addOutput(id, { s, imports, map })
175175

176-
const typeImports = program.body.filter((node): node is OxcImport => {
177-
if (!('source' in node) || !node.source) return false
178-
if ('importKind' in node && node.importKind === 'type') return true
179-
if ('exportKind' in node && node.exportKind === 'type') return true
180-
181-
if (node.type === 'ImportDeclaration') {
182-
return (
183-
!!node.specifiers &&
184-
node.specifiers.every(
185-
(spec) =>
186-
spec.type === 'ImportSpecifier' && spec.importKind === 'type',
187-
)
188-
)
189-
}
190-
return (
191-
node.type === 'ExportNamedDeclaration' &&
192-
node.specifiers &&
193-
node.specifiers.every((spec) => spec.exportKind === 'type')
194-
)
195-
})
196-
for (const { source } of typeImports) {
176+
const importsInDts = filterImports(program)
177+
for (const { source } of importsInDts) {
197178
const resolved = (await resolve(context, source.value, id))?.id
198179
if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
199180
let source: string
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## dep.d.ts
2+
3+
```ts
4+
export type Dep = "dep";
5+
6+
```
7+
## main.d.ts
8+
9+
```ts
10+
export { Dep } from "./dep.js";
11+
export declare const main = "main";
12+
13+
```
14+
## main.js
15+
16+
```js
17+
const main = "main";
18+
19+
export { main };
20+
21+
```

tests/fixtures/type-only/dep.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Dep = 'dep'

tests/fixtures/type-only/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type { Dep } from './dep'
2+
3+
export const main = 'main'

tests/rollup.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,20 @@ describe.concurrent('rollup', () => {
189189

190190
await expectSnapshot(dist, `rollup/${dir}`, expect)
191191
})
192+
193+
test('type only imports', async ({ expect }) => {
194+
const dir = 'type-only'
195+
const input = path.resolve(fixtures, dir, 'main.ts')
196+
const dist = path.resolve(TEST_SANDBOX_FOLDER, dir)
197+
198+
const bundle = await rollup({
199+
input,
200+
plugins: [UnpluginIsolatedDecl(), Oxc()],
201+
logLevel: 'silent',
202+
})
203+
204+
await bundle.write({ dir: dist })
205+
206+
await expectSnapshot(dist, `rollup/${dir}`, expect)
207+
})
192208
})

0 commit comments

Comments
 (0)