Skip to content

Commit 1f0a6e9

Browse files
committed
fix: export type
1 parent 25ec133 commit 1f0a6e9

File tree

7 files changed

+52
-28
lines changed

7 files changed

+52
-28
lines changed

src/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function dts(): Plugin {
105105
for (let node of program.body as (Node & Span)[]) {
106106
const stmt = node
107107

108-
if (rewriteImport(s, node)) continue
108+
if (rewriteImportExport(s, node)) continue
109109

110110
// remove `export` modifier
111111
const isDefaultExport = node.type === 'ExportDefaultDeclaration'
@@ -185,7 +185,7 @@ export function dts(): Plugin {
185185
}
186186

187187
const str = s.toString()
188-
console.log(str)
188+
// console.log(str)
189189
return str
190190
},
191191

@@ -354,21 +354,30 @@ function patchImportSource(s: MagicStringAST, node: Node) {
354354
// fix:
355355
// - import type { ... } from '...'
356356
// - import { type ... } from '...'
357+
// - export type { ... }
358+
// - export { type ... }
357359
// - import Foo = require("./bar")
358360
// - export = Foo
359-
function rewriteImport(s: MagicStringAST, node: Node) {
360-
if (node.type === 'ImportDeclaration') {
361+
function rewriteImportExport(s: MagicStringAST, node: Node) {
362+
if (
363+
node.type === 'ImportDeclaration' ||
364+
(node.type === 'ExportNamedDeclaration' && !node.declaration)
365+
) {
361366
for (const specifier of node.specifiers) {
362367
if (
363-
specifier.type === 'ImportSpecifier' &&
364-
specifier.importKind === 'type'
368+
(specifier.type === 'ImportSpecifier' &&
369+
specifier.importKind === 'type') ||
370+
(specifier.type === 'ExportSpecifier' &&
371+
specifier.exportKind === 'type')
365372
) {
366373
s.overwriteNode(specifier, s.sliceNode(specifier).replace(RE_TYPE, ''))
367374
}
368375
}
369376

370377
const firstSpecifier = node.specifiers[0]
371-
if (node.importKind === 'type' && firstSpecifier) {
378+
const kind =
379+
node.type === 'ImportDeclaration' ? node.importKind : node.exportKind
380+
if (kind === 'type' && firstSpecifier) {
372381
s.overwrite(
373382
node.start,
374383
firstSpecifier.start,

tests/fixtures/custom-tsconfig/diff.patch renamed to tests/fixtures/custom-tsconfig/known-diff.patch

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Index: diff.patch
22
===================================================================
33
--- diff.patch
44
+++ diff.patch
5-
@@ -1,2 +1,2 @@
5+
@@ -1,2 +1,3 @@
66
// index.d.ts
77
-export { Foo } from '@/foo';
8-
+export {}
8+
+import { Foo } from '@/foo';
9+
+export { Foo };
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
// index.d.ts
2-
export {}
2+
import { Foo } from "@/foo";
3+
4+
export { Foo };

tests/fixtures/type-only-import-export/diff.patch renamed to tests/fixtures/type-only-import-export/known-diff.patch

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Index: diff.patch
22
===================================================================
33
--- diff.patch
44
+++ diff.patch
5-
@@ -1,36 +1,16 @@
5+
@@ -1,36 +1,28 @@
66
// index.d.ts
77
-export { default as A } from 'a';
88
-export { default as D } from 'd';
@@ -31,23 +31,32 @@ Index: diff.patch
3131
-export { o as O };
3232
-interface Foo$1 {}
3333
+import A from 'a';
34+
+import D from 'd';
35+
+import { B } from 'b';
3436
+import { E, E as E3 } from 'e';
3537
+import { G1 } from 'g1';
38+
+import { B as B1 } from 'b1';
3639
+import { E as E4 } from 'e3';
3740
+import * as C from 'c';
3841
+import * as F from 'f';
3942
+import { G } from 'g';
43+
+import { J } from 'j';
44+
+import { L } from 'l';
4045
+import { H as H1 } from 'h1';
46+
+import { K as K1 } from 'k1';
47+
+import { M as M1 } from 'm1';
4148
+import * as I from 'i';
4249
+export * from 'i1'
50+
+interface Foo {}
4351
declare class BarType { }
4452
declare class BarValue { }
4553
+type index_d_default = E
4654
interface O {}
47-
-declare class X {}
55+
declare class X {}
4856
-interface Foo {
49-
- inline: string
50-
-}
57+
+interface Foo$1 {
58+
inline: string
59+
}
5160
-export { BarType, BarValue, X };
5261
-export { Foo$1 as Foo, Foo as FooInlne, O as O1 };
53-
+export { A, BarType, BarValue, C, C as C1, E3, E4, F, G, G1, H1, I, O as O1, index_d_default as default };
62+
+export { A, B, B1 as B2, B1 as B3, BarType, BarValue, C, C as C1, D, E3 as E2, E3, E4, F, Foo, Foo$1 as FooInlne, G, G1, H1, I, J, K1, L, M1, O as O1, X, index_d_default as default };

tests/fixtures/type-only-import-export/meta.js

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
// index.d.ts
22
import A from "a";
3+
import D from "d";
4+
import { B } from "b";
35
import { E, E as E3 } from "e";
46
import { G1 } from "g1";
7+
import { B as B1 } from "b1";
58
import { E as E4 } from "e3";
69
import * as C from "c";
710
import * as F from "f";
811
import { G } from "g";
12+
import { J } from "j";
13+
import { L } from "l";
914
import { H as H1 } from "h1";
15+
import { K as K1 } from "k1";
16+
import { M as M1 } from "m1";
1017
import * as I from "i";
1118

1219
export * from "i1"
1320

21+
//#region tests/fixtures/type-only-import-export/foo.d.ts
22+
interface Foo {}
23+
24+
//#endregion
1425
//#region tests/fixtures/type-only-import-export/bar.d.ts
1526
declare class BarType { }
1627
declare class BarValue { }
@@ -19,6 +30,10 @@ declare class BarValue { }
1930
//#region tests/fixtures/type-only-import-export/index.d.ts
2031
type index_d_default = E
2132
interface O {}
33+
declare class X {}
34+
interface Foo$1 {
35+
inline: string
36+
}
2237

2338
//#endregion
24-
export { A, BarType, BarValue, C, C as C1, E3, E4, F, G, G1, H1, I, O as O1, index_d_default as default };
39+
export { A, B, B1 as B2, B1 as B3, BarType, BarValue, C, C as C1, D, E3 as E2, E3, E4, F, Foo, Foo$1 as FooInlne, G, G1, H1, I, J, K1, L, M1, O as O1, X, index_d_default as default };

0 commit comments

Comments
 (0)