Skip to content

Commit aab0d1b

Browse files
committed
feat: supports specify real config file for bundling types
1 parent e305f33 commit aab0d1b

6 files changed

Lines changed: 53 additions & 14 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ type MaybePromise<T> = T | Promise<T>
304304

305305
export type BundleConfig = Omit<
306306
IExtractorConfigPrepareOptions['configObject'],
307-
'projectFolder' | 'mainEntryPointFilePath' | 'bundledPackages'
307+
'extends' | 'projectFolder' | 'mainEntryPointFilePath' | 'bundledPackages'
308308
>
309309

310310
export interface ResolverTransformOutput {
@@ -502,7 +502,15 @@ export interface PluginOptions {
502502
* @default {}
503503
* @see https://api-extractor.com/pages/setup/invoking/#invoking-from-a-build-script
504504
*/
505-
invokeOptions?: IExtractorInvokeOptions
505+
invokeOptions?: IExtractorInvokeOptions,
506+
/**
507+
* Specify a real api-extractor config file path.
508+
*
509+
* When invoking, the configuration will be merged in the order: internal config, file config, `extractorConfig`.
510+
*
511+
* @default './api-extractor.json'
512+
*/
513+
configPath?: string
506514
},
507515

508516
/**

README.zh-CN.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ type MaybePromise<T> = T | Promise<T>
321321

322322
export type BundleConfig = Omit<
323323
IExtractorConfigPrepareOptions['configObject'],
324-
'projectFolder' | 'mainEntryPointFilePath' | 'bundledPackages'
324+
'extends' | 'projectFolder' | 'mainEntryPointFilePath' | 'bundledPackages'
325325
>
326326

327327
export interface ResolverTransformOutput {
@@ -519,7 +519,15 @@ export interface PluginOptions {
519519
* @default {}
520520
* @see https://api-extractor.com/pages/setup/invoking/#invoking-from-a-build-script
521521
*/
522-
invokeOptions?: IExtractorInvokeOptions
522+
invokeOptions?: IExtractorInvokeOptions,
523+
/**
524+
* 指定一个真实的 api-extractor 配置文件路径
525+
*
526+
* 调用时将会按照内部配置、文件配置、`extractorConfig` 的顺序合并
527+
*
528+
* @default './api-extractor.json'
529+
*/
530+
configPath?: string
523531
},
524532

525533
/**

packages/unplugin-dts/src/core/bundle.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { mergeObjects, resolve, tryGetPackageInfo, tryGetPkgPath } from './utils'
1+
import { ensureAbsolute, mergeObjects, resolve, tryGetPackageInfo, tryGetPkgPath } from './utils'
22

3-
import type { ExtractorLogLevel, IExtractorInvokeOptions } from '@microsoft/api-extractor'
3+
import type { ExtractorLogLevel, IConfigFile, IExtractorInvokeOptions } from '@microsoft/api-extractor'
44
import type { BundleConfig } from './types'
55

66
export interface BundleDtsOptions {
77
root: string,
88
configPath?: string,
9+
tsconfigPath?: string,
910
compilerOptions: Record<string, any>,
1011
outDir: string,
1112
entryPath: string,
@@ -27,6 +28,7 @@ const dtsRE = /\.d\.(m|c)?tsx?$/
2728
export async function bundleDtsFiles({
2829
root,
2930
configPath,
31+
tsconfigPath,
3032
compilerOptions,
3133
outDir,
3234
entryPath,
@@ -38,7 +40,8 @@ export async function bundleDtsFiles({
3840
}: BundleDtsOptions) {
3941
const { Extractor, ExtractorConfig } = await import('@microsoft/api-extractor')
4042

41-
const configObjectFullPath = resolve(root, 'api-extractor.json')
43+
configPath = configPath ? ensureAbsolute(configPath, root) : ''
44+
const configObjectFullPath = configPath || resolve(root, 'api-extractor.json')
4245

4346
if (!dtsRE.test(fileName)) {
4447
fileName += '.d.ts'
@@ -49,12 +52,12 @@ export async function bundleDtsFiles({
4952
compilerOptions = { ...compilerOptions, module: 'ESNext' }
5053
}
5154

52-
const configObject = mergeObjects({
55+
const configObject: IConfigFile = {
5356
bundledPackages,
5457
projectFolder: root,
5558
mainEntryPointFilePath: entryPath,
5659
compiler: {
57-
tsconfigFilePath: configPath,
60+
tsconfigFilePath: tsconfigPath,
5861
overrideTsconfig: {
5962
$schema: 'http://json.schemastore.org/tsconfig',
6063
compilerOptions,
@@ -86,7 +89,15 @@ export async function bundleDtsFiles({
8689
},
8790
},
8891
},
89-
}, extractorConfig)
92+
}
93+
94+
if (configPath) {
95+
mergeObjects(configObject, ExtractorConfig.loadFile(configPath))
96+
}
97+
98+
if (Object.keys(extractorConfig).length) {
99+
mergeObjects(configObject, extractorConfig)
100+
}
90101

91102
const config = ExtractorConfig.prepare({
92103
configObject,

packages/unplugin-dts/src/core/runtime.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ export class Runtime {
457457
extractorConfig,
458458
bundledPackages,
459459
invokeOptions,
460+
configPath: bundleConfigPath,
460461
} = isNativeObj(bundleTypes) ? bundleTypes : {}
461462

462463
const cleanPath = (path: string, emittedFiles: Map<string, string>) => {
@@ -675,7 +676,10 @@ export class Runtime {
675676
const rollup = async (path: string) => {
676677
const result = await bundleDtsFiles({
677678
root,
678-
configPath,
679+
// api-extractor.json
680+
configPath: bundleConfigPath,
681+
// tsconfig.json
682+
tsconfigPath: configPath,
679683
compilerOptions,
680684
outDir,
681685
entryPath: path,

packages/unplugin-dts/src/core/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface Logger {
1515

1616
export type BundleConfig = Omit<
1717
IExtractorConfigPrepareOptions['configObject'],
18-
'projectFolder' | 'mainEntryPointFilePath' | 'bundledPackages'
18+
'extends' | 'projectFolder' | 'mainEntryPointFilePath' | 'bundledPackages'
1919
>
2020

2121
export type AliasOptions = {
@@ -183,7 +183,15 @@ export interface EmitOptions {
183183
* @default {}
184184
* @see https://api-extractor.com/pages/setup/invoking/#invoking-from-a-build-script
185185
*/
186-
invokeOptions?: IExtractorInvokeOptions
186+
invokeOptions?: IExtractorInvokeOptions,
187+
/**
188+
* Specify a real api-extractor config file path.
189+
*
190+
* When invoking, the configuration will be merged in the order: internal config, file config, `extractorConfig`.
191+
*
192+
* @default './api-extractor.json'
193+
*/
194+
configPath?: string
187195
},
188196
/**
189197
* Hook called prior to writing each declaration file.

packages/unplugin-dts/src/core/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function unwrapPromise<T>(maybePromise: T | Promise<T>) {
8585
return isPromise(maybePromise) ? await maybePromise : maybePromise
8686
}
8787

88-
export function mergeObjects<T extends Record<string, unknown>, U extends Record<string, unknown>>(
88+
export function mergeObjects<T extends Record<string, any>, U extends Record<string, any>>(
8989
sourceObj: T,
9090
targetObj: U,
9191
) {

0 commit comments

Comments
 (0)