Skip to content

Commit

Permalink
fix: ensure .d.ts files are cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Nov 20, 2023
1 parent 4247d8f commit e1e8840
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/api-extractor.ts
Expand Up @@ -17,6 +17,7 @@ import {
defaultOutExtension,
ensureTempDeclarationDir,
getApiExtractor,
removeFiles,
toAbsolutePath,
writeFileSync,
} from './utils'
Expand Down Expand Up @@ -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[]
Expand All @@ -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)
}
Expand Down
51 changes: 38 additions & 13 deletions test/index.test.ts
Expand Up @@ -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')
})

0 comments on commit e1e8840

Please sign in to comment.