Skip to content

Commit 9ea4b95

Browse files
committed
feat!: reuse generated dts by default
1 parent 084edef commit 9ea4b95

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ This is especially useful when you have a single `tsconfig.json` for multiple pr
148148

149149
---
150150

151+
### newContext
152+
153+
If `true`, the plugin will create a new isolated context for each build,
154+
ensuring that previously generated `.d.ts` code and caches are not reused.
155+
156+
By default, the plugin may reuse internal caches or incremental build artifacts
157+
to speed up repeated builds. Enabling this option forces a clean context,
158+
guaranteeing that all type definitions are generated from scratch.
159+
160+
---
161+
151162
### tsgo
152163

153164
**[Experimental]** Enables DTS generation using [`tsgo`](https://github.com/microsoft/typescript-go).

src/generate.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function createGeneratePlugin({
5252
parallel,
5353
eager,
5454
tsgo,
55+
newContext,
5556
}: Pick<
5657
OptionsResolved,
5758
| 'cwd'
@@ -64,6 +65,7 @@ export function createGeneratePlugin({
6465
| 'parallel'
6566
| 'eager'
6667
| 'tsgo'
68+
| 'newContext'
6769
>): Plugin {
6870
const dtsMap: DtsMap = new Map<string, TsModule>()
6971

@@ -105,7 +107,9 @@ export function createGeneratePlugin({
105107
tsgoDist = await runTsgo(cwd, tsconfig)
106108
} else if (!parallel && (!isolatedDeclarations || vue)) {
107109
tscModule = await import('./tsc/index.ts')
108-
tscContext = eager ? undefined : tscModule.createContext()
110+
if (newContext) {
111+
tscContext = tscModule.createContext()
112+
}
109113
}
110114

111115
if (!Array.isArray(options.input)) {
@@ -279,7 +283,10 @@ export function createGeneratePlugin({
279283
if (!debug.enabled && tsgoDist) {
280284
await rm(tsgoDist, { recursive: true, force: true }).catch(() => {})
281285
}
282-
tscContext = tsgoDist = undefined
286+
tsgoDist = undefined
287+
if (newContext) {
288+
tscContext = undefined
289+
}
283290
},
284291
}
285292
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createFakeJsPlugin } from './fake-js.ts'
55
import { createGeneratePlugin } from './generate.ts'
66
import { resolveOptions, type Options } from './options.ts'
77
import { createDtsResolvePlugin } from './resolver.ts'
8+
import { createContext, type TscContext } from './tsc/index.ts'
89
import type { Plugin } from 'rolldown'
910

1011
export {
@@ -35,8 +36,10 @@ export function dts(options: Options = {}): Plugin[] {
3536
}
3637

3738
export {
39+
createContext,
3840
createFakeJsPlugin,
3941
createGeneratePlugin,
4042
resolveOptions,
4143
type Options,
44+
type TscContext,
4245
}

src/options.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ export interface Options {
130130
* `tsconfigRaw` and `isolatedDeclarations` options will be ignored when this option is enabled.
131131
*/
132132
tsgo?: boolean
133+
134+
/**
135+
* If `true`, the plugin will create a new isolated context for each build,
136+
* ensuring that previously generated `.d.ts` code and caches are not reused.
137+
*
138+
* By default, the plugin may reuse internal caches or incremental build artifacts
139+
* to speed up repeated builds. Enabling this option forces a clean context,
140+
* guaranteeing that all type definitions are generated from scratch.
141+
*
142+
* **Default:** `false`
143+
*/
144+
newContext?: boolean
133145
}
134146

135147
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
@@ -160,6 +172,7 @@ export function resolveOptions({
160172
parallel = false,
161173
eager = false,
162174
tsgo = false,
175+
newContext = false,
163176
}: Options): OptionsResolved {
164177
let resolvedTsconfig: TsConfigJsonResolved | undefined
165178
if (tsconfig === true || tsconfig == null) {
@@ -222,5 +235,6 @@ export function resolveOptions({
222235
parallel,
223236
eager,
224237
tsgo,
238+
newContext,
225239
}
226240
}

0 commit comments

Comments
 (0)