Skip to content

Commit

Permalink
squash! Remove intermediate _tsup-dts-rollup file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Oct 22, 2024
1 parent 17de754 commit 62c9ecd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
59 changes: 38 additions & 21 deletions src/api-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getApiExtractor,
removeFiles,
toAbsolutePath,
writeFileSync,
} from './utils'
import type {
ExperimentalDtsConfig,
Expand All @@ -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()

Expand Down Expand Up @@ -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<string, string>
},
exports: ExportDeclaration[],
format: Format,
) {
if (!options.experimentalDts || !options.experimentalDts?.entry) {
Expand All @@ -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,
)) {
Expand Down Expand Up @@ -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),
)
}
}

Expand All @@ -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<string, string>
},
exports?: ExportDeclaration[],
) {
try {
const start = Date.now()
Expand Down
8 changes: 1 addition & 7 deletions src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 62c9ecd

Please sign in to comment.