Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> =>
Object.prototype.toString.call(obj) === '[object Object]';
Expand Down Expand Up @@ -108,10 +114,12 @@ export const calcBundledPackages = (options: {
export async function generateDts(data: DtsGenOptions): Promise<void> {
const {
bundle,
dtsEmitPath,
dtsEntry,
tsconfigPath,
tsConfigResult,
distPath,
rootDistPath,
cleanDistPath,
name,
cwd,
build,
Expand All @@ -126,6 +134,24 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {

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
Expand Down
33 changes: 5 additions & 28 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<RsbuildConfig['output']>['cleanDistPath'];
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
};

Expand Down Expand Up @@ -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}`), [], {
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ export async function cleanDtsFiles(dir: string): Promise<void> {

export async function cleanTsBuildInfoFile(
tsconfigPath: string,
tsConfigResult: ts.ParsedCommandLine,
compilerOptions: ts.CompilerOptions,
): Promise<void> {
const tsconfigDir = dirname(tsconfigPath);
const { outDir, rootDir, tsBuildInfoFile } = tsConfigResult.options;
const { outDir, rootDir, tsBuildInfoFile } = compilerOptions;
let tsbuildInfoFilePath = `${basename(
tsconfigPath,
'.json',
Expand All @@ -263,5 +263,7 @@ export async function cleanTsBuildInfoFile(
}
}

await fsP.rm(tsbuildInfoFilePath, { force: true });
if (await pathExists(tsbuildInfoFilePath)) {
await fsP.rm(tsbuildInfoFilePath, { force: true });
}
}
Loading