From f83baf86c89a04d3ab31f26fc50216a85d60f617 Mon Sep 17 00:00:00 2001 From: ckohen Date: Sun, 19 Nov 2023 04:53:05 -0800 Subject: [PATCH] fix(dts): ensure chunks conform to bundle format (#1034) --- src/rollup.ts | 1 + test/index.test.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/rollup.ts b/src/rollup.ts index 8691a3883..a7dd07801 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -165,6 +165,7 @@ const getRollupConfig = async ( banner: dtsOptions.banner, footer: dtsOptions.footer, entryFileNames: `[name]${outputExtension}`, + chunkFileNames: `[name]-[hash]${outputExtension}`, plugins: [ format === 'cjs' && options.cjsInterop && fixCjsExport, ].filter(Boolean), diff --git a/test/index.test.ts b/test/index.test.ts index db07db345..5f84bde35 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1398,6 +1398,93 @@ test('should emit a declaration file per format (type: module)', async () => { ]) }) +test('should emit dts chunks per format', async () => { + const { outFiles } = await run( + getTestName(), + { + 'src/input1.ts': ` + import type { InternalType } from './shared.js' + + export function getValue(value: InternalType) { + return value; + } + `, + 'src/input2.ts': ` + import type { InternalType } from './shared.js' + + export function getValue(value: InternalType) { + return value; + } + `, + 'src/shared.ts': `export type InternalType = 'foo'`, + 'tsup.config.ts': ` + export default { + entry: ['./src/input1.ts', './src/input2.ts'], + format: ['esm', 'cjs'], + dts: true + }`, + }, + { entry: [] } + ) + expect(outFiles).toEqual([ + 'input1.d.mts', + 'input1.d.ts', + 'input1.js', + 'input1.mjs', + 'input2.d.mts', + 'input2.d.ts', + 'input2.js', + 'input2.mjs', + 'shared-qBqaX8Tr.d.mts', + 'shared-qBqaX8Tr.d.ts', + ]) +}) + +test('should emit dts chunks per format (type: module)', async () => { + const { outFiles } = await run( + getTestName(), + { + 'src/input1.ts': ` + import type { InternalType } from './shared.js' + + export function getValue(value: InternalType) { + return value; + } + `, + 'src/input2.ts': ` + import type { InternalType } from './shared.js' + + export function getValue(value: InternalType) { + return value; + } + `, + 'src/shared.ts': `export type InternalType = 'foo'`, + 'tsup.config.ts': ` + export default { + entry: ['./src/input1.ts', './src/input2.ts'], + format: ['esm', 'cjs'], + dts: true + }`, + 'package.json': `{ + "type": "module" + }`, + }, + { entry: [] } + ) + expect(outFiles).toEqual([ + 'input1.cjs', + 'input1.d.cts', + 'input1.d.ts', + 'input1.js', + 'input2.cjs', + 'input2.d.cts', + 'input2.d.ts', + 'input2.js', + 'shared-qBqaX8Tr.d.cts', + 'shared-qBqaX8Tr.d.ts', + ]) +}) + test('should emit declaration files with experimentalDts', async () => { const files = { 'package.json': `