Skip to content

fix: add export {} when inline exports leave no export statement - #310

Closed
harshit-d3v wants to merge 1 commit into
sxzz:mainfrom
harshit-d3v:fix/inline-exports-scope-marker
Closed

harshit-d3v wants to merge 1 commit into
sxzz:mainfrom
harshit-d3v:fix/inline-exports-scope-marker

Conversation

@harshit-d3v

Copy link
Copy Markdown
Contributor

Fixes rolldown/tsdown#1087

since 0.28.2 declarations are exported inline, so a chunk can end up with no export { ... } statement. a .d.ts without export { ... }, export * or export = exports every top-level declaration, so types that were never exported become public:

type Internal = { a: string }
export declare class Foo { readonly x: Internal; constructor(x: Internal) }

import type { Internal } from 'pkg' compiles against that. this adds export {}; when a chunk has a declaration that isn't exported and nothing that limits the exports, same as tsc. imports and ambient modules don't count.

  • bisected: 0.28.1 fine, 0.28.2 broken (b9bdaf7)
  • 50 snapshots only gain export {};, each one had a private declaration leaking (e.g. declare class B in remove-private)
  • TS checker: exports go from [Internal, Foo] to [Foo]
  • ran the repro from the issue against the patched build, .d.mts and .d.cts both end with export {}; now

A .d.ts file without an `export { ... }`, `export *` or `export =`
statement exports every top-level declaration. Since declarations are
exported inline, a chunk can end up without one, and the declarations it
never exported become public. Add `export {}` in that case, as tsc does.

Fixes rolldown/tsdown#1087

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Top-level TSDeclareFunction declarations may still leak as public exports.

Review effort: Lite
Findings: None

What changed in this PR

This PR prevents private declarations from leaking from bundled .d.ts files by adding export {} when inline exports leave no export scope.

Changes:

  • Adds export-scope detection and marker generation.
  • Adds regression coverage for private declarations.
  • Updates affected declaration snapshots.

Review note: top-level TSDeclareFunction declarations still require handling in the export-scope guard.

File Summary
tests/​rollup-plugin-dts/​variadic-tuple-types/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​using-namespace-import/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​using-namespace-import-multiple/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-typeof/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-this/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-template-literals/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-nodes/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-mapped/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-literal/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-index/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-function/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-constructor/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​type-conditional/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts48/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts47/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts43-staticindex/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts43-override/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts43-getset/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts42-tuple-rest/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​ts42-abstract/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​shadowing/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​remove-private/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​remapped-mapped-types/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​overrides/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​namespace-references/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​keep-referenced-interface/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​keep-extended-interface/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​issue-254/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​issue-236/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​issue-220/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​issue-160-global-namespace/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​issue-106-inline-import-generic/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​inline-import/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​inline-import-namespace/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​inline-import-generic/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​import-renamed/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​import-referenced-interface/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​import-default-interface/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​implements-expression/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​has-class/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​getters-setters/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​generics/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​export-simple-alias/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​export-default-abstract/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​constructor-shorthands/​snapshot.d.ts Updated declaration snapshot.
tests/​rollup-plugin-dts/​computed-property/​snapshot.d.ts Updated declaration snapshot.
tests/​index.test.ts Adds regression test coverage.
tests/​fixtures/​export-decl-private.ts Adds the private-declaration fixture.
tests/​__snapshots__/​index.test.ts.snap Updates integration snapshots.
src/​fake-js/​index.ts Adds conditional export {} insertion; TSDeclareFunction handling remains incomplete.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@harshit-d3v

Copy link
Copy Markdown
Contributor Author

checked the TSDeclareFunction note: is.Declaration from yuku-ast includes it, and a private declare function internal(): string next to an exported function gets export {}; too

gitKrystan added a commit to warp-drive-data/warp-drive that referenced this pull request Sep 29, 2026
…mments

Applies two fixes to the built dist/index.mjs of 0.28.6:

- sxzz/rolldown-plugin-dts#310 (open, for rolldown/tsdown#1087): add
  `export {}` when a chunk has a declaration it doesn't export and no
  `export { ... }`, `export *` or `export =`. Without it TypeScript treats
  every top-level declaration as exported, which is how @warp-drive/ember's
  unexported `Tag` type got an API page.
- Keep an entry's file-level doc comment when its first statement is an
  import or `export ... from`. The comment was attached to that statement,
  which the plugin keeps without comments, so the `@module` comment of
  @warp-drive/ember's entries was dropped. It's restored only for the
  chunk's facade module, not for the modules the chunk inlines. There is
  no upstream PR for this one yet.

Drop the patch once a release includes both.
gitKrystan added a commit to warp-drive-data/warp-drive that referenced this pull request Sep 29, 2026
… replace

The ember-rolldown and rolldown-plugin-dts patches now do what these did:

- the templateTagDeclarationMaps plugin, and its @jridgewell/remapping
  dependency and catalog entry: ember-rolldown traces .gts declaration
  maps through content-tag's map itself (NullVoxPopuli/ember.nvp#140).
