Skip to content

[code-infra] Write declarations with tsdown in build-new - #2061

Merged
brijeshb42 merged 11 commits into
codex/tsdown-babel-configfrom
codex/tsdown-dts
Nov 20, 2026
Merged

brijeshb42 merged 11 commits into
codex/tsdown-babel-configfrom
codex/tsdown-dts

Conversation

@brijeshb42

@brijeshb42 brijeshb42 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

code-infra build-new now writes .d.ts files, like code-infra build --tsgo. --no-buildTypes skips them.

tsdown generates them with the workspace's TypeScript 7 tsc and the package's tsconfig.build.json. On top of that:

  • Re-exports are rewritten. tsdown's declaration bundler turns export * from './toolbar.js' into

    import { Props } from './toolbar.js';
    export { Props };

    On TypeScript 7 this breaks augmentation when the same interface is augmented through two packages, as in mui-x Premium:

    // premium
    declare module 'pro' { interface Props { fromPro?: boolean } }
    declare module 'grid' { interface Props { fromGrid?: boolean } }
    
    // consumer, where Slots['toolbar'] is grid's Props
    const toolbar: Slots['toolbar'] = { fromGrid: true }; // TS2353 on TS 7

    dtsExportsPlugin rewrites the output to export { Props } from './toolbar.js';, which works like tsc's export * output. TS 6 handles both forms.

  • CJS declarations keep export default (cjsDefault: false), matching the JS output.

  • Babel skips .d.ts files. tsdown passes them through user plugins, and Babel can't parse them.

  • package.json gets types conditions.

  • build and build-new share getTsconfig (typescript.mjs) to find tsconfig.build.json.

Hand-written .d.ts files need explicit exports: the bundler drops declarations that TypeScript exports implicitly. Only @mui/material's Popover.d.ts relied on that; mui/material-ui#49802 fixes it.

Known gap: types reached through export * as ns can fail with TS2694 (6 errors in mui-x charts). #2076 fixes it.

Checked against code-infra build --tsgo: package.json is identical, and the exported type names match for docs-infra, test-utils, typescript-api-extractor, benchmark, mui-utils, mui-material and x-internals.

dtsExportsPlugin.test.mjs reproduces the Premium case above with TS 7, for CJS and ESM.

Part of #413

@brijeshb42 brijeshb42 added the scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). label Nov 13, 2026
@brijeshb42
brijeshb42 added this pull request to stack #2058 November 13, 2026 18:19
@code-infra-dashboard

code-infra-dashboard Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-2061--mui-internal.netlify.app/
QR code for https://deploy-preview-2061--mui-internal.netlify.app/

Bundle size

Total Size Change: 0B(0.00%) - Total Gzip Change: 0B(0.00%)
Files: 79 total (0 added, 0 removed, 0 changed)

Show details for 79 more bundles

