diff --git a/packages/plugin-dts/src/apiExtractor.ts b/packages/plugin-dts/src/apiExtractor.ts index bb1e459a2..7d08b548c 100644 --- a/packages/plugin-dts/src/apiExtractor.ts +++ b/packages/plugin-dts/src/apiExtractor.ts @@ -24,9 +24,12 @@ export async function bundleDts(options: BundleOptions): Promise { try { apiExtractor = await import('@microsoft/api-extractor'); } catch { - throw new Error( + const error = new Error( `${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://rslib.rs/guide/advanced/dts#how-to-generate-declaration-files-in-rslib for more details.`, ); + // do not log the stack trace, it is not helpful for users + error.stack = ''; + throw error; } const { Extractor, ExtractorConfig, ExtractorLogLevel } = apiExtractor!; @@ -103,6 +106,9 @@ export async function bundleDts(options: BundleOptions): Promise { }), ); } catch (e) { - throw new Error(`${logPrefixApiExtractor} ${e}`); + const error = new Error(`${logPrefixApiExtractor} ${e}`); + // do not log the stack trace, it is not helpful for users + error.stack = ''; + throw error; } } diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 3fd5d6be0..57a4402e1 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -117,10 +117,12 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ ); if (!tsconfigPath) { - logger.error( + const error = new Error( `Failed to resolve tsconfig file ${color.cyan(`"${config.source.tsconfigPath}"`)} from ${color.cyan(cwd)}. Please ensure that the file exists.`, ); - throw new Error(); + error.stack = ''; + // do not log the stack trace, it is not helpful for users + throw error; } const tsConfigResult = loadTsconfig(tsconfigPath); @@ -215,7 +217,10 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ for (const result of promisesResult) { if (result.status === 'error') { if (options.abortOnError) { - throw new Error(result.errorMessage); + const error = new Error(result.errorMessage); + // do not log the stack trace, it is not helpful for users + error.stack = ''; + throw error; } result.errorMessage && logger.error(result.errorMessage); logger.warn( diff --git a/packages/plugin-dts/src/tsc.ts b/packages/plugin-dts/src/tsc.ts index c4fa46dac..f4f9719e1 100644 --- a/packages/plugin-dts/src/tsc.ts +++ b/packages/plugin-dts/src/tsc.ts @@ -63,9 +63,12 @@ async function handleDiagnosticsAndProcessFiles( logger.error(logPrefixTsc, message); } - throw new Error( + const error = new Error( `Failed to generate declaration files. ${color.gray(`(${name})`)}`, ); + // do not log the stack trace, diagnostic messages are enough + error.stack = ''; + throw error; } } @@ -318,9 +321,12 @@ export async function emitDts( ); if (errorNumber > 0) { - throw new Error( + const error = new Error( `Failed to generate declaration files. ${color.gray(`(${name})`)}`, ); + // do not log the stack trace, diagnostic messages are enough + error.stack = ''; + throw error; } } diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 6ba178d84..a9982be36 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -450,17 +450,23 @@ export function processSourceEntry( ); if (entries.length === 0) { - throw new Error( + const noValidEntryError = new Error( `Can not find a valid entry for ${color.cyan('dts.bundle')} option, please check your entry config.`, ); + // do not log the stack trace, it is not helpful for users + noValidEntryError.stack = ''; + throw noValidEntryError; } return entries; } - throw new Error( + const error = new Error( '@microsoft/api-extractor only support entry of Record type to bundle declaration files, please check your entry config.', ); + // do not log the stack trace, it is not helpful for users + error.stack = ''; + throw error; } // same as @rslib/core, we should extract into a single published package to share diff --git a/tests/integration/dts/index.test.ts b/tests/integration/dts/index.test.ts index a16295b52..ad291959f 100644 --- a/tests/integration/dts/index.test.ts +++ b/tests/integration/dts/index.test.ts @@ -159,19 +159,13 @@ describe('dts when bundle: false', () => { const fixturePath = join(__dirname, 'bundle-false', 'tsconfig-path'); await createTempFiles(fixturePath, false); - const { logs, restore } = proxyConsole(); try { await buildAndGetResults({ fixturePath, type: 'dts' }); } catch (err: any) { - expect( - logs - .map((log) => stripAnsi(log)) - .find((log) => log.includes('Failed to resolve tsconfig file')), - ).toMatchInlineSnapshot( - `"error Failed to resolve tsconfig file "/tests/integration/dts/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from /tests/integration/dts/bundle-false/tsconfig-path. Please ensure that the file exists."`, + expect(stripAnsi(err.message)).toMatchInlineSnapshot( + `"Failed to resolve tsconfig file "/tests/integration/dts/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from /tests/integration/dts/bundle-false/tsconfig-path. Please ensure that the file exists."`, ); } - restore(); }); });