Skip to content

Commit c233f2d

Browse files
authored
perf: rm buildinfo on demand (#495)
1 parent 37bc7ee commit c233f2d

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

packages/plugin-dts/src/dts.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import { logger } from '@rsbuild/core';
1212
import color from 'picocolors';
1313
import type { DtsGenOptions } from './index';
1414
import { emitDts } from './tsc';
15-
import { calcLongestCommonPath, ensureTempDeclarationDir } from './utils';
15+
import {
16+
calcLongestCommonPath,
17+
cleanDtsFiles,
18+
cleanTsBuildInfoFile,
19+
clearTempDeclarationDir,
20+
ensureTempDeclarationDir,
21+
} from './utils';
1622

1723
const isObject = (obj: unknown): obj is Record<string, any> =>
1824
Object.prototype.toString.call(obj) === '[object Object]';
@@ -108,10 +114,12 @@ export const calcBundledPackages = (options: {
108114
export async function generateDts(data: DtsGenOptions): Promise<void> {
109115
const {
110116
bundle,
111-
dtsEmitPath,
112117
dtsEntry,
113118
tsconfigPath,
114119
tsConfigResult,
120+
distPath,
121+
rootDistPath,
122+
cleanDistPath,
115123
name,
116124
cwd,
117125
build,
@@ -126,6 +134,24 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
126134

127135
const { options: rawCompilerOptions, fileNames } = tsConfigResult;
128136

137+
const dtsEmitPath =
138+
distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath;
139+
140+
// clean dts files
141+
if (cleanDistPath !== false) {
142+
await cleanDtsFiles(dtsEmitPath);
143+
}
144+
145+
// clean .rslib temp folder
146+
if (bundle) {
147+
await clearTempDeclarationDir(cwd);
148+
}
149+
150+
// clean tsbuildinfo file
151+
if (rawCompilerOptions.composite || rawCompilerOptions.incremental || build) {
152+
await cleanTsBuildInfoFile(tsconfigPath, rawCompilerOptions);
153+
}
154+
129155
// The longest common path of all non-declaration input files.
130156
// If composite is set, the default is instead the directory containing the tsconfig.json file.
131157
// see https://www.typescriptlang.org/tsconfig/#rootDir

packages/plugin-dts/src/index.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import { dirname, extname, join } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { type RsbuildConfig, type RsbuildPlugin, logger } from '@rsbuild/core';
55
import ts from 'typescript';
6-
import {
7-
cleanDtsFiles,
8-
cleanTsBuildInfoFile,
9-
clearTempDeclarationDir,
10-
loadTsconfig,
11-
processSourceEntry,
12-
} from './utils';
6+
import { loadTsconfig, processSourceEntry } from './utils';
137

148
const __filename = fileURLToPath(import.meta.url);
159
const __dirname = dirname(__filename);
@@ -41,10 +35,11 @@ export type DtsGenOptions = PluginDtsOptions & {
4135
cwd: string;
4236
isWatch: boolean;
4337
dtsEntry: DtsEntry;
44-
dtsEmitPath: string;
4538
build?: boolean;
4639
tsconfigPath: string;
4740
tsConfigResult: ts.ParsedCommandLine;
41+
rootDistPath: string;
42+
cleanDistPath: NonNullable<RsbuildConfig['output']>['cleanDistPath'];
4843
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
4944
};
5045

@@ -98,10 +93,6 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
9893
}
9994

10095
const tsConfigResult = loadTsconfig(tsconfigPath);
101-
const dtsEmitPath =
102-
options.distPath ??
103-
tsConfigResult.options.declarationDir ??
104-
config.output?.distPath?.root;
10596

10697
const jsExtension = extname(__filename);
10798
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
@@ -110,28 +101,14 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
110101

111102
childProcesses.push(childProcess);
112103

113-
const { cleanDistPath } = config.output;
114-
115-
// clean dts files
116-
if (cleanDistPath !== false) {
117-
await cleanDtsFiles(dtsEmitPath);
118-
}
119-
120-
// clean .rslib temp folder
121-
if (options.bundle) {
122-
await clearTempDeclarationDir(cwd);
123-
}
124-
125-
// clean tsbuildinfo file
126-
await cleanTsBuildInfoFile(tsconfigPath, tsConfigResult);
127-
128104
const dtsGenOptions: DtsGenOptions = {
129105
...options,
130106
dtsEntry,
131-
dtsEmitPath,
107+
rootDistPath: config.output?.distPath?.root,
132108
userExternals: config.output.externals,
133109
tsconfigPath,
134110
tsConfigResult,
111+
cleanDistPath: config.output.cleanDistPath,
135112
name: environment.name,
136113
cwd,
137114
isWatch,

packages/plugin-dts/src/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ export async function cleanDtsFiles(dir: string): Promise<void> {
243243

244244
export async function cleanTsBuildInfoFile(
245245
tsconfigPath: string,
246-
tsConfigResult: ts.ParsedCommandLine,
246+
compilerOptions: ts.CompilerOptions,
247247
): Promise<void> {
248248
const tsconfigDir = dirname(tsconfigPath);
249-
const { outDir, rootDir, tsBuildInfoFile } = tsConfigResult.options;
249+
const { outDir, rootDir, tsBuildInfoFile } = compilerOptions;
250250
let tsbuildInfoFilePath = `${basename(
251251
tsconfigPath,
252252
'.json',
@@ -263,5 +263,7 @@ export async function cleanTsBuildInfoFile(
263263
}
264264
}
265265

266-
await fsP.rm(tsbuildInfoFilePath, { force: true });
266+
if (await pathExists(tsbuildInfoFilePath)) {
267+
await fsP.rm(tsbuildInfoFilePath, { force: true });
268+
}
267269
}

0 commit comments

Comments
 (0)