Skip to content

Commit

Permalink
fix(experimental-dts): make --experimental-dts to be compatible wit…
Browse files Browse the repository at this point in the history
…h `--clean` (#1041)
  • Loading branch information
ocavue committed Nov 20, 2023
1 parent 731f43f commit 8c26e63
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -297,7 +297,7 @@ export async function build(_options: Options) {
: []
// .d.ts files are removed in the `dtsTask` instead
// `dtsTask` is a separate process, which might start before `mainTasks`
if (options.dts) {
if (options.dts || options.experimentalDts) {
extraPatterns.unshift('!**/*.d.{ts,cts,mts}')
}
await removeFiles(['**/*', ...extraPatterns], options.outDir)
Expand Down
47 changes: 47 additions & 0 deletions test/index.test.ts
Expand Up @@ -1668,3 +1668,50 @@ test('should only include exported declarations with experimentalDts', async ()
expect(entry2dts).toContain('declare2')
expect(entry2dts).not.toContain('declare1')
})

test('.d.ts files should be cleaned when --clean and --experimental-dts are provided', async () => {
const filesFoo = {
'package.json': `{ "name": "tsup-playground", "private": true }`,
'foo.ts': `export const foo = 1`,
}

const filesFooBar = {
...filesFoo,
'bar.ts': `export const bar = 2`,
}

// 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'],
})

// 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'],
})

// 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')
})

1 comment on commit 8c26e63

@vercel
Copy link

@vercel vercel bot commented on 8c26e63 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tsup – ./

tsup-git-main-egoist.vercel.app
tsup.vercel.app
tsup-egoist.vercel.app
tsup.egoist.dev

Please sign in to comment.