Skip to content

Commit d351b43

Browse files
authored
feat(dts): upgrade rolldown-plugin-dts to v0.30 (#1123)
1 parent 6ad0571 commit d351b43

11 files changed

Lines changed: 274 additions & 49 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Public plugin exports in `src/plugins.ts`: `DepsPlugin`, `NodeProtocolPlugin`, `
181181

182182
### Key Architectural Patterns
183183

184-
1. **Multi-format builds:** Single config produces ESM + CJS + types. ES format handles types via dts plugin; CJS format has separate dts pass with `emitDtsOnly: true`
184+
1. **Multi-format builds:** Single config produces ESM + CJS + types. ES format handles types via dts plugin; CJS format has separate dts pass with `dtsOnly: true`
185185

186186
2. **Package-aware building:** Detects package.json, auto-generates exports field, validates bundled dependencies, runs package linters
187187

@@ -280,7 +280,7 @@ Entry points support:
280280
When `format: ['esm', 'cjs']`:
281281

282282
1. First pass builds both formats with shared types from ESM build
283-
2. If CJS needs separate types, second pass runs with `emitDtsOnly: true`
283+
2. If CJS needs separate types, second pass runs with `dtsOnly: true`
284284

285285
### Package.json Integration
286286

‎docs/options/dts.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,20 @@ If `isolatedDeclarations` is not enabled, `tsdown` will fall back to using the T
9898
## Advanced Options
9999

100100
`rolldown-plugin-dts` provides several advanced options to customize `.d.ts` generation. For a detailed explanation of these options, refer to the [plugin's documentation](https://github.com/sxzz/rolldown-plugin-dts#options).
101+
102+
### Filtering Entries
103+
104+
By default, every entry gets a declaration file. Use `dts.entry` to emit declarations only for some entries:
105+
106+
```ts [tsdown.config.ts]
107+
import { defineConfig } from 'tsdown'
108+
109+
export default defineConfig({
110+
entry: ['src/index.ts', 'src/cli.ts'],
111+
dts: {
112+
entry: ['src/**/*.ts', '!src/cli.ts'],
113+
},
114+
})
115+
```
116+
117+
`dts.entry` only filters entries: a file that is not an entry never gets its own declaration file, even if it matches.

‎docs/zh-CN/options/dts.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,20 @@ export default defineConfig({
9898
## 高级选项
9999

100100
`rolldown-plugin-dts` 提供了多个高级选项用于自定义 `.d.ts` 文件的生成。详细说明请参阅 [插件文档](https://github.com/sxzz/rolldown-plugin-dts#options)。
101+
102+
### 筛选入口
103+
104+
默认情况下,每个入口都会生成声明文件。使用 `dts.entry` 可以只为部分入口生成声明文件:
105+
106+
```ts [tsdown.config.ts]
107+
import { defineConfig } from 'tsdown'
108+
109+
export default defineConfig({
110+
entry: ['src/index.ts', 'src/cli.ts'],
111+
dts: {
112+
entry: ['src/**/*.ts', '!src/cli.ts'],
113+
},
114+
})
115+
```
116+
117+
`dts.entry` 只筛选入口:不是入口的文件即使匹配,也不会单独生成声明文件。

‎pnpm-lock.yaml‎

Lines changed: 35 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ catalogs:
9999
picomatch: ^4.0.7
100100
postcss-load-config: ^6.0.1
101101
rolldown: ~1.2.16
102-
rolldown-plugin-dts: ^0.29.0
102+
rolldown-plugin-dts: ^0.30.0
103103
tinyexec: ^1.3.1
104104
tinyglobby: ^0.2.17
105105
tinyps: ^0.5.1

‎schema.json‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4954,20 +4954,15 @@
49544954
{ "type": "string" },
49554955
{ "type": "array", "items": { "type": "string" } }
49564956
],
4957-
"description": "Glob pattern(s) that select source files for declaration generation.\n\nPatterns are relative to {@link cwd} and may be negated with `!`. Matching\nfiles are emitted even when they are not Rolldown entry points."
4957+
"description": "Glob pattern(s) that filter which Rolldown entries emit declarations.\n\nPatterns are relative to {@link cwd} and may be negated with `!`. Only\nRolldown entries are matched; other files never emit declarations.\n\nCannot be combined with {@link dtsOnly}; set Rolldown's `input` instead."
49584958
},
49594959
"cwd": {
49604960
"type": "string",
49614961
"description": "Base directory for config discovery, glob matching, and relative paths."
49624962
},
4963-
"dtsInput": {
4963+
"dtsOnly": {
49644964
"anyOf": [{ "const": false }, { "const": true }],
4965-
"description": "Treats entry files as existing declarations instead of source files.",
4966-
"default": false
4967-
},
4968-
"emitDtsOnly": {
4969-
"anyOf": [{ "const": false }, { "const": true }],
4970-
"description": "Removes non-declaration chunks from the output.",
4965+
"description": "Builds declarations only. Each entry resolves to its declaration, so the\nbuild emits no JavaScript. Entries may be source files or existing\ndeclaration files.",
49714966
"default": false
49724967
},
49734968
"tsconfig": {
@@ -5034,6 +5029,16 @@
50345029
"items": { "$ref": "#/$defs/CustomLanguage" },
50355030
"description": "Registers non-standard source languages such as Vue or Astro.\n\nIf a language is supported via {@link https://volarjs.dev Volar},\n{@link CustomLanguage.volarTypeScript} and\n{@link CustomLanguage.createVolarPlugins} are both required. Volar\nlanguages require `tsc`. Non-Volar languages can use `tsgo` together\nwith {@link TsgoOptions.vfs`tsgo.vfs`}, or `tsrs` with\n{@link TsrsOptions.vfs`tsrs.vfs`}."
50365031
},
5032+
"dtsInput": {
5033+
"anyOf": [{ "const": false }, { "const": true }],
5034+
"description": "Deprecated: Use `dtsOnly` instead.",
5035+
"deprecated": true
5036+
},
5037+
"emitDtsOnly": {
5038+
"anyOf": [{ "const": false }, { "const": true }],
5039+
"description": "Deprecated: Use `dtsOnly` instead.",
5040+
"deprecated": true
5041+
},
50375042
"build": {
50385043
"anyOf": [{ "const": false }, { "const": true }],
50395044
"description": "Deprecated: Use `tsc.build` instead.",

‎skills/tsdown/references/option-dts.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export default defineConfig({
227227

228228
| Option | Type | Description |
229229
|--------|------|-------------|
230+
| `entry` | `string \| string[]` | Globs filtering which entries emit declarations; non-entry files never do |
230231
| `sourcemap` | `boolean` | Generate declaration source maps |
231232
| `compilerOptions` | `object` | Override TypeScript compiler options |
232233
| `vue` | `boolean` | Enable Vue type generation (requires vue-tsc) |

‎src/features/rolldown.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ async function resolveInputOptions(
132132
if (dts) {
133133
const { dts: dtsPlugin } = await import('rolldown-plugin-dts')
134134
const options: DtsOptions = {
135+
cwd,
135136
tsconfig,
136137
logger,
137138
...dts,
@@ -140,10 +141,13 @@ async function resolveInputOptions(
140141
if (format === 'es') {
141142
plugins.push(dtsPlugin(options))
142143
} else if (cjsDts) {
144+
// `dtsOnly` cannot be combined with `entry`, so the entry filter is
145+
// applied to `input` below instead.
146+
const { entry: _, ...rest } = options
143147
plugins.push(
144148
dtsPlugin({
145-
...options,
146-
emitDtsOnly: true,
149+
...rest,
150+
dtsOnly: true,
147151
cjsDefault,
148152
}),
149153
)
@@ -248,7 +252,7 @@ async function resolveInputOptions(
248252

249253
const inputOptions = await mergeUserOptions(
250254
{
251-
input: entry,
255+
input: cjsDts && dts ? filterDtsEntry(entry, cwd, dts) : entry,
252256
cwd,
253257
external,
254258
resolve: {
@@ -291,6 +295,35 @@ async function resolveInputOptions(
291295
return inputOptions
292296
}
293297

298+
/**
299+
* Applies `dts.entry` to the entries of a declaration-only build, matching
300+
* the way rolldown-plugin-dts filters entries in a regular build.
301+
*/
302+
function filterDtsEntry(
303+
entry: Record<string, string>,
304+
cwd: string,
305+
{ entry: patterns, cwd: dtsCwd = cwd }: DtsOptions,
306+
): Record<string, string> {
307+
if (patterns === undefined) return entry
308+
309+
const includes: string[] = []
310+
const ignores: string[] = []
311+
for (const pattern of toArray(patterns)) {
312+
if (pattern[0] === '!') ignores.push(pattern.slice(1))
313+
else includes.push(pattern)
314+
}
315+
316+
return Object.fromEntries(
317+
Object.entries(entry).filter(([, file]) => {
318+
const id = path.relative(dtsCwd, path.resolve(cwd, file))
319+
return (
320+
includes.some((p) => path.matchesGlob(id, p)) &&
321+
ignores.every((p) => !path.matchesGlob(id, p))
322+
)
323+
}),
324+
)
325+
}
326+
294327
async function resolveOutputOptions(
295328
inputOptions: InputOptions,
296329
config: ResolvedConfig,

0 commit comments

Comments
 (0)