Skip to content

Commit 9520ea5

Browse files
authored
fix(dts): explicit error for @microsoft/api-extractor is required (#930)
1 parent 7ee1151 commit 9520ea5

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { join, relative } from 'node:path';
2-
import {
3-
Extractor,
4-
ExtractorConfig,
5-
ExtractorLogLevel,
6-
type ExtractorResult,
7-
} from '@microsoft/api-extractor';
2+
import type * as ApiExtractor from '@microsoft/api-extractor';
83
import { logger } from '@rsbuild/core';
94
import color from 'picocolors';
105
import type { DtsEntry } from './index';
@@ -25,6 +20,16 @@ export type BundleOptions = {
2520
};
2621

2722
export async function bundleDts(options: BundleOptions): Promise<void> {
23+
let apiExtractor: typeof ApiExtractor | undefined = undefined;
24+
try {
25+
apiExtractor = await import('@microsoft/api-extractor');
26+
} catch {
27+
throw new Error(
28+
`${color.cyan('@microsoft/api-extractor')} is required when ${color.cyan('dts.bundle')} is set to ${color.cyan('true')}, please make sure it is installed. You could check https://lib.rsbuild.dev/guide/advanced/dts#how-to-generate-declaration-files-in-rslib for more details.`,
29+
);
30+
}
31+
32+
const { Extractor, ExtractorConfig, ExtractorLogLevel } = apiExtractor!;
2833
const {
2934
name,
3035
cwd,
@@ -65,7 +70,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
6570
packageJsonFullPath: join(cwd, 'package.json'),
6671
});
6772

68-
const extractorResult: ExtractorResult = Extractor.invoke(
73+
const extractorResult: ApiExtractor.ExtractorResult = Extractor.invoke(
6974
extractorConfig,
7075
{
7176
localBuild: true,

0 commit comments

Comments
 (0)