Skip to content

Commit 1314644

Browse files
committed
feat!: add build option for tsc -b
1 parent 022223a commit 1314644

File tree

4 files changed

+90
-49
lines changed

4 files changed

+90
-49
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ Resolve external types used in `.d.ts` files from `node_modules`.
9191
> [!NOTE]
9292
> These options are only applicable when `oxc` and `tsgo` are not enabled.
9393
94+
#### `build`
95+
96+
Build mode for the TypeScript compiler:
97+
98+
- If `true`, the plugin will use [`tsc -b`](https://www.typescriptlang.org/docs/handbook/project-references.html#build-mode-for-typescript) to build the project and all referenced projects before emitting `.d.ts` files.
99+
- If `false`, the plugin will use [`tsc`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) to emit `.d.ts` files without building referenced projects.
100+
101+
**Default:** `false`
102+
94103
#### `incremental`
95104

96105
Controls how project references and incremental builds are handled:
97106

98-
- If your `tsconfig.json` uses [`references`](https://www.typescriptlang.org/tsconfig/#references), the plugin will use [`tsc -b`](https://www.typescriptlang.org/docs/handbook/project-references.html#build-mode-for-typescript) to build the project and all referenced projects before emitting `.d.ts` files.
99107
- If `incremental` is `true`, all built files (including [`.tsbuildinfo`](https://www.typescriptlang.org/tsconfig/#tsBuildInfoFile)) will be written to disk, similar to running `tsc -b` in your project.
100108
- If `incremental` is `false`, built files are kept in memory, minimizing disk usage.
101109

src/generate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type DtsMap = Map<string, TsModule>
5050
export function createGeneratePlugin({
5151
tsconfig,
5252
tsconfigRaw,
53+
build,
5354
incremental,
5455
cwd,
5556
oxc,
@@ -65,6 +66,7 @@ export function createGeneratePlugin({
6566
| 'cwd'
6667
| 'tsconfig'
6768
| 'tsconfigRaw'
69+
| 'build'
6870
| 'incremental'
6971
| 'oxc'
7072
| 'emitDtsOnly'
@@ -248,6 +250,7 @@ export function createGeneratePlugin({
248250
const options: Omit<TscOptions, 'programs'> = {
249251
tsconfig,
250252
tsconfigRaw,
253+
build,
251254
incremental,
252255
cwd,
253256
entries,

src/options.ts

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
} from 'get-tsconfig'
99
import type { IsolatedDeclarationsOptions } from 'rolldown/experimental'
1010

11-
export interface Options {
11+
//#region General Options
12+
export interface GeneralOptions {
1213
/**
1314
* The directory in which the plugin will search for the `tsconfig.json` file.
1415
*/
@@ -45,6 +46,36 @@ export interface Options {
4546
*/
4647
tsconfigRaw?: Omit<TsConfigJson, 'compilerOptions'>
4748

49+
/**
50+
* Override the `compilerOptions` specified in `tsconfig.json`.
51+
*
52+
* @see https://www.typescriptlang.org/tsconfig/#compilerOptions
53+
*/
54+
compilerOptions?: TsConfigJson.CompilerOptions
55+
56+
/**
57+
* If `true`, the plugin will generate declaration maps (`.d.ts.map`) for `.d.ts` files.
58+
*/
59+
sourcemap?: boolean
60+
61+
/**
62+
* Resolve external types used in `.d.ts` files from `node_modules`.
63+
*/
64+
resolve?: boolean | (string | RegExp)[]
65+
}
66+
67+
//#region tsc Options
68+
export interface TscOptions {
69+
/**
70+
* Build mode for the TypeScript compiler:
71+
*
72+
* - If `true`, the plugin will use [`tsc -b`](https://www.typescriptlang.org/docs/handbook/project-references.html#build-mode-for-typescript) to build the project and all referenced projects before emitting `.d.ts` files.
73+
* - If `false`, the plugin will use [`tsc`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) to emit `.d.ts` files without building referenced projects.
74+
*
75+
* @default false
76+
*/
77+
build?: boolean
78+
4879
/**
4980
* If your tsconfig.json has
5081
* [`references`](https://www.typescriptlang.org/tsconfig/#references) option,
@@ -76,31 +107,6 @@ export interface Options {
76107
*/
77108
incremental?: boolean
78109

79-
/**
80-
* Override the `compilerOptions` specified in `tsconfig.json`.
81-
*
82-
* @see https://www.typescriptlang.org/tsconfig/#compilerOptions
83-
*/
84-
compilerOptions?: TsConfigJson.CompilerOptions
85-
86-
/**
87-
* If `true`, the plugin will generate `.d.ts` files using Oxc,
88-
* which is significantly faster than the TypeScript compiler.
89-
*
90-
* This option is automatically enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
91-
*/
92-
oxc?: boolean | Omit<IsolatedDeclarationsOptions, 'sourcemap'>
93-
94-
/**
95-
* If `true`, the plugin will generate declaration maps (`.d.ts.map`) for `.d.ts` files.
96-
*/
97-
sourcemap?: boolean
98-
99-
/**
100-
* Resolve external types used in `.d.ts` files from `node_modules`.
101-
*/
102-
resolve?: boolean | (string | RegExp)[]
103-
104110
/**
105111
* If `true`, the plugin will generate `.d.ts` files using `vue-tsc`.
106112
*/
@@ -119,16 +125,6 @@ export interface Options {
119125
*/
120126
eager?: boolean
121127

122-
/**
123-
* **[Experimental]** Enables DTS generation using `tsgo`.
124-
*
125-
* To use this option, make sure `@typescript/native-preview` is installed as a dependency.
126-
*
127-
* **Note:** This option is not yet recommended for production environments.
128-
* `tsconfigRaw` and `isolatedDeclarations` options will be ignored when this option is enabled.
129-
*/
130-
tsgo?: boolean
131-
132128
/**
133129
* If `true`, the plugin will create a new isolated context for each build,
134130
* ensuring that previously generated `.d.ts` code and caches are not reused.
@@ -152,6 +148,30 @@ export interface Options {
152148
emitJs?: boolean
153149
}
154150

151+
export interface Options extends GeneralOptions, TscOptions {
152+
//#region Oxc
153+
154+
/**
155+
* If `true`, the plugin will generate `.d.ts` files using Oxc,
156+
* which is significantly faster than the TypeScript compiler.
157+
*
158+
* This option is automatically enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
159+
*/
160+
oxc?: boolean | Omit<IsolatedDeclarationsOptions, 'sourcemap'>
161+
162+
//#region TypeScript Go
163+
164+
/**
165+
* **[Experimental]** Enables DTS generation using `tsgo`.
166+
*
167+
* To use this option, make sure `@typescript/native-preview` is installed as a dependency.
168+
*
169+
* **Note:** This option is not yet recommended for production environments.
170+
* `tsconfigRaw` and `isolatedDeclarations` options will be ignored when this option is enabled.
171+
*/
172+
tsgo?: boolean
173+
}
174+
155175
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
156176

157177
export type OptionsResolved = Overwrite<
@@ -167,21 +187,25 @@ let warnedTsgo = false
167187

168188
export function resolveOptions({
169189
cwd = process.cwd(),
190+
dtsInput = false,
191+
emitDtsOnly = false,
170192
tsconfig,
171-
incremental = false,
172-
compilerOptions = {},
173193
tsconfigRaw: overriddenTsconfigRaw = {},
174-
oxc,
194+
compilerOptions = {},
175195
sourcemap,
176-
dtsInput = false,
177-
emitDtsOnly = false,
178196
resolve = false,
197+
198+
// tsc
199+
build = false,
200+
incremental = false,
179201
vue = false,
180202
parallel = false,
181203
eager = false,
182-
tsgo = false,
183204
newContext = false,
184205
emitJs,
206+
207+
oxc,
208+
tsgo = false,
185209
}: Options): OptionsResolved {
186210
let resolvedTsconfig: TsConfigJsonResolved | undefined
187211
if (tsconfig === true || tsconfig == null) {
@@ -250,19 +274,23 @@ export function resolveOptions({
250274

251275
return {
252276
cwd,
277+
dtsInput,
278+
emitDtsOnly,
253279
tsconfig,
254280
tsconfigRaw,
255-
incremental,
256-
oxc,
257281
sourcemap,
258-
dtsInput,
259-
emitDtsOnly,
260282
resolve,
283+
284+
// tsc
285+
build,
286+
incremental,
261287
vue,
262288
parallel,
263289
eager,
264-
tsgo,
265290
newContext,
266291
emitJs,
292+
293+
oxc,
294+
tsgo,
267295
}
268296
}

src/tsc/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface TscOptions {
1616
tsconfig?: string
1717
tsconfigRaw: TsConfigJson
1818
cwd: string
19+
build: boolean
1920
incremental: boolean
2021
entries?: string[]
2122
id: string
@@ -174,6 +175,7 @@ function createTsProgram({
174175
id,
175176
tsconfig,
176177
tsconfigRaw,
178+
build,
177179
incremental,
178180
vue,
179181
cwd,
@@ -188,7 +190,7 @@ function createTsProgram({
188190
)
189191

190192
// If the tsconfig has project references, build the project tree.
191-
if (tsconfig && parsedConfig.projectReferences?.length) {
193+
if (tsconfig && build) {
192194
// Build the project tree and collect all projects.
193195
const projectPaths = buildSolution(tsconfig, incremental, context)
194196
debug(`collected projects: ${JSON.stringify(projectPaths)}`)

0 commit comments

Comments
 (0)