diff --git a/src/api-extractor.ts b/src/api-extractor.ts index 618615bf4..df4b40607 100644 --- a/src/api-extractor.ts +++ b/src/api-extractor.ts @@ -9,6 +9,7 @@ import { getApiExtractor, removeFiles, toAbsolutePath, + writeFileSync, } from './utils' import type { ExperimentalDtsConfig, @@ -22,6 +23,7 @@ import type { IExtractorConfigPrepareOptions, } from '@microsoft/api-extractor' import type { InputOption } from 'rollup' +import { formatAggregationExports, formatDistributionExports, type ExportDeclaration } from "./exports" const logger = createLogger() @@ -86,13 +88,7 @@ function rollupDtsFile( async function rollupDtsFiles( options: NormalizedOptions, - exports: { - /** - * **Source file name** to **Output file name** mapping. - * (`src/index.ts` \=> `.tsup/declaration/index.d.ts`) - */ - fileMapping: Map - }, + exports: ExportDeclaration[], format: Format, ) { if (!options.experimentalDts || !options.experimentalDts?.entry) { @@ -108,6 +104,32 @@ async function rollupDtsFiles( const dtsExtension = defaultOutExtension({ format, pkgType: pkg.type }).dts const tsconfig = options.tsconfig || 'tsconfig.json' + let dtsInputFilePath = path.join( + declarationDir, + `_tsup-dts-aggregation${dtsExtension}`, + ) + // @microsoft/api-extractor doesn't support `.d.mts` and `.d.cts` file as a + // entrypoint yet. So we replace the extension here as a temporary workaround. + // + // See the issue for more details: + // https://github.com/microsoft/rushstack/pull/4196 + dtsInputFilePath = dtsInputFilePath + .replace(/\.d\.mts$/, '.dmts.d.ts') + .replace(/\.d\.cts$/, '.dcts.d.ts') + + const dtsOutputFilePath = path.join(outDir, `_tsup-dts-rollup${dtsExtension}`) + + writeFileSync( + dtsInputFilePath, + formatAggregationExports(exports, declarationDir), + ) + + rollupDtsFile( + dtsInputFilePath, + dtsOutputFilePath, + tsconfig, + ) + for (let [out, sourceFileName] of Object.entries( options.experimentalDts.entry, )) { @@ -142,14 +164,15 @@ async function rollupDtsFiles( */ const outFileName = path.join(outDir, out + dtsExtension) - /** - * **Input file path** (`.tsup/declaration/index.d.ts`) - */ - const inputFilePath = - exports.fileMapping.get(sourceFileName) || - `${path.join(declarationDir, out)}.d.ts` + // Find all declarations that are exported from the current source file + const currentExports = exports.filter( + (declaration) => declaration.sourceFileName === sourceFileName, + ) - rollupDtsFile(inputFilePath, outFileName, tsconfig) + writeFileSync( + outFileName, + formatDistributionExports(currentExports, outFileName, dtsOutputFilePath), + ) } } @@ -161,13 +184,7 @@ async function cleanDtsFiles(options: NormalizedOptions) { export async function runDtsRollup( options: NormalizedOptions, - exports?: { - /** - * **Source file name** to **Output file name** mapping. - * (`src/index.ts` \=> `.tsup/declaration/index.d.ts`) - */ - fileMapping: Map - }, + exports?: ExportDeclaration[], ) { try { const start = Date.now() diff --git a/src/tsc.ts b/src/tsc.ts index 921d2b8b1..3b1aa387d 100644 --- a/src/tsc.ts +++ b/src/tsc.ts @@ -198,13 +198,7 @@ function emit(compilerOptions?: any, tsconfig?: string) { ) const fileMapping = emitDtsFiles(program, host) - return { - /** - * **Source file name** to **Output file name** mapping. - * (`src/index.ts` \=> `.tsup/declaration/index.d.ts`) - */ - fileMapping, - } + return getExports(program, fileMapping) } export function runTypeScriptCompiler(options: NormalizedOptions) {