- the ember `footer` that appended `export {}`, and the `footer`
  pass-through: rolldown-plugin-dts adds it where it's needed
  (sxzz/rolldown-plugin-dts#310).
- the ember `banner` that copied each entry's `@module` comment back, and
  the `banner` pass-through, which had no other user: rolldown-plugin-dts
  keeps that comment itself.

The ember dist .d.ts and .d.ts.map files are unchanged by this, apart from
index.d.ts and experiments.d.ts losing the `export {}` they never needed:
both already end with an `export { ... }` list.
runspired pushed a commit to warp-drive-data/warp-drive that referenced this pull request Sep 30, 2026
…ed types (#11353)

* docs(ember): keep each entry's @module comment in the built declarations

The d.ts bundler keeps only comments attached to declarations, so it dropped the
file-level @module comment from install.ts and experiments.ts, and every tag but
@mergeModuleWith from index.ts. TypeDoc reads these dist files, so the install and
experiments API pages rendered with no summary or text. Copy each entry's own
@module comment, found with the TypeScript parser, onto its declaration chunk.

* docs(ember): link ember API pages to their .gts sources

@warp-drive/ember's TypeDoc entry points are its built dist/*.d.ts, so every
"Defined in" link pointed at dist, which is not committed. The declaration
maps could not fix that on their own: for a .gts module they named a
src/-private/*.ts file that does not exist, at the line content-tag's output
put it on.

The shared tsdown config now chains each .d.ts.map through the sourcemap of a
template-tag module as loaded, so the maps name the .gts file and line. The
source-links TypeDoc plugin now follows the declaration map of any .d.ts, not
only an installed copy's, and links the source it names.

* docs(ember): stop the install declarations from exporting Tag

TypeScript treats every top-level declaration in a .d.ts module as exported
unless the file has an export declaration, and an export modifier does not
count. The d.ts bundler wrote install.d.ts with modifiers only, so the
unexported type Tag became an export and got its own API page. End each ember
declaration chunk with an empty export {}, which keeps exactly the exports the
source has.

* docs(ember): say the experiments entry point holds only pagination today

Point readers at @warp-drive/experiments for the other experiments, such
as storage resources.

* docs(ember): explain what importing @warp-drive/ember/install does

The install module's comment had only an @summary, which never renders,
so its API page showed no text. It now says the import calls
setupSignals(buildSignalConfig), where to add it and that addons add it
only in tests, linking the Ember installation guide.

* chore(deps): patch @nullvoxpopuli/ember-rolldown with its unreleased .gts map fixes

Applies two upstream changes to the built dist/index.js of 2.9.0:

- NullVoxPopuli/ember.nvp#140 (merged, not released): traces each
  .d.ts.map through content-tag's map in generateBundle, so the maps name
  the .gts file at its own lines.
- NullVoxPopuli/ember.nvp#143 (open): the .gts specifier rewrite maps
  exact columns through magic-string, and a .gts with no <template> loads
  with an identity map that names the .gts file.

Both import packages 2.9.0 doesn't depend on, so a packageExtensions entry
adds @jridgewell/remapping and magic-string, at the ranges upstream uses.
Drop the patch and the extension once a release includes both PRs.

* chore(deps): patch rolldown-plugin-dts for export {} and entry doc comments

Applies two fixes to the built dist/index.mjs of 0.28.6:

- sxzz/rolldown-plugin-dts#310 (open, for rolldown/tsdown#1087): add
  `export {}` when a chunk has a declaration it doesn't export and no
  `export { ... }`, `export *` or `export =`. Without it TypeScript treats
  every top-level declaration as exported, which is how @warp-drive/ember's
  unexported `Tag` type got an API page.
- Keep an entry's file-level doc comment when its first statement is an
  import or `export ... from`. The comment was attached to that statement,
  which the plugin keeps without comments, so the `@module` comment of
  @warp-drive/ember's entries was dropped. It's restored only for the
  chunk's facade module, not for the modules the chunk inlines. There is
  no upstream PR for this one yet.

Drop the patch once a release includes both.

* chore(build): drop the ember .d.ts workarounds the dependency patches replace

The ember-rolldown and rolldown-plugin-dts patches now do what these did:

- the templateTagDeclarationMaps plugin, and its @jridgewell/remapping
  dependency and catalog entry: ember-rolldown traces .gts declaration
  maps through content-tag's map itself (NullVoxPopuli/ember.nvp#140).
- the ember `footer` that appended `export {}`, and the `footer`
  pass-through: rolldown-plugin-dts adds it where it's needed
  (sxzz/rolldown-plugin-dts#310).
- the ember `banner` that copied each entry's `@module` comment back, and
  the `banner` pass-through, which had no other user: rolldown-plugin-dts
  keeps that comment itself.

The ember dist .d.ts and .d.ts.map files are unchanged by this, apart from
index.d.ts and experiments.d.ts losing the `export {}` they never needed:
both already end with an `export { ... }` list.

* chore: link the upstream PR for the rolldown-plugin-dts @module fix

* chore(deps): bump @nullvoxpopuli/ember-rolldown to 2.10.0 and drop its patch

2.10.0 ships NullVoxPopuli/ember.nvp#140 and #143, so the patch and the
packageExtensions entry that added their dependencies are no longer needed.
2.10.0 is younger than minimumReleaseAge, so it is excluded by exact version.
@coldsmirk

Copy link
Copy Markdown

This also breaks consumers of @tanstack/table-core 9.2.5+: 24 private declarations across 9 .d.ts files become implicit exports, and declaration emit in user code fails with TS2883 (TanStack/table#6619). Adding export {}; to the affected file fixes it, which is what this PR does.

@harshit-d3v

Copy link
Copy Markdown
Contributor Author

thanks for confirming. @sxzz this is showing up downstream now in TanStack/table#6619, could you take a look when you get a chance?

sxzz added a commit that referenced this pull request Oct 10, 2026
@sxzz

sxzz commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thanks for the fix! Incorporated it in 004032f, with the scope checks combined into a single pass and a snapshot regression test. Your contribution is credited in the commit’s co-author trailer.

@sxzz sxzz closed this Oct 10, 2026
@harshit-d3v

Copy link
Copy Markdown
Contributor Author

thanks for landing it!

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.

0.23.0: bundled .d.ts no longer ends with export { ... }, so non-exported types become public

4 participants