Skip to content

Commit acde863

Browse files
committed
feat: support tsc --build to incremental build project references
1 parent 9e8b683 commit acde863

File tree

35 files changed

+575
-110
lines changed

35 files changed

+575
-110
lines changed

packages/core/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ const composeDtsConfig = async (
930930
// Only setting ⁠dts.bundle to true will generate the bundled d.ts.
931931
bundle: dts?.bundle ?? false,
932932
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
933+
build: dts?.build ?? false,
933934
abortOnError: dts?.abortOnError ?? true,
934935
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
935936
autoExternal,

packages/core/src/types/config/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export type Syntax =
2929
| string[];
3030

3131
export type Dts =
32-
| (Pick<PluginDtsOptions, 'bundle' | 'distPath' | 'abortOnError'> & {
32+
| (Pick<
33+
PluginDtsOptions,
34+
'bundle' | 'distPath' | 'abortOnError' | 'build'
35+
> & {
3336
autoExtension?: boolean;
3437
})
3538
| boolean;

packages/plugin-dts/src/dts.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import fs from 'node:fs';
2-
import { basename, dirname, isAbsolute, join, relative } from 'node:path';
2+
import {
3+
basename,
4+
dirname,
5+
isAbsolute,
6+
join,
7+
relative,
8+
resolve,
9+
} from 'node:path';
310
import { logger } from '@rsbuild/core';
411
import color from 'picocolors';
512
import ts from 'typescript';
@@ -110,6 +117,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
110117
tsconfigPath,
111118
name,
112119
cwd,
120+
build,
113121
isWatch,
114122
dtsExtension = '.d.ts',
115123
autoExternal = true,
@@ -133,6 +141,32 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
133141
? distPath
134142
: rawCompilerOptions.declarationDir || './dist';
135143

144+
if (build) {
145+
// do not allow to use bundle DTS when 'build: true' since temp declarationDir should be set by user in tsconfig
146+
if (bundle) {
147+
throw Error(`Can not set "dts.bundle: true" when "dts.build = true"`);
148+
}
149+
150+
// can not set '--declarationDir' or '--outDir' when 'build: true'.
151+
if (
152+
(!rawCompilerOptions.outDir ||
153+
rawCompilerOptions.outDir !== resolve(dirname(configPath), outDir)) &&
154+
(!rawCompilerOptions.declarationDir ||
155+
rawCompilerOptions.declarationDir !==
156+
resolve(dirname(configPath), outDir))
157+
) {
158+
const info =
159+
rawCompilerOptions.outDir && !rawCompilerOptions.declarationDir
160+
? 'outDir'
161+
: 'declarationDir';
162+
throw Error(
163+
`Please set ${info}: "${outDir}" in ${color.underline(
164+
configPath,
165+
)} to keep it same as Lib config.`,
166+
);
167+
}
168+
}
169+
136170
const getDeclarationDir = (bundle: boolean, distPath?: string) => {
137171
if (bundle) {
138172
return ensureTempDeclarationDir(cwd);
@@ -204,6 +238,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
204238
onComplete,
205239
bundle,
206240
isWatch,
241+
build,
207242
);
208243

209244
if (!isWatch) {

packages/plugin-dts/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const __dirname = dirname(__filename);
1010
export type PluginDtsOptions = {
1111
bundle?: boolean;
1212
distPath?: string;
13+
build?: boolean;
1314
abortOnError?: boolean;
1415
dtsExtension?: string;
1516
autoExternal?:
@@ -33,6 +34,7 @@ export type DtsGenOptions = PluginDtsOptions & {
3334
cwd: string;
3435
isWatch: boolean;
3536
dtsEntry: DtsEntry;
37+
build?: boolean;
3638
tsconfigPath?: string;
3739
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
3840
};
@@ -46,14 +48,14 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts';
4648

4749
// use ts compiler API to generate bundleless dts
4850
// use ts compiler API and api-extractor to generate dts bundle
49-
// TODO: support incremental build, to build one or more projects and their dependencies
5051
// TODO: deal alias in dts
5152
export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
5253
name: PLUGIN_DTS_NAME,
5354

5455
setup(api) {
5556
options.bundle = options.bundle ?? false;
5657
options.abortOnError = options.abortOnError ?? true;
58+
options.build = options.build || false;
5759

5860
const dtsPromises: Promise<TaskResult>[] = [];
5961
let promisesResult: TaskResult[] = [];

0 commit comments

Comments
 (0)