From 40ec8a2381ed0398221c8235e145d129a404ec98 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 29 Nov 2024 12:43:45 +0800 Subject: [PATCH 1/2] perf: rm buildinfo on demand --- packages/plugin-dts/src/index.ts | 11 +++++++++-- packages/plugin-dts/src/utils.ts | 4 +++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index f278937a2..6bcea539e 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -98,9 +98,10 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ } const tsConfigResult = loadTsconfig(tsconfigPath); + const compilerOptions = tsConfigResult.options; const dtsEmitPath = options.distPath ?? - tsConfigResult.options.declarationDir ?? + compilerOptions.declarationDir ?? config.output?.distPath?.root; const jsExtension = extname(__filename); @@ -123,7 +124,13 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ } // clean tsbuildinfo file - await cleanTsBuildInfoFile(tsconfigPath, tsConfigResult); + if ( + compilerOptions.composite || + compilerOptions.incremental || + options.build + ) { + await cleanTsBuildInfoFile(tsconfigPath, tsConfigResult); + } const dtsGenOptions: DtsGenOptions = { ...options, diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 78a146866..d82f7b3f6 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -263,5 +263,7 @@ export async function cleanTsBuildInfoFile( } } - await fsP.rm(tsbuildInfoFilePath, { force: true }); + if (await pathExists(tsbuildInfoFilePath)) { + await fsP.rm(tsbuildInfoFilePath, { force: true }); + } } From c61857e5d72fcb990fb5cfe5daef785e4afb573a Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 29 Nov 2024 13:25:08 +0800 Subject: [PATCH 2/2] perf: rm buildinfo on demand --- packages/plugin-dts/src/dts.ts | 30 ++++++++++++++++++++++-- packages/plugin-dts/src/index.ts | 40 ++++---------------------------- packages/plugin-dts/src/utils.ts | 4 ++-- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index 3636e8134..4cecde1af 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -12,7 +12,13 @@ import { logger } from '@rsbuild/core'; import color from 'picocolors'; import type { DtsGenOptions } from './index'; import { emitDts } from './tsc'; -import { calcLongestCommonPath, ensureTempDeclarationDir } from './utils'; +import { + calcLongestCommonPath, + cleanDtsFiles, + cleanTsBuildInfoFile, + clearTempDeclarationDir, + ensureTempDeclarationDir, +} from './utils'; const isObject = (obj: unknown): obj is Record => Object.prototype.toString.call(obj) === '[object Object]'; @@ -108,10 +114,12 @@ export const calcBundledPackages = (options: { export async function generateDts(data: DtsGenOptions): Promise { const { bundle, - dtsEmitPath, dtsEntry, tsconfigPath, tsConfigResult, + distPath, + rootDistPath, + cleanDistPath, name, cwd, build, @@ -126,6 +134,24 @@ export async function generateDts(data: DtsGenOptions): Promise { const { options: rawCompilerOptions, fileNames } = tsConfigResult; + const dtsEmitPath = + distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath; + + // clean dts files + if (cleanDistPath !== false) { + await cleanDtsFiles(dtsEmitPath); + } + + // clean .rslib temp folder + if (bundle) { + await clearTempDeclarationDir(cwd); + } + + // clean tsbuildinfo file + if (rawCompilerOptions.composite || rawCompilerOptions.incremental || build) { + await cleanTsBuildInfoFile(tsconfigPath, rawCompilerOptions); + } + // The longest common path of all non-declaration input files. // If composite is set, the default is instead the directory containing the tsconfig.json file. // see https://www.typescriptlang.org/tsconfig/#rootDir diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 6bcea539e..610545492 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -3,13 +3,7 @@ import { dirname, extname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { type RsbuildConfig, type RsbuildPlugin, logger } from '@rsbuild/core'; import ts from 'typescript'; -import { - cleanDtsFiles, - cleanTsBuildInfoFile, - clearTempDeclarationDir, - loadTsconfig, - processSourceEntry, -} from './utils'; +import { loadTsconfig, processSourceEntry } from './utils'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -41,10 +35,11 @@ export type DtsGenOptions = PluginDtsOptions & { cwd: string; isWatch: boolean; dtsEntry: DtsEntry; - dtsEmitPath: string; build?: boolean; tsconfigPath: string; tsConfigResult: ts.ParsedCommandLine; + rootDistPath: string; + cleanDistPath: NonNullable['cleanDistPath']; userExternals?: NonNullable['externals']; }; @@ -98,11 +93,6 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ } const tsConfigResult = loadTsconfig(tsconfigPath); - const compilerOptions = tsConfigResult.options; - const dtsEmitPath = - options.distPath ?? - compilerOptions.declarationDir ?? - config.output?.distPath?.root; const jsExtension = extname(__filename); const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], { @@ -111,34 +101,14 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ childProcesses.push(childProcess); - const { cleanDistPath } = config.output; - - // clean dts files - if (cleanDistPath !== false) { - await cleanDtsFiles(dtsEmitPath); - } - - // clean .rslib temp folder - if (options.bundle) { - await clearTempDeclarationDir(cwd); - } - - // clean tsbuildinfo file - if ( - compilerOptions.composite || - compilerOptions.incremental || - options.build - ) { - await cleanTsBuildInfoFile(tsconfigPath, tsConfigResult); - } - const dtsGenOptions: DtsGenOptions = { ...options, dtsEntry, - dtsEmitPath, + rootDistPath: config.output?.distPath?.root, userExternals: config.output.externals, tsconfigPath, tsConfigResult, + cleanDistPath: config.output.cleanDistPath, name: environment.name, cwd, isWatch, diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index d82f7b3f6..ff93bfa25 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -243,10 +243,10 @@ export async function cleanDtsFiles(dir: string): Promise { export async function cleanTsBuildInfoFile( tsconfigPath: string, - tsConfigResult: ts.ParsedCommandLine, + compilerOptions: ts.CompilerOptions, ): Promise { const tsconfigDir = dirname(tsconfigPath); - const { outDir, rootDir, tsBuildInfoFile } = tsConfigResult.options; + const { outDir, rootDir, tsBuildInfoFile } = compilerOptions; let tsbuildInfoFilePath = `${basename( tsconfigPath, '.json',