Skip to content

Commit 34fe99b

Browse files
authored
chore(plugin-dts): remove error stack which is not helpful to users (#1056)
1 parent 40564e0 commit 34fe99b

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
2424
try {
2525
apiExtractor = await import('@microsoft/api-extractor');
2626
} catch {
27-
throw new Error(
27+
const error = new Error(
2828
`${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.`,
2929
);
30+
// do not log the stack trace, it is not helpful for users
31+
error.stack = '';
32+
throw error;
3033
}
3134

3235
const { Extractor, ExtractorConfig, ExtractorLogLevel } = apiExtractor!;
@@ -103,6 +106,9 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
103106
}),
104107
);
105108
} catch (e) {
106-
throw new Error(`${logPrefixApiExtractor} ${e}`);
109+
const error = new Error(`${logPrefixApiExtractor} ${e}`);
110+
// do not log the stack trace, it is not helpful for users
111+
error.stack = '';
112+
throw error;
107113
}
108114
}

packages/plugin-dts/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
117117
);
118118

119119
if (!tsconfigPath) {
120-
logger.error(
120+
const error = new Error(
121121
`Failed to resolve tsconfig file ${color.cyan(`"${config.source.tsconfigPath}"`)} from ${color.cyan(cwd)}. Please ensure that the file exists.`,
122122
);
123-
throw new Error();
123+
error.stack = '';
124+
// do not log the stack trace, it is not helpful for users
125+
throw error;
124126
}
125127

126128
const tsConfigResult = loadTsconfig(tsconfigPath);
@@ -215,7 +217,10 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
215217
for (const result of promisesResult) {
216218
if (result.status === 'error') {
217219
if (options.abortOnError) {
218-
throw new Error(result.errorMessage);
220+
const error = new Error(result.errorMessage);
221+
// do not log the stack trace, it is not helpful for users
222+
error.stack = '';
223+
throw error;
219224
}
220225
result.errorMessage && logger.error(result.errorMessage);
221226
logger.warn(

packages/plugin-dts/src/tsc.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ async function handleDiagnosticsAndProcessFiles(
6363
logger.error(logPrefixTsc, message);
6464
}
6565

66-
throw new Error(
66+
const error = new Error(
6767
`Failed to generate declaration files. ${color.gray(`(${name})`)}`,
6868
);
69+
// do not log the stack trace, diagnostic messages are enough
70+
error.stack = '';
71+
throw error;
6972
}
7073
}
7174

@@ -318,9 +321,12 @@ export async function emitDts(
318321
);
319322

320323
if (errorNumber > 0) {
321-
throw new Error(
324+
const error = new Error(
322325
`Failed to generate declaration files. ${color.gray(`(${name})`)}`,
323326
);
327+
// do not log the stack trace, diagnostic messages are enough
328+
error.stack = '';
329+
throw error;
324330
}
325331
}
326332

packages/plugin-dts/src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,23 @@ export function processSourceEntry(
450450
);
451451

452452
if (entries.length === 0) {
453-
throw new Error(
453+
const noValidEntryError = new Error(
454454
`Can not find a valid entry for ${color.cyan('dts.bundle')} option, please check your entry config.`,
455455
);
456+
// do not log the stack trace, it is not helpful for users
457+
noValidEntryError.stack = '';
458+
throw noValidEntryError;
456459
}
457460

458461
return entries;
459462
}
460463

461-
throw new Error(
464+
const error = new Error(
462465
'@microsoft/api-extractor only support entry of Record<string, string> type to bundle declaration files, please check your entry config.',
463466
);
467+
// do not log the stack trace, it is not helpful for users
468+
error.stack = '';
469+
throw error;
464470
}
465471

466472
// same as @rslib/core, we should extract into a single published package to share

tests/integration/dts/index.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,13 @@ describe('dts when bundle: false', () => {
159159
const fixturePath = join(__dirname, 'bundle-false', 'tsconfig-path');
160160
await createTempFiles(fixturePath, false);
161161

162-
const { logs, restore } = proxyConsole();
163162
try {
164163
await buildAndGetResults({ fixturePath, type: 'dts' });
165164
} catch (err: any) {
166-
expect(
167-
logs
168-
.map((log) => stripAnsi(log))
169-
.find((log) => log.includes('Failed to resolve tsconfig file')),
170-
).toMatchInlineSnapshot(
171-
`"error Failed to resolve tsconfig file "<ROOT>/tests/integration/dts/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from <ROOT>/tests/integration/dts/bundle-false/tsconfig-path. Please ensure that the file exists."`,
165+
expect(stripAnsi(err.message)).toMatchInlineSnapshot(
166+
`"Failed to resolve tsconfig file "<ROOT>/tests/integration/dts/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from <ROOT>/tests/integration/dts/bundle-false/tsconfig-path. Please ensure that the file exists."`,
172167
);
173168
}
174-
restore();
175169
});
176170
});
177171

0 commit comments

Comments
 (0)