Skip to content

Commit

Permalink
feat: support override the config of api-extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Jul 30, 2023
1 parent 284c77f commit 8f5929c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -80,10 +80,19 @@ This is an existing [TypeScript issue](https://github.com/microsoft/TypeScript/i

```ts
import type ts from 'typescript'
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
import type { LogLevel } from 'vite'

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

export type RollupConfig = Omit<
IExtractorConfigPrepareOptions['configObject'],
| 'projectFolder'
| 'mainEntryPointFilePath'
| 'compiler'
| 'dtsRollup'
>

export interface Resolver {
/**
* The name of the resolver
Expand Down Expand Up @@ -251,6 +260,14 @@ export interface PluginOptions {
*/
bundledPackages?: string[],

/**
* Override the config of `@microsoft/api-extractor`
*
* @default null
* @see https://api-extractor.com/pages/setup/configure_api_report/
*/
rollupConfig?: RollupConfig,

/**
* Whether to copy .d.ts source files to `outDir`
*
Expand Down
17 changes: 17 additions & 0 deletions README.zh-CN.md
Expand Up @@ -80,10 +80,19 @@ export default defineConfig({

```ts
import type ts from 'typescript'
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
import type { LogLevel } from 'vite'

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

export type RollupConfig = Omit<
IExtractorConfigPrepareOptions['configObject'],
| 'projectFolder'
| 'mainEntryPointFilePath'
| 'compiler'
| 'dtsRollup'
>

export interface Resolver {
/**
* 解析器的名称
Expand Down Expand Up @@ -251,6 +260,14 @@ export interface PluginOptions {
*/
bundledPackages?: string[],

/**
* 覆写 `@microsoft/api-extractor` 的配置
*
* @default null
* @see https://api-extractor.com/pages/setup/configure_api_report/
*/
rollupConfig?: RollupConfig,

/**
* 是否将源码里的 .d.ts 文件复制到 `outDir`
*
Expand Down
1 change: 1 addition & 0 deletions examples/vue/.gitignore
Expand Up @@ -2,3 +2,4 @@ types/
dist/
node_modules/
components/*.d.ts
docs/
10 changes: 8 additions & 2 deletions examples/vue/vite.config.ts
Expand Up @@ -38,10 +38,16 @@ export default defineConfig({
// include: ['src/index.ts'],
exclude: ['src/ignore'],
staticImport: true,
// rollupTypes: true,
insertTypesEntry: true,
rollupTypes: true,
// insertTypesEntry: true,
compilerOptions: {
declarationMap: true
},
rollupConfig: {
docModel: {
enabled: true,
apiJsonFilePath: '<projectFolder>/docs/<unscopedPackageName>.api.json'
}
}
}),
vue(),
Expand Down
8 changes: 5 additions & 3 deletions src/plugin.ts
Expand Up @@ -73,7 +73,6 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
cleanVueFileName = false,
insertTypesEntry = false,
rollupTypes = false,
bundledPackages = [],
pathsToAliases = true,
aliasesExclude = [],
copyDtsFiles = false,
Expand Down Expand Up @@ -116,6 +115,9 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
const rootFiles = new Set<string>()
const outputFiles = new Map<string, string>()

const rollupConfig = { ...(options.rollupConfig || {}) }
rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || []

return {
name: 'vite:dts',

Expand Down Expand Up @@ -606,7 +608,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
entryPath: path,
fileName: basename(path),
libFolder,
bundledPackages
rollupConfig
})

emittedFiles.delete(path)
Expand All @@ -621,7 +623,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
entryPath: typesPath,
fileName: basename(typesPath),
libFolder,
bundledPackages
rollupConfig
})

emittedFiles.delete(typesPath)
Expand Down
21 changes: 13 additions & 8 deletions src/rollup.ts
Expand Up @@ -3,8 +3,9 @@ import { resolve } from 'node:path'
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor'
import { tryGetPkgPath } from './utils'

import type { ExtractorLogLevel } from '@microsoft/api-extractor'
import type ts from 'typescript'
import type { ExtractorLogLevel } from '@microsoft/api-extractor'
import type { RollupConfig } from './types'

export interface BundleOptions {
root: string,
Expand All @@ -14,7 +15,7 @@ export interface BundleOptions {
entryPath: string,
fileName: string,
libFolder?: string,
bundledPackages?: string[]
rollupConfig?: RollupConfig
}

const dtsRE = /\.d\.tsx?$/
Expand All @@ -27,7 +28,7 @@ export function rollupDeclarationFiles({
entryPath,
fileName,
libFolder,
bundledPackages
rollupConfig = {}
}: BundleOptions) {
const configObjectFullPath = resolve(root, 'api-extractor.json')

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

const extractorConfig = ExtractorConfig.prepare({
configObject: {
...rollupConfig,
projectFolder: root,
mainEntryPointFilePath: entryPath,
bundledPackages,
compiler: {
tsconfigFilePath: configPath,
overrideTsconfig: {
Expand All @@ -49,17 +50,20 @@ export function rollupDeclarationFiles({
},
apiReport: {
enabled: false,
reportFileName: '<unscopedPackageName>.api.md'
reportFileName: '<unscopedPackageName>.api.md',
...rollupConfig.apiReport
},
docModel: {
enabled: false
enabled: false,
...rollupConfig.docModel
},
dtsRollup: {
enabled: true,
publicTrimmedFilePath: resolve(outDir, fileName)
},
tsdocMetadata: {
enabled: false
enabled: false,
...rollupConfig.tsdocMetadata
},
messages: {
compilerMessageReporting: {
Expand All @@ -71,7 +75,8 @@ export function rollupDeclarationFiles({
default: {
logLevel: 'none' as ExtractorLogLevel.None
}
}
},
...rollupConfig.messages
}
},
configObjectFullPath,
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
@@ -1,8 +1,14 @@
import type ts from 'typescript'
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
import type { LogLevel } from 'vite'

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

export type RollupConfig = Omit<
IExtractorConfigPrepareOptions['configObject'],
'projectFolder' | 'mainEntryPointFilePath' | 'compiler' | 'dtsRollup'
>

export interface Resolver {
/**
* The name of the resolver
Expand Down Expand Up @@ -170,6 +176,14 @@ export interface PluginOptions {
*/
bundledPackages?: string[],

/**
* Override the config of `@microsoft/api-extractor`
*
* @default null
* @see https://api-extractor.com/pages/setup/configure_api_report/
*/
rollupConfig?: RollupConfig,

/**
* Whether to copy .d.ts source files to `outDir`
*
Expand Down

0 comments on commit 8f5929c

Please sign in to comment.