diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index c62f69ce0..3423e5816 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -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 => Object.prototype.toString.call(obj) === '[object Object]'; @@ -115,11 +109,9 @@ export async function generateDts(data: DtsGenOptions): Promise { const { bundle, dtsEntry, + dtsEmitPath, tsconfigPath, tsConfigResult, - distPath, - rootDistPath, - cleanDistPath, name, cwd, build, @@ -138,24 +130,6 @@ 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 001aea64d..a56fa320e 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -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); @@ -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['cleanDistPath']; userExternals?: NonNullable['externals']; }; @@ -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}`), [], { @@ -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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ca7e03ce..0bdd51bf9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -682,6 +682,8 @@ importers: tests/integration/dts/composite/process-files: {} + tests/integration/dts/copy: {} + tests/integration/entry/default: {} tests/integration/entry/duplicate: {} diff --git a/tests/integration/dts/copy/copy.d.ts b/tests/integration/dts/copy/copy.d.ts new file mode 100644 index 000000000..27e281d93 --- /dev/null +++ b/tests/integration/dts/copy/copy.d.ts @@ -0,0 +1,5 @@ +export type Copy = { + from: string; + to: string; + context: string; +}; diff --git a/tests/integration/dts/copy/package.json b/tests/integration/dts/copy/package.json new file mode 100644 index 000000000..ac7001309 --- /dev/null +++ b/tests/integration/dts/copy/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-copy-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts/copy/rslib.config.ts b/tests/integration/dts/copy/rslib.config.ts new file mode 100644 index 000000000..a1584c536 --- /dev/null +++ b/tests/integration/dts/copy/rslib.config.ts @@ -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, + }, + ], + }, + }, +}); diff --git a/tests/integration/dts/copy/src/index.ts b/tests/integration/dts/copy/src/index.ts new file mode 100644 index 000000000..ac65b18f2 --- /dev/null +++ b/tests/integration/dts/copy/src/index.ts @@ -0,0 +1,2 @@ +export const a = 'hello world'; +export type A = string; diff --git a/tests/integration/dts/copy/tsconfig.json b/tests/integration/dts/copy/tsconfig.json new file mode 100644 index 000000000..93773dda6 --- /dev/null +++ b/tests/integration/dts/copy/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "baseUrl": "./", + "rootDir": "src", + "composite": true + }, + "include": ["src"] +} diff --git a/tests/integration/dts/index.test.ts b/tests/integration/dts/index.test.ts index 76ac94432..6ae3d852b 100644 --- a/tests/integration/dts/index.test.ts +++ b/tests/integration/dts/index.test.ts @@ -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(` + [ + "/tests/integration/dts/copy/dist/esm/copy.d.ts", + "/tests/integration/dts/copy/dist/esm/index.d.ts", + ] + `); + }); +});