-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathapi-extractor.ts
168 lines (149 loc) · 4.54 KB
/
api-extractor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type {
ExtractorResult,
IConfigFile,
IExtractorConfigPrepareOptions,
} from '@microsoft/api-extractor'
import path from 'path'
import { handleError } from './errors'
import {
formatAggregationExports,
formatDistributionExports,
type ExportDeclaration,
} from './exports'
import { loadPkg } from './load'
import { createLogger } from './log'
import { Format, NormalizedOptions } from './options'
import {
defaultOutExtension,
ensureTempDeclarationDir,
getApiExtractor,
removeFiles,
toAbsolutePath,
writeFileSync,
} from './utils'
const logger = createLogger()
function rollupDtsFile(
inputFilePath: string,
outputFilePath: string,
tsconfigFilePath: string
) {
let cwd = process.cwd()
let packageJsonFullPath = path.join(cwd, 'package.json')
let configObject: IConfigFile = {
mainEntryPointFilePath: inputFilePath,
apiReport: {
enabled: false,
// `reportFileName` is not been used. It's just to fit the requirement of API Extractor.
reportFileName: 'tsup-report.api.md',
},
docModel: { enabled: false },
dtsRollup: {
enabled: true,
untrimmedFilePath: outputFilePath,
},
tsdocMetadata: { enabled: false },
compiler: {
tsconfigFilePath: tsconfigFilePath,
},
projectFolder: cwd,
}
const prepareOptions: IExtractorConfigPrepareOptions = {
configObject,
configObjectFullPath: undefined,
packageJsonFullPath,
}
const imported = getApiExtractor()
if (!imported) {
throw new Error(
`@microsoft/api-extractor is not installed. Please install it first.`
)
}
const { ExtractorConfig, Extractor } = imported
const extractorConfig = ExtractorConfig.prepare(prepareOptions)
// Invoke API Extractor
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
// Equivalent to the "--local" command-line parameter
localBuild: true,
// Equivalent to the "--verbose" command-line parameter
showVerboseMessages: true,
})
if (!extractorResult.succeeded) {
throw new Error(
`API Extractor completed with ${extractorResult.errorCount} errors and ${extractorResult.warningCount} warnings when processing ${inputFilePath}`
)
}
}
async function rollupDtsFiles(
options: NormalizedOptions,
exports: ExportDeclaration[],
format: Format
) {
let declarationDir = ensureTempDeclarationDir()
let outDir = options.outDir || 'dist'
let pkg = await loadPkg(process.cwd())
let dtsExtension = defaultOutExtension({ format, pkgType: pkg.type }).dts
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')
let dtsOutputFilePath = path.join(outDir, '_tsup-dts-rollup' + dtsExtension)
writeFileSync(
dtsInputFilePath,
formatAggregationExports(exports, declarationDir)
)
rollupDtsFile(
dtsInputFilePath,
dtsOutputFilePath,
options.tsconfig || 'tsconfig.json'
)
for (let [out, sourceFileName] of Object.entries(
options.experimentalDts!.entry
)) {
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(currentExports, outFileName, dtsOutputFilePath)
)
}
}
function cleanDtsFiles(options: NormalizedOptions) {
if (options.clean) {
removeFiles(['**/*.d.{ts,mts,cts}'], options.outDir)
}
}
export async function runDtsRollup(
options: NormalizedOptions,
exports?: ExportDeclaration[]
) {
try {
const start = Date.now()
const getDuration = () => {
return `${Math.floor(Date.now() - start)}ms`
}
logger.info('dts', 'Build start')
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)
}
logger.success('dts', `⚡️ Build success in ${getDuration()}`)
} catch (error) {
handleError(error)
logger.error('dts', 'Build error')
}
}