这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(experimental-dts): make --experimental-dts to be compatible with --clean #1041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/api-extractor.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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')
})