Repository navigation
fix: tree-shake a namespace import referenced only by qualified name - #315
Merged
sxzz merged 3 commits intoOct 11, 2026
Merged
Conversation
dex4er
force-pushed
the
fix/qualified-name-namespace-tree-shaking
branch
from
October 5, 2026 01:06
585b189 to
3fb323a
Compare
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
force-pushed
the
fix/qualified-name-namespace-tree-shaking
branch
from
October 10, 2026 18:41
3fb323a to
3ab18b5
Compare
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:
|
commit: |
sxzz
approved these changes
Oct 11, 2026
Owner
|
Thanks! I'll draft a release soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AI usage
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.
With
treeshake: true, the bundledindex.d.tscurrently containsdeclare namespace mod_d_exports { export { A$1 as A, b$1 as b, foo } }, the declaration offooandimport * as http from "node:http", althoughindex.tsuses onlyAandb.Cause
In
collectDependencies, theTSTypeReferenceandTSTypeQuerycases already add the qualified name as a whole (mod.Aas a member ofmod) throughTSEntityNameToRuntime. The walker then also leaves the nestedTSQualifiedName, whose case adds the leftmost identifiermod. That dependency on the namespace object itself keeps every member alive.Fix
Skip the
TSQualifiedNamecase when its parent is aTSTypeReference,TSTypeQuery,TSQualifiedNameorTSImportType, 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
tests/fixtures/tree-shaking-qualified-namewith a test next totree-shaking. It fails without the fix and checks that neitherfoonornode:httpreaches the output.rollup-plugin-dts/basicandrollup-plugin-dts/using-namespace-import. Each loses adeclare namespace …_exportsthat nothing in the output referenced. Inbasicthe//#regionmarker ofmod.d.tsalso comes back, which the namespace declaration had replaced.pnpm test,pnpm typecheckandpnpm lintpass.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 theirnode:child_processandnode:httpimports.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.