Skip to content

Commit 8c26e63

Browse files
authored
fix(experimental-dts): make --experimental-dts to be compatible with --clean (#1041)
1 parent 731f43f commit 8c26e63

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/api-extractor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
defaultOutExtension,
1818
ensureTempDeclarationDir,
1919
getApiExtractor,
20+
removeFiles,
2021
toAbsolutePath,
2122
writeFileSync,
2223
} from './utils'
@@ -135,6 +136,12 @@ async function rollupDtsFiles(
135136
}
136137
}
137138

139+
function cleanDtsFiles(options: NormalizedOptions) {
140+
if (options.clean) {
141+
removeFiles(['**/*.d.{ts,mts,cts}'], options.outDir)
142+
}
143+
}
144+
138145
export async function runDtsRollup(
139146
options: NormalizedOptions,
140147
exports?: ExportDeclaration[]
@@ -149,6 +156,7 @@ export async function runDtsRollup(
149156
if (!exports) {
150157
throw new Error('Unexpected internal error: dts exports is not define')
151158
}
159+
cleanDtsFiles(options)
152160
for (const format of options.format) {
153161
await rollupDtsFiles(options, exports, format)
154162
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export async function build(_options: Options) {
297297
: []
298298
// .d.ts files are removed in the `dtsTask` instead
299299
// `dtsTask` is a separate process, which might start before `mainTasks`
300-
if (options.dts) {
300+
if (options.dts || options.experimentalDts) {
301301
extraPatterns.unshift('!**/*.d.{ts,cts,mts}')
302302
}
303303
await removeFiles(['**/*', ...extraPatterns], options.outDir)

test/index.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,3 +1668,50 @@ test('should only include exported declarations with experimentalDts', async ()
16681668
expect(entry2dts).toContain('declare2')
16691669
expect(entry2dts).not.toContain('declare1')
16701670
})
1671+
1672+
test('.d.ts files should be cleaned when --clean and --experimental-dts are provided', async () => {
1673+
const filesFoo = {
1674+
'package.json': `{ "name": "tsup-playground", "private": true }`,
1675+
'foo.ts': `export const foo = 1`,
1676+
}
1677+
1678+
const filesFooBar = {
1679+
...filesFoo,
1680+
'bar.ts': `export const bar = 2`,
1681+
}
1682+
1683+
// First run with both foo and bar
1684+
const result1 = await run(getTestName(), filesFooBar, {
1685+
entry: ['foo.ts', 'bar.ts'],
1686+
flags: ['--experimental-dts'],
1687+
})
1688+
1689+
expect(result1.outFiles).toContain('foo.d.ts')
1690+
expect(result1.outFiles).toContain('foo.js')
1691+
expect(result1.outFiles).toContain('bar.d.ts')
1692+
expect(result1.outFiles).toContain('bar.js')
1693+
1694+
// Second run with only foo
1695+
const result2 = await run(getTestName(), filesFoo, {
1696+
entry: ['foo.ts'],
1697+
flags: ['--experimental-dts'],
1698+
})
1699+
1700+
// When --clean is not provided, the previous bar.* files should still exist
1701+
expect(result2.outFiles).toContain('foo.d.ts')
1702+
expect(result2.outFiles).toContain('foo.js')
1703+
expect(result2.outFiles).toContain('bar.d.ts')
1704+
expect(result2.outFiles).toContain('bar.js')
1705+
1706+
// Third run with only foo and --clean
1707+
const result3 = await run(getTestName(), filesFoo, {
1708+
entry: ['foo.ts'],
1709+
flags: ['--experimental-dts', '--clean'],
1710+
})
1711+
1712+
// When --clean is provided, the previous bar.* files should be deleted
1713+
expect(result3.outFiles).toContain('foo.d.ts')
1714+
expect(result3.outFiles).toContain('foo.js')
1715+
expect(result3.outFiles).not.toContain('bar.d.ts')
1716+
expect(result3.outFiles).not.toContain('bar.js')
1717+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy