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 f278937a2..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,10 +93,6 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ } const tsConfigResult = loadTsconfig(tsconfigPath); - const dtsEmitPath = - options.distPath ?? - tsConfigResult.options.declarationDir ?? - config.output?.distPath?.root; const jsExtension = extname(__filename); const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], { @@ -110,28 +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 - 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 78a146866..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', @@ -263,5 +263,7 @@ export async function cleanTsBuildInfoFile( } } - await fsP.rm(tsbuildInfoFilePath, { force: true }); + if (await pathExists(tsbuildInfoFilePath)) { + await fsP.rm(tsbuildInfoFilePath, { force: true }); + } }