diff --git a/README.md b/README.md index 7213c79..bdc76f3 100644 --- a/README.md +++ b/README.md @@ -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 | Promise +export type RollupConfig = Omit< + IExtractorConfigPrepareOptions['configObject'], + | 'projectFolder' + | 'mainEntryPointFilePath' + | 'compiler' + | 'dtsRollup' + > + export interface Resolver { /** * The name of the resolver @@ -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` * diff --git a/README.zh-CN.md b/README.zh-CN.md index cca63be..7907438 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -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 | Promise +export type RollupConfig = Omit< + IExtractorConfigPrepareOptions['configObject'], + | 'projectFolder' + | 'mainEntryPointFilePath' + | 'compiler' + | 'dtsRollup' + > + export interface Resolver { /** * 解析器的名称 @@ -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` * diff --git a/examples/vue/.gitignore b/examples/vue/.gitignore index f153131..6ef0f5b 100644 --- a/examples/vue/.gitignore +++ b/examples/vue/.gitignore @@ -2,3 +2,4 @@ types/ dist/ node_modules/ components/*.d.ts +docs/ diff --git a/examples/vue/vite.config.ts b/examples/vue/vite.config.ts index 03ed92e..1f17618 100644 --- a/examples/vue/vite.config.ts +++ b/examples/vue/vite.config.ts @@ -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: '/docs/.api.json' + } } }), vue(), diff --git a/src/plugin.ts b/src/plugin.ts index 896784d..49b1ac1 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -73,7 +73,6 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { cleanVueFileName = false, insertTypesEntry = false, rollupTypes = false, - bundledPackages = [], pathsToAliases = true, aliasesExclude = [], copyDtsFiles = false, @@ -116,6 +115,9 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { const rootFiles = new Set() const outputFiles = new Map() + const rollupConfig = { ...(options.rollupConfig || {}) } + rollupConfig.bundledPackages = rollupConfig.bundledPackages || options.bundledPackages || [] + return { name: 'vite:dts', @@ -606,7 +608,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { entryPath: path, fileName: basename(path), libFolder, - bundledPackages + rollupConfig }) emittedFiles.delete(path) @@ -621,7 +623,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { entryPath: typesPath, fileName: basename(typesPath), libFolder, - bundledPackages + rollupConfig }) emittedFiles.delete(typesPath) diff --git a/src/rollup.ts b/src/rollup.ts index 54bb0a7..2966ab5 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -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, @@ -14,7 +15,7 @@ export interface BundleOptions { entryPath: string, fileName: string, libFolder?: string, - bundledPackages?: string[] + rollupConfig?: RollupConfig } const dtsRE = /\.d\.tsx?$/ @@ -27,7 +28,7 @@ export function rollupDeclarationFiles({ entryPath, fileName, libFolder, - bundledPackages + rollupConfig = {} }: BundleOptions) { const configObjectFullPath = resolve(root, 'api-extractor.json') @@ -37,9 +38,9 @@ export function rollupDeclarationFiles({ const extractorConfig = ExtractorConfig.prepare({ configObject: { + ...rollupConfig, projectFolder: root, mainEntryPointFilePath: entryPath, - bundledPackages, compiler: { tsconfigFilePath: configPath, overrideTsconfig: { @@ -49,17 +50,20 @@ export function rollupDeclarationFiles({ }, apiReport: { enabled: false, - reportFileName: '.api.md' + reportFileName: '.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: { @@ -71,7 +75,8 @@ export function rollupDeclarationFiles({ default: { logLevel: 'none' as ExtractorLogLevel.None } - } + }, + ...rollupConfig.messages } }, configObjectFullPath, diff --git a/src/types.ts b/src/types.ts index 3b8a10b..4789302 100644 --- a/src/types.ts +++ b/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 | Promise +export type RollupConfig = Omit< + IExtractorConfigPrepareOptions['configObject'], + 'projectFolder' | 'mainEntryPointFilePath' | 'compiler' | 'dtsRollup' +> + export interface Resolver { /** * The name of the resolver @@ -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` *