diff --git a/src/api-extractor.ts b/src/api-extractor.ts index ea2a5a6c..8e269e9e 100644 --- a/src/api-extractor.ts +++ b/src/api-extractor.ts @@ -17,6 +17,7 @@ import { defaultOutExtension, ensureTempDeclarationDir, getApiExtractor, + removeFiles, toAbsolutePath, writeFileSync, } from './utils' @@ -135,6 +136,12 @@ async function rollupDtsFiles( } } +function cleanDtsFiles(options: NormalizedOptions) { + if (options.clean) { + removeFiles(['**/*.d.{ts,mts,cts}'], options.outDir) + } +} + export async function runDtsRollup( options: NormalizedOptions, exports?: ExportDeclaration[] @@ -149,6 +156,7 @@ export async function runDtsRollup( if (!exports) { throw new Error('Unexpected internal error: dts exports is not define') } + cleanDtsFiles(options) for (const format of options.format) { await rollupDtsFiles(options, exports, format) } diff --git a/test/index.test.ts b/test/index.test.ts index 7d47613b..71c946a2 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1669,24 +1669,49 @@ test('should only include exported declarations with experimentalDts', async () expect(entry2dts).not.toContain('declare1') }) -test('--experimental-dts should work when --clean is provided', async () => { - const files = { +test('.d.ts files should be cleaned when --clean and --experimental-dts are provided', async () => { + const filesFoo = { 'package.json': `{ "name": "tsup-playground", "private": true }`, - 'input.ts': `export const foo = 1`, + 'foo.ts': `export const foo = 1`, + } + + const filesFooBar = { + ...filesFoo, + 'bar.ts': `export const bar = 2`, } - const withoutClean = await run(getTestName(), files, { + + // First run with both foo and bar + const result1 = await run(getTestName(), filesFooBar, { + entry: ['foo.ts', 'bar.ts'], + flags: ['--experimental-dts'], + }) + + expect(result1.outFiles).toContain('foo.d.ts') + expect(result1.outFiles).toContain('foo.js') + expect(result1.outFiles).toContain('bar.d.ts') + expect(result1.outFiles).toContain('bar.js') + + // Second run with only foo + const result2 = await run(getTestName(), filesFoo, { + entry: ['foo.ts'], flags: ['--experimental-dts'], }) - const withClean = await run(getTestName(), files, { + + // When --clean is not provided, the previous bar.* files should still exist + expect(result2.outFiles).toContain('foo.d.ts') + expect(result2.outFiles).toContain('foo.js') + expect(result2.outFiles).toContain('bar.d.ts') + expect(result2.outFiles).toContain('bar.js') + + // Third run with only foo and --clean + const result3 = await run(getTestName(), filesFoo, { + entry: ['foo.ts'], flags: ['--experimental-dts', '--clean'], }) - expect(withClean.outFiles).toEqual(withoutClean.outFiles) - expect(withClean.outFiles).toMatchInlineSnapshot(` - [ - "_tsup-dts-rollup.d.ts", - "input.d.ts", - "input.js", - ] - `) + // When --clean is provided, the previous bar.* files should be deleted + expect(result3.outFiles).toContain('foo.d.ts') + expect(result3.outFiles).toContain('foo.js') + expect(result3.outFiles).not.toContain('bar.d.ts') + expect(result3.outFiles).not.toContain('bar.js') })