Skip to content

fix: clean dts distPath earlier #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
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: 2 additions & 28 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ import { logger } from '@rsbuild/core';
import color from 'picocolors';
import type { DtsGenOptions } from './index';
import { emitDts } from './tsc';
import {
calcLongestCommonPath,
cleanDtsFiles,
cleanTsBuildInfoFile,
clearTempDeclarationDir,
ensureTempDeclarationDir,
} from './utils';
import { calcLongestCommonPath, ensureTempDeclarationDir } from './utils';

const isObject = (obj: unknown): obj is Record<string, any> =>
Object.prototype.toString.call(obj) === '[object Object]';
Expand Down Expand Up @@ -115,11 +109,9 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
const {
bundle,
dtsEntry,
dtsEmitPath,
tsconfigPath,
tsConfigResult,
distPath,
rootDistPath,
cleanDistPath,
name,
cwd,
build,
Expand All @@ -138,24 +130,6 @@ 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
38 changes: 33 additions & 5 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { fileURLToPath } from 'node:url';
import { type RsbuildConfig, type RsbuildPlugin, logger } from '@rsbuild/core';
import color from 'picocolors';
import ts from 'typescript';
import { loadTsconfig, processSourceEntry } from './utils';
import {
cleanDtsFiles,
cleanTsBuildInfoFile,
clearTempDeclarationDir,
loadTsconfig,
processSourceEntry,
} from './utils';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down Expand Up @@ -42,11 +48,10 @@ 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 @@ -104,6 +109,30 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
}

const tsConfigResult = loadTsconfig(tsconfigPath);
const { options: rawCompilerOptions } = tsConfigResult;
const dtsEmitPath =
options.distPath ??
rawCompilerOptions.declarationDir ??
config.output?.distPath?.root;

// clean dts files
if (config.output.cleanDistPath !== false) {
await cleanDtsFiles(dtsEmitPath);
}

// clean .rslib temp folder
if (options.bundle) {
await clearTempDeclarationDir(cwd);
}

// clean tsbuildinfo file
if (
rawCompilerOptions.composite ||
rawCompilerOptions.incremental ||
options.build
) {
await cleanTsBuildInfoFile(tsconfigPath, rawCompilerOptions);
}

const jsExtension = extname(__filename);
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
Expand All @@ -115,11 +144,10 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
const dtsGenOptions: DtsGenOptions = {
...options,
dtsEntry,
rootDistPath: config.output?.distPath?.root,
dtsEmitPath,
userExternals: config.output.externals,
tsconfigPath,
tsConfigResult,
cleanDistPath: config.output.cleanDistPath,
name: environment.name,
cwd,
isWatch,
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/integration/dts/copy/copy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Copy = {
from: string;
to: string;
context: string;
};
6 changes: 6 additions & 0 deletions tests/integration/dts/copy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-copy-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
21 changes: 21 additions & 0 deletions tests/integration/dts/copy/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
dts: true,
}),
],
output: {
copy: {
patterns: [
{
from: './copy.d.ts',
to: './copy.d.ts',
context: __dirname,
},
],
},
},
});
2 changes: 2 additions & 0 deletions tests/integration/dts/copy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const a = 'hello world';
export type A = string;
9 changes: 9 additions & 0 deletions tests/integration/dts/copy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"composite": true
},
"include": ["src"]
}
17 changes: 17 additions & 0 deletions tests/integration/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,20 @@ describe('dts when composite: true', () => {
expect(existsSync(buildInfoPath)).toBeTruthy();
});
});

describe('use with other features', async () => {
test('use output.copy to copy dts files', async () => {
const fixturePath = join(__dirname, 'copy');
const { files } = await buildAndGetResults({
fixturePath,
type: 'dts',
});

expect(files.esm).toMatchInlineSnapshot(`
[
"<ROOT>/tests/integration/dts/copy/dist/esm/copy.d.ts",
"<ROOT>/tests/integration/dts/copy/dist/esm/index.d.ts",
]
`);
});
});
Loading