Skip to content

Commit 849c39b

Browse files
dex4ersxzz
andauthored
fix: tree-shake a namespace import referenced only by qualified name (#315)
Co-authored-by: Kevin Deng <[email protected]>
1 parent 45e6094 commit 849c39b

7 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎src/fake-js/dependency.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ export async function collectDependencies(
128128
break
129129
}
130130
case 'TSQualifiedName': {
131+
// A qualified name inside one of these parents is resolved there as
132+
// a whole (`ns.A` as a member of `ns`); adding its leftmost
133+
// identifier as well would keep the entire namespace.
134+
if (
135+
parent &&
136+
is.oneOf(parent, [
137+
'TSTypeReference',
138+
'TSTypeQuery',
139+
'TSQualifiedName',
140+
'TSImportType',
141+
])
142+
)
143+
break
131144
addDependency(getIdFromTSEntityName(node.left))
132145
break
133146
}

‎tests/__snapshots__/index.test.ts.snap‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,19 @@ export type { A };
512512
"
513513
`;
514514
515+
exports[`tree-shaking namespace import referenced by qualified name 1`] = `
516+
"// index.d.ts
517+
//#region tests/fixtures/tree-shaking-qualified-name/mod.d.ts
518+
type A$1 = string;
519+
declare const b$1: number;
520+
//#endregion
521+
//#region tests/fixtures/tree-shaking-qualified-name/index.d.ts
522+
export type A = A$1;
523+
export declare const b: typeof b$1;
524+
//#endregion
525+
export {};"
526+
`;
527+
515528
exports[`tsgo with custom path 1`] = `
516529
"// basic.d.ts
517530
//#region tests/fixtures/basic.d.ts
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as mod from './mod'
2+
3+
export type A = mod.A
4+
5+
export const b: typeof mod.b = mod.b
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as http from 'node:http'
2+
3+
export function foo(): typeof http {
4+
return http
5+
}
6+
7+
export type A = string
8+
9+
export const b: number = 1

‎tests/index.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ test('tree-shaking', async () => {
146146
expect(snapshot).matchSnapshot()
147147
})
148148

149+
test('tree-shaking namespace import referenced by qualified name', async () => {
150+
const { snapshot } = await rolldownBuild(
151+
path.resolve(dirname, 'fixtures/tree-shaking-qualified-name/index.ts'),
152+
[dts({ emitDtsOnly: true })],
153+
{
154+
treeshake: {
155+
moduleSideEffects: false,
156+
},
157+
external: /^node:.*/,
158+
},
159+
)
160+
expect(snapshot).matchSnapshot()
161+
expect(snapshot).not.toContain('node:http')
162+
expect(snapshot).not.toContain('foo')
163+
})
164+
149165
describe('dts input', () => {
150166
test('input array', async () => {
151167
const { snapshot, chunks } = await rolldownBuild(

‎tests/rollup-plugin-dts/basic/snapshot.d.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// index.d.ts
2-
declare namespace mod_d_exports {
3-
export { a };
4-
}
2+
//#region tests/rollup-plugin-dts/basic/mod.d.ts
53
declare const a: string;
64
//#endregion
75
//#region tests/rollup-plugin-dts/basic/foo.d.ts

‎tests/rollup-plugin-dts/using-namespace-import/snapshot.d.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// index.d.ts
2-
declare namespace namespace_d_exports {
3-
export { Bar };
4-
}
2+
//#region tests/rollup-plugin-dts/using-namespace-import/namespace.d.ts
53
interface Bar {}
64
//#endregion
75
//#region tests/rollup-plugin-dts/using-namespace-import/index.d.ts

0 commit comments

Comments
 (0)