Skip to content

Commit

Permalink
fix(experimental-dts): only include exported declarations (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue authored Nov 20, 2023
1 parent d8a203c commit 731f43f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 65 deletions.
7 changes: 6 additions & 1 deletion src/api-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ async function rollupDtsFiles(
sourceFileName = toAbsolutePath(sourceFileName)
const outFileName = path.join(outDir, out + dtsExtension)

// Find all declarations that are exported from the current source file
const currentExports = exports.filter(
(declaration) => declaration.sourceFileName === sourceFileName
)

writeFileSync(
outFileName,
formatDistributionExports(exports, outFileName, dtsOutputFilePath)
formatDistributionExports(currentExports, outFileName, dtsOutputFilePath)
)
}
}
Expand Down
78 changes: 14 additions & 64 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -340,44 +340,22 @@ export { }
// dist/index.d.mts
//////////////////////////////////////////////////////////////////////
export { render } from './_tsup-dts-rollup';
export { ClientRenderOptions } from './_tsup-dts-rollup';
export { sharedFunction } from './_tsup-dts-rollup';
export { sharedType } from './_tsup-dts-rollup';
export { VERSION } from './_tsup-dts-rollup';
export { default_alias as default } from './_tsup-dts-rollup';
export { ServerRenderOptions } from './_tsup-dts-rollup';
export { serverConstant } from './_tsup-dts-rollup';
export { serverConstantAlias } from './_tsup-dts-rollup';
export { ServerClass } from './_tsup-dts-rollup';
export { ServerThirdPartyNamespace } from './_tsup-dts-rollup';
export { renderToString } from './_tsup-dts-rollup';
export { renderToNodeStream } from './_tsup-dts-rollup';
export { renderToStaticMarkup } from './_tsup-dts-rollup';
export { renderToStaticNodeStream } from './_tsup-dts-rollup';
export { version } from './_tsup-dts-rollup';
export { render_alias_1 as render } from './_tsup-dts-rollup';
export { ClientRenderOptions_alias_1 as ClientRenderOptions } from './_tsup-dts-rollup';
export { sharedFunction_alias_1 as sharedFunction } from './_tsup-dts-rollup';
export { sharedType_alias_1 as sharedType } from './_tsup-dts-rollup';
//////////////////////////////////////////////////////////////////////
// dist/index.d.ts
//////////////////////////////////////////////////////////////////////
export { render } from './_tsup-dts-rollup';
export { ClientRenderOptions } from './_tsup-dts-rollup';
export { sharedFunction } from './_tsup-dts-rollup';
export { sharedType } from './_tsup-dts-rollup';
export { VERSION } from './_tsup-dts-rollup';
export { default_alias as default } from './_tsup-dts-rollup';
export { ServerRenderOptions } from './_tsup-dts-rollup';
export { serverConstant } from './_tsup-dts-rollup';
export { serverConstantAlias } from './_tsup-dts-rollup';
export { ServerClass } from './_tsup-dts-rollup';
export { ServerThirdPartyNamespace } from './_tsup-dts-rollup';
export { renderToString } from './_tsup-dts-rollup';
export { renderToNodeStream } from './_tsup-dts-rollup';
export { renderToStaticMarkup } from './_tsup-dts-rollup';
export { renderToStaticNodeStream } from './_tsup-dts-rollup';
export { version } from './_tsup-dts-rollup';
export { render_alias_1 as render } from './_tsup-dts-rollup';
export { ClientRenderOptions_alias_1 as ClientRenderOptions } from './_tsup-dts-rollup';
export { sharedFunction_alias_1 as sharedFunction } from './_tsup-dts-rollup';
export { sharedType_alias_1 as sharedType } from './_tsup-dts-rollup';
//////////////////////////////////////////////////////////////////////
Expand All @@ -388,18 +366,6 @@ export { render } from './_tsup-dts-rollup';
export { ClientRenderOptions } from './_tsup-dts-rollup';
export { sharedFunction } from './_tsup-dts-rollup';
export { sharedType } from './_tsup-dts-rollup';
export { VERSION } from './_tsup-dts-rollup';
export { default_alias as default } from './_tsup-dts-rollup';
export { ServerRenderOptions } from './_tsup-dts-rollup';
export { serverConstant } from './_tsup-dts-rollup';
export { serverConstantAlias } from './_tsup-dts-rollup';
export { ServerClass } from './_tsup-dts-rollup';
export { ServerThirdPartyNamespace } from './_tsup-dts-rollup';
export { renderToString } from './_tsup-dts-rollup';
export { renderToNodeStream } from './_tsup-dts-rollup';
export { renderToStaticMarkup } from './_tsup-dts-rollup';
export { renderToStaticNodeStream } from './_tsup-dts-rollup';
export { version } from './_tsup-dts-rollup';
//////////////////////////////////////////////////////////////////////
Expand All @@ -410,35 +376,21 @@ export { render } from './_tsup-dts-rollup';
export { ClientRenderOptions } from './_tsup-dts-rollup';
export { sharedFunction } from './_tsup-dts-rollup';
export { sharedType } from './_tsup-dts-rollup';
export { VERSION } from './_tsup-dts-rollup';
export { default_alias as default } from './_tsup-dts-rollup';
export { ServerRenderOptions } from './_tsup-dts-rollup';
export { serverConstant } from './_tsup-dts-rollup';
export { serverConstantAlias } from './_tsup-dts-rollup';
export { ServerClass } from './_tsup-dts-rollup';
export { ServerThirdPartyNamespace } from './_tsup-dts-rollup';
export { renderToString } from './_tsup-dts-rollup';
export { renderToNodeStream } from './_tsup-dts-rollup';
export { renderToStaticMarkup } from './_tsup-dts-rollup';
export { renderToStaticNodeStream } from './_tsup-dts-rollup';
export { version } from './_tsup-dts-rollup';
//////////////////////////////////////////////////////////////////////
// dist/server/index.d.mts
//////////////////////////////////////////////////////////////////////
export { render } from '../_tsup-dts-rollup';
export { ClientRenderOptions } from '../_tsup-dts-rollup';
export { sharedFunction } from '../_tsup-dts-rollup';
export { sharedType } from '../_tsup-dts-rollup';
export { VERSION } from '../_tsup-dts-rollup';
export { render_alias_2 as render } from '../_tsup-dts-rollup';
export { default_alias as default } from '../_tsup-dts-rollup';
export { ServerRenderOptions } from '../_tsup-dts-rollup';
export { serverConstant } from '../_tsup-dts-rollup';
export { serverConstantAlias } from '../_tsup-dts-rollup';
export { ServerClass } from '../_tsup-dts-rollup';
export { ServerThirdPartyNamespace } from '../_tsup-dts-rollup';
export { sharedFunction_alias_2 as sharedFunction } from '../_tsup-dts-rollup';
export { sharedType_alias_2 as sharedType } from '../_tsup-dts-rollup';
export { renderToString } from '../_tsup-dts-rollup';
export { renderToNodeStream } from '../_tsup-dts-rollup';
export { renderToStaticMarkup } from '../_tsup-dts-rollup';
Expand All @@ -450,17 +402,15 @@ export { version } from '../_tsup-dts-rollup';
// dist/server/index.d.ts
//////////////////////////////////////////////////////////////////////
export { render } from '../_tsup-dts-rollup';
export { ClientRenderOptions } from '../_tsup-dts-rollup';
export { sharedFunction } from '../_tsup-dts-rollup';
export { sharedType } from '../_tsup-dts-rollup';
export { VERSION } from '../_tsup-dts-rollup';
export { render_alias_2 as render } from '../_tsup-dts-rollup';
export { default_alias as default } from '../_tsup-dts-rollup';
export { ServerRenderOptions } from '../_tsup-dts-rollup';
export { serverConstant } from '../_tsup-dts-rollup';
export { serverConstantAlias } from '../_tsup-dts-rollup';
export { ServerClass } from '../_tsup-dts-rollup';
export { ServerThirdPartyNamespace } from '../_tsup-dts-rollup';
export { sharedFunction_alias_2 as sharedFunction } from '../_tsup-dts-rollup';
export { sharedType_alias_2 as sharedType } from '../_tsup-dts-rollup';
export { renderToString } from '../_tsup-dts-rollup';
export { renderToNodeStream } from '../_tsup-dts-rollup';
export { renderToStaticMarkup } from '../_tsup-dts-rollup';
Expand Down
35 changes: 35 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1633,3 +1633,38 @@ test('should emit declaration files with experimentalDts', async () => {
)
expect(snapshots.sort().join('\n')).toMatchSnapshot()
})

test('should only include exported declarations with experimentalDts', async () => {
const files = {
'package.json': `{ "name": "tsup-playground", "private": true }`,
'tsconfig.json': `{ "compilerOptions": { "skipLibCheck": true } }`,
'tsup.config.ts': `
export default {
entry: ['./src/entry1.ts', './src/entry2.ts']
}
`,
'src/shared.ts': `
export const declare1 = 'declare1'
export const declare2 = 'declare2'
`,
'src/entry1.ts': `
export { declare1 } from './shared'
`,
'src/entry2.ts': `
export { declare2 } from './shared'
`,
}
const { getFileContent } = await run(getTestName(), files, {
entry: [],
flags: ['--experimental-dts'],
})

let entry1dts = await getFileContent('dist/entry1.d.ts')
let entry2dts = await getFileContent('dist/entry2.d.ts')

expect(entry1dts).toContain('declare1')
expect(entry1dts).not.toContain('declare2')

expect(entry2dts).toContain('declare2')
expect(entry2dts).not.toContain('declare1')
})

0 comments on commit 731f43f

Please sign in to comment.