@mui/internal-docs-infra/abstractCreateDemo parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/abstractCreateDemoClient parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/abstractCreateStream parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/abstractCreateTypes parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/ChunkProvider parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/cli parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeControllerContext parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeExternalsContext parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeHighlighter parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeHighlighter/errors parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeHighlighter/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeProvider parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CoordinatedLazy parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CoordinatedLazy/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createDemoData parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createDemoData/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createSitemap parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createSitemap/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/resolvePageUrl parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/routeGroups parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCode parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCodeWindow parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCoordinated parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCopier parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useDemo parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useDemoController parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useErrors parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useLocalStorageState parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/usePreference parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useScrollAnchor parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useSearch parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useSearch/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useStream parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useStream/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useType parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useTypes parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useUrlHashState parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/withDocsInfra parsed: 0B(0.00%) gzip: 0B(0.00%)
addLineGutters parsed: 0B(0.00%) gzip: 0B(0.00%)
applyCodeTransformWithComments parsed: 0B(0.00%) gzip: 0B(0.00%)
BuildEngine parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeEditor parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeHighlighterChunk parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeHighlighterClient parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeInitialSourceLoader parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeSourceLoader parsed: 0B(0.00%) gzip: 0B(0.00%)
compileCssWithPostcss parsed: 0B(0.00%) gzip: 0B(0.00%)
computeHastDeltas parsed: 0B(0.00%) gzip: 0B(0.00%)
createFrame parsed: 0B(0.00%) gzip: 0B(0.00%)
createParseSourceWorkerClient parsed: 0B(0.00%) gzip: 0B(0.00%)
embedTransforms parsed: 0B(0.00%) gzip: 0B(0.00%)
enhanceCodeEmphasis parsed: 0B(0.00%) gzip: 0B(0.00%)
findExpandingRanges parsed: 0B(0.00%) gzip: 0B(0.00%)
frameVisibility parsed: 0B(0.00%) gzip: 0B(0.00%)
getHastTextContent parsed: 0B(0.00%) gzip: 0B(0.00%)
grammarCache parsed: 0B(0.00%) gzip: 0B(0.00%)
grammarLoaders parsed: 0B(0.00%) gzip: 0B(0.00%)
grammarMaps parsed: 0B(0.00%) gzip: 0B(0.00%)
grammars parsed: 0B(0.00%) gzip: 0B(0.00%)
hastUtils parsed: 0B(0.00%) gzip: 0B(0.00%)
isFrameSpan parsed: 0B(0.00%) gzip: 0B(0.00%)
loadCodeFallback parsed: 0B(0.00%) gzip: 0B(0.00%)
loadIsomorphicCodeVariant parsed: 0B(0.00%) gzip: 0B(0.00%)
parseSource parsed: 0B(0.00%) gzip: 0B(0.00%)
rolldown-runtime parsed: 0B(0.00%) gzip: 0B(0.00%)
source.css parsed: 0B(0.00%) gzip: 0B(0.00%)
source.diff parsed: 0B(0.00%) gzip: 0B(0.00%)
source.js parsed: 0B(0.00%) gzip: 0B(0.00%)
source.json parsed: 0B(0.00%) gzip: 0B(0.00%)
source.mdx parsed: 0B(0.00%) gzip: 0B(0.00%)
source.shell parsed: 0B(0.00%) gzip: 0B(0.00%)
source.ts parsed: 0B(0.00%) gzip: 0B(0.00%)
source.tsx parsed: 0B(0.00%) gzip: 0B(0.00%)
source.yaml parsed: 0B(0.00%) gzip: 0B(0.00%)
SourceEditingEngine parsed: 0B(0.00%) gzip: 0B(0.00%)
text.html.basic parsed: 0B(0.00%) gzip: 0B(0.00%)
text.md parsed: 0B(0.00%) gzip: 0B(0.00%)
TransformEngine parsed: 0B(0.00%) gzip: 0B(0.00%)
transpileSource parsed: 0B(0.00%) gzip: 0B(0.00%)

Details of bundle changes

Performance

No regressions in 8 benchmarks · ran 1m 14s · details


Check out the code infra dashboard for more information about this PR.

@brijeshb42
brijeshb42 force-pushed the codex/tsdown-dts branch 2 times, most recently from 4f6f154 to fa3fa94 Compare November 14, 2026 07:46
@brijeshb42
brijeshb42 removed this pull request from stack #2058 November 16, 2026 04:26
@brijeshb42
brijeshb42 added this pull request to stack #2074 November 16, 2026 04:27
@brijeshb42
brijeshb42 force-pushed the codex/tsdown-dts branch 2 times, most recently from 67ccccb to c539df6 Compare November 16, 2026 05:48
@brijeshb42
brijeshb42 removed this pull request from stack #2074 November 16, 2026 06:11
@brijeshb42
brijeshb42 added this pull request to stack #2075 November 16, 2026 06:11
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 16, 2026
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 16, 2026

@brijeshb42 brijeshb42 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PR review

One verified, non-blocking issue in global declaration copying.

Bugs (1)

1. 🟠 Preserve dependencies of copied global declarations

