Skip to content

fix: tree-shake a namespace import referenced only by qualified name - #315

Merged
sxzz merged 3 commits into
sxzz:mainfrom
dex4er:fix/qualified-name-namespace-tree-shaking
Oct 11, 2026
Merged

sxzz merged 3 commits into
sxzz:mainfrom
dex4er:fix/qualified-name-namespace-tree-shaking

Conversation

@dex4er

@dex4er dex4er commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

AI usage

  • No AI was used in this PR.
  • AI was used: Opus 5.5 high
    • I have carefully reviewed the AI-generated content myself.

Description

A namespace import whose members are referenced only by qualified name keeps the whole namespace in the bundle, with every member of the imported module and the imports those members need.

// mod.ts
import * as http from 'node:http'
export function foo(): typeof http { return http }
export type A = string
export const b: number = 1

// index.ts
import * as mod from './mod'
export type A = mod.A
export const b: typeof mod.b = mod.b

With treeshake: true, the bundled index.d.ts currently contains declare namespace mod_d_exports { export { A$1 as A, b$1 as b, foo } }, the declaration of foo and import * as http from "node:http", although index.ts uses only A and b.

Cause

In collectDependencies, the TSTypeReference and TSTypeQuery cases already add the qualified name as a whole (mod.A as a member of mod) through TSEntityNameToRuntime. The walker then also leaves the nested TSQualifiedName, whose case adds the leftmost identifier mod. That dependency on the namespace object itself keeps every member alive.

Fix

Skip the TSQualifiedName case when its parent is a TSTypeReference, TSTypeQuery, TSQualifiedName or TSImportType, since each of those resolves the qualified name on its own. A qualified name in any other position still adds its leftmost identifier, as before.

Tests

  • New fixture tests/fixtures/tree-shaking-qualified-name with a test next to tree-shaking. It fails without the fix and checks that neither foo nor node:http reaches the output.
  • Two existing snapshots change, rollup-plugin-dts/basic and rollup-plugin-dts/using-namespace-import. Each loses a declare namespace …_exports that nothing in the output referenced. In basic the //#region marker of mod.d.ts also comes back, which the namespace declaration had replaced.
  • pnpm test, pnpm typecheck and pnpm lint pass.

We hit this in a project that re-exports a selection of a utility module through import * as utilities: without the fix, the members it deliberately leaves out came back into the declaration, with their node:child_process and node:http imports.

Linked Issues

Additional context

Our application is Electron-based with extensions that currently have multiple (>20) packages in their devDependencies and the unpublished release will bring just only 1 single devDependency: this is a reason to use rolldown-plugin-dts at all.

Our Electron app breaks context isolation: in renderer (browser) context still we use require "node:foo" and this is handled by Electron's IPC. However: we do not expose Node.js internals via our Extensions API especially for renderer context: the exported types should be compatible with DOM types and shoud not have leaked imports from "node:http", etc.

@dex4er
dex4er force-pushed the fix/qualified-name-namespace-tree-shaking branch from 585b189 to 3fb323a Compare October 5, 2026 01:06
A type that names a member of a namespace import, such as `ns.A` or
`typeof ns.b`, already adds `ns.A` as a dependency when its
TSTypeReference or TSTypeQuery is visited. The nested TSQualifiedName
then added the namespace identifier `ns` as well, which kept the whole
namespace object and every member of the module in the bundle, together
with the imports those members need.

Skip the TSQualifiedName case when its parent already resolves the
qualified name as a whole. Two existing snapshots lose a namespace
declaration that nothing referenced.
@dex4er
dex4er force-pushed the fix/qualified-name-namespace-tree-shaking branch from 3fb323a to 3ab18b5 Compare October 10, 2026 18:41
@dex4er

dex4er commented Oct 11, 2026

Copy link
Copy Markdown
Contributor Author

@sxzz Hello! Could you review this PR? Basically without this patch my project would be broken so I would like to prepare some strategy:

  • you'll accept it: I'm saved :)
  • you'll reject it: I need to find another way, like. ie. postprocessor for the output from this plugin or else :(
  • you'll ignore it: it complicates updates for me a little as it is not a simple bump: the pnpm patch must be recreated, but I will pantiently wait for your decission

@pkg-pr-new

pkg-pr-new Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/sxzz/rolldown-plugin-dts@315

commit: bd41b6a

@sxzz
sxzz merged commit 849c39b into sxzz:main Oct 11, 2026
11 checks passed
@sxzz

sxzz commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Thanks! I'll draft a release soon.

@dex4er
dex4er deleted the fix/qualified-name-namespace-tree-shaking branch October 11, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants