Skip to content

Commit 8f5929c

Browse files
committedJul 30, 2023
feat: support override the config of api-extractor
1 parent 284c77f commit 8f5929c

File tree

7 files changed

+75
-13
lines changed

7 files changed

+75
-13
lines changed
 

‎README.md

+17
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,19 @@ This is an existing [TypeScript issue](https://github.com/microsoft/TypeScript/i
8080

8181
```ts
8282
import type ts from 'typescript'
83+
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
8384
import type { LogLevel } from 'vite'
8485

8586
type MaybePromise<T> = T | Promise<T>
8687

88+
export type RollupConfig = Omit<
89+
IExtractorConfigPrepareOptions['configObject'],
90+
| 'projectFolder'
91+
| 'mainEntryPointFilePath'
92+
| 'compiler'
93+
| 'dtsRollup'
94+
>
95+
8796
export interface Resolver {
8897
/**
8998
* The name of the resolver
@@ -251,6 +260,14 @@ export interface PluginOptions {
251260
*/
252261
bundledPackages?: string[],
253262

263+
/**
264+
* Override the config of `@microsoft/api-extractor`
265+
*
266+
* @default null
267+
* @see https://api-extractor.com/pages/setup/configure_api_report/
268+
*/
269+
rollupConfig?: RollupConfig,
270+
254271
/**
255272
* Whether to copy .d.ts source files to `outDir`
256273
*

‎README.zh-CN.md

+17
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,19 @@ export default defineConfig({
8080

8181
```ts
8282
import type ts from 'typescript'
83+
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
8384
import type { LogLevel } from 'vite'
8485

8586
type MaybePromise<T> = T | Promise<T>
8687

88+
export type RollupConfig = Omit<
89+
IExtractorConfigPrepareOptions['configObject'],
90+
| 'projectFolder'
91+
| 'mainEntryPointFilePath'
92+
| 'compiler'
93+
| 'dtsRollup'
94+
>
95+
8796
export interface Resolver {
8897
/**
8998
* 解析器的名称
@@ -251,6 +260,14 @@ export interface PluginOptions {
251260
*/
252261
bundledPackages?: string[],
253262

263+
/**
264+
* 覆写 `@microsoft/api-extractor` 的配置
265+
*
266+
* @default null
267+
* @see https://api-extractor.com/pages/setup/configure_api_report/
268+
*/
269+
rollupConfig?: RollupConfig,
270+
254271
/**
255272
* 是否将源码里的 .d.ts 文件复制到 `outDir`
256273
*

‎examples/vue/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ types/
22
dist/
33
node_modules/
44
components/*.d.ts
5+
docs/

‎examples/vue/vite.config.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ export default defineConfig({
3838
// include: ['src/index.ts'],
3939
exclude: ['src/ignore'],
4040
staticImport: true,
41-
// rollupTypes: true,
42-
insertTypesEntry: true,
41+
rollupTypes: true,
42+
// insertTypesEntry: true,
4343
compilerOptions: {
4444
declarationMap: true
45+
},
46+
rollupConfig: {
47+
docModel: {
48+
enabled: true,
49+
apiJsonFilePath: '<projectFolder>/docs/<unscopedPackageName>.api.json'
50+
}
4551
}
4652
}),
4753
vue(),

‎src/plugin.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
7373
cleanVueFileName = false,
7474
insertTypesEntry = false,
7575
rollupTypes = false,
76-
bundledPackages = [],
7776
pathsToAliases = true,
7877
aliasesExclude = [],
7978
copyDtsFiles = false,
@@ -116,6 +115,9 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
116115
const rootFiles = new Set<string>()
117116
const outputFiles = new Map<string, string>()
118117

118+
const rollupConfig = { ...(options.rollupConfig || {}) }
119+
rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || []
120+
119121
return {
120122
name: 'vite:dts',
121123

@@ -606,7 +608,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
606608
entryPath: path,
607609
fileName: basename(path),
608610
libFolder,
609-
bundledPackages
611+
rollupConfig
610612
})
611613

612614
emittedFiles.delete(path)
@@ -621,7 +623,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
621623
entryPath: typesPath,
622624
fileName: basename(typesPath),
623625
libFolder,
624-
bundledPackages
626+
rollupConfig
625627
})
626628

627629
emittedFiles.delete(typesPath)

‎src/rollup.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { resolve } from 'node:path'
33
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor'
44
import { tryGetPkgPath } from './utils'
55

6-
import type { ExtractorLogLevel } from '@microsoft/api-extractor'
76
import type ts from 'typescript'
7+
import type { ExtractorLogLevel } from '@microsoft/api-extractor'
8+
import type { RollupConfig } from './types'
89

910
export interface BundleOptions {
1011
root: string,
@@ -14,7 +15,7 @@ export interface BundleOptions {
1415
entryPath: string,
1516
fileName: string,
1617
libFolder?: string,
17-
bundledPackages?: string[]
18+
rollupConfig?: RollupConfig
1819
}
1920

2021
const dtsRE = /\.d\.tsx?$/
@@ -27,7 +28,7 @@ export function rollupDeclarationFiles({
2728
entryPath,
2829
fileName,
2930
libFolder,
30-
bundledPackages
31+
rollupConfig = {}
3132
}: BundleOptions) {
3233
const configObjectFullPath = resolve(root, 'api-extractor.json')
3334

@@ -37,9 +38,9 @@ export function rollupDeclarationFiles({
3738

3839
const extractorConfig = ExtractorConfig.prepare({
3940
configObject: {
41+
...rollupConfig,
4042
projectFolder: root,
4143
mainEntryPointFilePath: entryPath,
42-
bundledPackages,
4344
compiler: {
4445
tsconfigFilePath: configPath,
4546
overrideTsconfig: {
@@ -49,17 +50,20 @@ export function rollupDeclarationFiles({
4950
},
5051
apiReport: {
5152
enabled: false,
52-
reportFileName: '<unscopedPackageName>.api.md'
53+
reportFileName: '<unscopedPackageName>.api.md',
54+
...rollupConfig.apiReport
5355
},
5456
docModel: {
55-
enabled: false
57+
enabled: false,
58+
...rollupConfig.docModel
5659
},
5760
dtsRollup: {
5861
enabled: true,
5962
publicTrimmedFilePath: resolve(outDir, fileName)
6063
},
6164
tsdocMetadata: {
62-
enabled: false
65+
enabled: false,
66+
...rollupConfig.tsdocMetadata
6367
},
6468
messages: {
6569
compilerMessageReporting: {
@@ -71,7 +75,8 @@ export function rollupDeclarationFiles({
7175
default: {
7276
logLevel: 'none' as ExtractorLogLevel.None
7377
}
74-
}
78+
},
79+
...rollupConfig.messages
7580
}
7681
},
7782
configObjectFullPath,

‎src/types.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import type ts from 'typescript'
2+
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
23
import type { LogLevel } from 'vite'
34

45
type MaybePromise<T> = T | Promise<T>
56

7+
export type RollupConfig = Omit<
8+
IExtractorConfigPrepareOptions['configObject'],
9+
'projectFolder' | 'mainEntryPointFilePath' | 'compiler' | 'dtsRollup'
10+
>
11+
612
export interface Resolver {
713
/**
814
* The name of the resolver
@@ -170,6 +176,14 @@ export interface PluginOptions {
170176
*/
171177
bundledPackages?: string[],
172178

179+
/**
180+
* Override the config of `@microsoft/api-extractor`
181+
*
182+
* @default null
183+
* @see https://api-extractor.com/pages/setup/configure_api_report/
184+
*/
185+
rollupConfig?: RollupConfig,
186+
173187
/**
174188
* Whether to copy .d.ts source files to `outDir`
175189
*

0 commit comments

Comments
 (0)
Please sign in to comment.