Location: packages/code-infra/src/utils/dtsExportsPlugin.mjs:82–87

The plugin copies a global declaration and marks its import external, but never follows that declaration’s references.

Failure scenario: index.d.ts imports ./globals.js; globals.d.ts references ./nested.d.ts and uses a type declared there. The build copies globals.d.ts but omits nested.d.ts. Consumers receive TS6053 (missing file) and TS2304 (missing type).

Verified with the actual build-new handler: source declarations and legacy createTypes output pass TypeScript checking; the new output fails.

Fix: Copy referenced declaration dependencies recursively, or preserve the legacy behavior of copying all source declarations. Add a regression fixture covering this chain.

Tests (0)

No findings.

Simplifications (0)

No findings.

Docs (0)

No findings.

Verdict

Approve after nits — the narrow global-declaration case needs dependency preservation.

Checks: 57 existing build/declaration tests passed; changed JavaScript syntax checks and diff whitespace checks passed. Full repository builds and downstream package validation were not run. No additional hypotheses or pre-existing issues are reported.

No files changed or published; the worktree is clean. No open questions.

@brijeshb42
brijeshb42 enabled stacked auto-merge (squash) November 18, 2026 12:18
stacked auto-merge was automatically disabled November 18, 2026 15:33

Auto-merge was disabled for another pull request in the stack

@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Nov 19, 2026
@brijeshb42
brijeshb42 requested a review from Janpot November 19, 2026 16:41
* @param {string} cwd
* @returns {Promise<string>} The `tsconfig.build.json` to emit declarations with.
*/
async function getTsconfig(cwd) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't already have this functionality anywhere in this repo?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moved it to getTsconfig in typescript.mjs and used it in both build and build-new (6a9abb6). the tsc lookup is different - findTscPair returns the .bin shim, which rolldown-plugin-dts can't spawn on Windows, so build-new resolves the native binary through getExePath

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 20, 2026
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 20, 2026
Emit declarations through tsdown's dts with the workspace TypeScript 7
`tsc` and the package's `tsconfig.build.json`, as `code-infra build`
does. `--no-buildTypes` skips them.

`cjsDefault: false` keeps CJS declarations on `export default`,
matching the named-exports JS interop.

rolldown-plugin-dts appends `export {}` to declarations, which turns
off implicit exports in hand-written `.d.ts` files. A load plugin adds
`export` to their top-level declarations; it is registered through
`inputOptions` so the CJS declaration pass runs it too.
A .d.ts file without imports or exports declares globals. Bundled by
rolldown-plugin-dts it became a module, and the side-effect import that
pulled it in was dropped, so published types referenced names that no
longer existed. code-infra build keeps both; imports of such files now
stay external and the file is copied unchanged.
The declaration plugin only parses and inserts text, so it uses
Rolldown's own parser and magic string instead of Babel, which stays
for running Babel plugins.
A global declaration that a module imports is copied as-is, but the
files it pulls in with `/// <reference path>` were left behind, so
consumers hit TS6053 and TS2304. Follow those references recursively
and copy the local declarations too, as `code-infra build` ships every
source declaration.
rolldown-plugin-dts spawns the tsc path build-new passes it. On Windows
node_modules/.bin/tsc is a shell script, so the spawn failed with
ENOENT (material-ui's Windows CI). The path now comes from the
workspace's TypeScript 7 package through its getExePath, as
rolldown-plugin-dts finds tsgo by default.
Re-export imported declarations directly so module augmentation
reaches the original interface through package subpaths.

Cover CJS and ESM declarations through tsdown and TypeScript 7.
Only `@mui/material`'s `Popover.d.ts` relied on implicit exports, and
it now exports its declarations. No package imports a global
declaration file, so building without the copy changes no output.
What remains is the re-export rewrite module augmentation needs.
build and build-new both need the package's tsconfig.build.json to
emit declarations. Use one getTsconfig in typescript.mjs for both.
@brijeshb42
brijeshb42 merged commit 1ae4080 into master Nov 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants