Skip to content

Commit e7a5716

Browse files
authored
refactor: optimize dts logs/docs with declaration files keyword (#817)
1 parent a260377 commit e7a5716

File tree

27 files changed

+135
-143
lines changed

27 files changed

+135
-143
lines changed

packages/core/src/types/config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,31 @@ export type Syntax =
4545
export type Dts =
4646
| {
4747
/**
48-
* Whether to bundle the DTS files.
48+
* Whether to bundle the declaration files.
4949
* @defaultValue `false`
5050
* @see {@link https://lib.rsbuild.dev/config/lib/dts#dtsbundle}
5151
*/
5252
bundle?: boolean;
5353
/**
54-
* The output directory of DTS files.
54+
* The output directory of declaration files.
5555
* @defaultValue {@link https://lib.rsbuild.dev/config/lib/dts#default-value}
5656
* @see {@link https://lib.rsbuild.dev/config/lib/dts#dtsdistpath}
5757
*/
5858
distPath?: string;
5959
/**
60-
* Whether to generate DTS files with building the project references.
60+
* Whether to generate declaration files with building the project references.
6161
* @defaultValue `false`
6262
* @see {@link https://lib.rsbuild.dev/config/lib/dts#dtsbuild}
6363
*/
6464
build?: boolean;
6565
/**
66-
* Whether to abort the build process when an error occurs during DTS generation.
66+
* Whether to abort the build process when an error occurs during declaration files generation.
6767
* @defaultValue `true`
6868
* @see {@link https://lib.rsbuild.dev/config/lib/dts#dtsabortonerror}
6969
*/
7070
abortOnError?: boolean;
7171
/**
72-
* Whether to automatically set the DTS file extension based on the {@link format} option.
72+
* Whether to automatically set the declaration file extension based on the {@link format} option.
7373
* @defaultValue `false`
7474
* @see {@link https://lib.rsbuild.dev/config/lib/dts#dtsautoextension}
7575
*/
@@ -251,13 +251,13 @@ export interface LibConfig extends EnvironmentConfig {
251251
*/
252252
externalHelpers?: boolean;
253253
/**
254-
* Inject content into the top of each JavaScript, CSS or DTS file.
254+
* Inject content into the top of each JavaScript, CSS or declaration file.
255255
* @defaultValue `{}`
256256
* @see {@link https://lib.rsbuild.dev/config/lib/banner}
257257
*/
258258
banner?: BannerAndFooter;
259259
/**
260-
* Inject content into the bottom of each JavaScript, CSS or DTS file.
260+
* Inject content into the bottom of each JavaScript, CSS or declaration file.
261261
* @defaultValue `{}`
262262
* @see {@link https://lib.rsbuild.dev/config/lib/footer}
263263
*/

packages/plugin-dts/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ An [Rsbuild plugin](https://www.npmjs.com/package/rsbuild-plugin-dts) to emit de
88

99
## Using in Rslib
1010

11-
Read [DTS](https://lib.rsbuild.dev/guide/advanced/dts) and [lib.dts](https://lib.rsbuild.dev/config/lib/dts) for more details.
11+
Read [Declaration files](https://lib.rsbuild.dev/guide/advanced/dts) and [lib.dts](https://lib.rsbuild.dev/config/lib/dts) for more details.
1212

1313
## Using in Rsbuild
1414

@@ -36,11 +36,11 @@ export default {
3636
- **Type:** `boolean`
3737
- **Default:** `false`
3838

39-
Whether to bundle the DTS files.
39+
Whether to bundle the declaration files.
4040

41-
If you want to [bundle DTS](https://lib.rsbuild.dev/guide/advanced/dts#bundle-dts) files, you should:
41+
If you want to [bundle declaration files](https://lib.rsbuild.dev/guide/advanced/dts#bundle-declaration-files) files, you should:
4242

43-
1. Install `@microsoft/api-extractor` as a development dependency, which is the underlying tool used for bundling DTS files.
43+
1. Install `@microsoft/api-extractor` as a development dependency, which is the underlying tool used for bundling declaration files.
4444

4545
```bash
4646
npm add @microsoft/api-extractor -D
@@ -58,7 +58,7 @@ pluginDts({
5858

5959
- **Type:** `string`
6060

61-
The output directory of DTS files. The default value follows the priority below:
61+
The output directory of declaration files. The default value follows the priority below:
6262

6363
1. The `distPath` value of the plugin options.
6464
2. The `declarationDir` value in the `tsconfig.json` file.
@@ -75,7 +75,7 @@ pluginDts({
7575
- **Type:** `boolean`
7676
- **Default:** `false`
7777

78-
Whether to generate DTS files with building the project references. This is equivalent to using the `--build` flag with the `tsc` command. See [Project References](https://www.typescriptlang.org/docs/handbook/project-references.html) for more details.
78+
Whether to generate declaration files with building the project references. This is equivalent to using the `--build` flag with the `tsc` command. See [Project References](https://www.typescriptlang.org/docs/handbook/project-references.html) for more details.
7979

8080
When this option is enabled, you must explicitly set `declarationDir` or `outDir` in `tsconfig.json` in order to meet the build requirements.
8181

@@ -84,7 +84,7 @@ When this option is enabled, you must explicitly set `declarationDir` or `outDir
8484
- **Type:** `boolean`
8585
- **Default:** `true`
8686

87-
Whether to abort the build process when an error occurs during DTS generation.
87+
Whether to abort the build process when an error occurs during declaration files generation.
8888

8989
By default, type errors will cause the build to fail.
9090

@@ -101,7 +101,7 @@ pluginDts({
101101
- **Type:** `string`
102102
- **Default:** `'.d.ts'`
103103

104-
The extension of the DTS file.
104+
The extension of the declaration file.
105105

106106
```js
107107
pluginDts({
@@ -114,7 +114,7 @@ pluginDts({
114114
- **Type:** `boolean`
115115
- **Default:** `true`
116116

117-
Whether to automatically externalize dependencies of different dependency types and do not bundle them into the DTS file.
117+
Whether to automatically externalize dependencies of different dependency types and do not bundle them into the declaration file.
118118

119119
The default value of `autoExternal` is `true`, which means the following dependency types will not be bundled:
120120

@@ -142,7 +142,7 @@ pluginDts({
142142
- **Type:** `string`
143143
- **Default:** `undefined`
144144

145-
Inject content into the top of each DTS file.
145+
Inject content into the top of each declaration file.
146146

147147
```js
148148
pluginDts({
@@ -155,7 +155,7 @@ pluginDts({
155155
- **Type:** `string`
156156
- **Default:** `undefined`
157157

158-
Inject content into the bottom of each DTS file.
158+
Inject content into the bottom of each declaration file.
159159

160160
```js
161161
pluginDts({
@@ -201,7 +201,7 @@ pluginDts({
201201

202202
Whether to automatically redirect the import paths of TypeScript declaration output files.
203203

204-
- When set to `true`, Rslib will redirect the import path in the DTS output file to the corresponding relative path based on the [compilerOptions.paths](https://typescriptlang.org/tsconfig#paths) configured in `tsconfig.json`.
204+
- When set to `true`, Rslib will redirect the import path in the declaration output file to the corresponding relative path based on the [compilerOptions.paths](https://typescriptlang.org/tsconfig#paths) configured in `tsconfig.json`.
205205

206206
```ts
207207
// `compilerOptions.paths` is set to `{ "@/*": ["src/*"] }`
@@ -221,7 +221,7 @@ import { foo } from '../foo'; // expected output './dist/utils/index.d.ts'
221221

222222
Whether to automatically redirect the file extension to import paths based on the TypeScript declaration output files.
223223

224-
- When set to `true`, the import paths in DTS files will be redirected to the corresponding JavaScript extension which can be resolved to corresponding DTS file. The extension of the DTS output file is related to the `dtsExtension` configuration.
224+
- When set to `true`, the import paths in declaration files will be redirected to the corresponding JavaScript extension which can be resolved to corresponding declaration file. The extension of the declaration output file is related to the `dtsExtension` configuration.
225225

226226
```ts
227227
// `dtsExtension` is set to `.d.mts`

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
7676
await addBannerAndFooter(untrimmedFilePath, banner, footer);
7777

7878
logger.info(
79-
`API Extractor bundle DTS succeeded: ${color.cyan(untrimmedFilePath)} in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
79+
`bundle declaration files succeeded: ${color.cyan(untrimmedFilePath)} in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
8080
);
8181
}),
8282
);
8383
} catch (e) {
84-
logger.error('API Extractor Error');
85-
throw new Error(`${e}`);
84+
throw new Error(`API Extractor Error:\n ${e}`);
8685
}
8786
}

packages/plugin-dts/src/dts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
126126
extension: false,
127127
},
128128
} = data;
129-
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
129+
logger.start(`generating declaration files... ${color.gray(`(${name})`)}`);
130130

131131
const { options: rawCompilerOptions, fileNames } = tsConfigResult;
132132

@@ -147,7 +147,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
147147
);
148148

149149
if (build) {
150-
// do not allow to use bundle DTS when 'build: true' since temp declarationDir should be set by user in tsconfig
150+
// do not allow to use bundle declaration files when 'build: true' since temp declarationDir should be set by user in tsconfig
151151
if (bundle) {
152152
throw Error(`Can not set "dts.bundle: true" when "dts.build: true"`);
153153
}

packages/plugin-dts/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
8989

9090
const { config } = environment;
9191

92-
// @microsoft/api-extractor only support single entry to bundle DTS
92+
// @microsoft/api-extractor only support single entry to bundle declaration files
9393
// see https://github.com/microsoft/rushstack/issues/1596#issuecomment-546790721
9494
// we support multiple entries by calling api-extractor multiple times
9595
const dtsEntry = processSourceEntry(
@@ -171,7 +171,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
171171
} else if (message === 'error') {
172172
resolve({
173173
status: 'error',
174-
errorMessage: `Error occurred in ${environment.name} DTS generation`,
174+
errorMessage: `Error occurred in ${environment.name} declaration files generation.`,
175175
});
176176
}
177177
});
@@ -188,7 +188,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
188188

189189
promisesResult = await Promise.all(dtsPromises);
190190
},
191-
// Set the order to 'pre' to ensure that when DTS files of multiple formats are generated simultaneously,
191+
// Set the order to 'pre' to ensure that when declaration files of multiple formats are generated simultaneously,
192192
// all errors are thrown together before exiting the process.
193193
order: 'pre',
194194
});
@@ -205,7 +205,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
205205
}
206206
result.errorMessage && logger.error(result.errorMessage);
207207
logger.warn(
208-
'With the `abortOnError` configuration currently turned off, type errors do not cause build failures, but they do not guarantee proper type file output.',
208+
'With `abortOnError` configuration currently disabled, type errors will not fail the build, but proper type declaration output cannot be guaranteed.',
209209
);
210210
}
211211
}

packages/plugin-dts/src/tsc.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ts from 'typescript';
44
import type { DtsRedirect } from './index';
55
import { getFileLoc, getTimeCost, processDtsFiles } from './utils';
66

7+
const logPrefix = color.dim('[tsc]');
8+
79
export type EmitDtsOptions = {
810
name: string;
911
cwd: string;
@@ -53,15 +55,13 @@ async function handleDiagnosticsAndProcessFiles(
5355
);
5456

5557
if (diagnosticMessages.length) {
56-
logger.error(
57-
`Failed to emit declaration files. ${color.gray(`(${name})`)}`,
58-
);
59-
6058
for (const message of diagnosticMessages) {
61-
logger.error(message);
59+
logger.error(logPrefix, message);
6260
}
6361

64-
throw new Error('DTS generation failed');
62+
throw new Error(
63+
`Failed to generate declaration files. ${color.gray(`(${name})`)}`,
64+
);
6565
}
6666
}
6767

@@ -110,6 +110,7 @@ export async function emitDts(
110110
const fileLoc = getFileLoc(diagnostic, configPath);
111111

112112
logger.error(
113+
logPrefix,
113114
`${fileLoc} - ${color.red('error')} ${color.gray(`TS${diagnostic.code}:`)}`,
114115
ts.flattenDiagnosticMessageText(
115116
diagnostic.messageText,
@@ -132,16 +133,16 @@ export async function emitDts(
132133
// 6031: File change detected. Starting incremental compilation...
133134
// 6032: Starting compilation in watch mode...
134135
if (diagnostic.code === 6031 || diagnostic.code === 6032) {
135-
logger.info(message);
136+
logger.info(logPrefix, message);
136137
}
137138

138139
// 6194: 0 errors or 2+ errors!
139140
if (diagnostic.code === 6194) {
140141
if (errorCount === 0 || !errorCount) {
141-
logger.info(message);
142+
logger.info(logPrefix, message);
142143
onComplete(true);
143144
} else {
144-
logger.error(message);
145+
logger.error(logPrefix, message);
145146
}
146147
await processDtsFiles(
147148
bundle,
@@ -157,7 +158,7 @@ export async function emitDts(
157158

158159
// 6193: 1 error
159160
if (diagnostic.code === 6193) {
160-
logger.error(message);
161+
logger.error(logPrefix, message);
161162
await processDtsFiles(
162163
bundle,
163164
declarationDir,
@@ -277,16 +278,14 @@ export async function emitDts(
277278
);
278279

279280
if (errorNumber > 0) {
280-
logger.error(
281-
`Failed to emit declaration files. ${color.gray(`(${name})`)}`,
281+
throw new Error(
282+
`Failed to generate declaration files. ${color.gray(`(${name})`)}`,
282283
);
283-
284-
throw new Error('DTS generation failed');
285284
}
286285
}
287286

288287
logger.ready(
289-
`DTS generated in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
288+
`declaration files generated in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
290289
);
291290
} else {
292291
// watch mode, can also deal with incremental build

packages/plugin-dts/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export async function processDtsFiles(
398398
const newFile = file.replace('.d.ts', dtsExtension);
399399
fs.renameSync(file, newFile);
400400
} catch (error) {
401-
logger.error(`Error renaming DTS file ${file}: ${error}`);
401+
logger.error(`Failed to rename declaration file ${file}: ${error}`);
402402
}
403403
}
404404
}
@@ -424,7 +424,7 @@ export function processSourceEntry(
424424
}
425425

426426
throw new Error(
427-
'@microsoft/api-extractor only support entry of Record<string, string> type to bundle DTS, please check your entry config.',
427+
'@microsoft/api-extractor only support entry of Record<string, string> type to bundle declaration files, please check your entry config.',
428428
);
429429
}
430430

tests/integration/dts/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,9 @@ describe('dts when build: true', () => {
464464
});
465465
} catch (err: any) {
466466
// not easy to proxy child process stdout
467-
expect(err.message).toBe('Error occurred in esm DTS generation');
467+
expect(err.message).toBe(
468+
'Error occurred in esm declaration files generation.',
469+
);
468470
}
469471
});
470472

website/docs/en/config/lib/banner.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Banner = {
1616

1717
- **Default:** `{}`
1818

19-
Inject content into the top of each JavaScript, CSS or DTS output file.
19+
Inject content into the top of each JavaScript, CSS or declaration output file.
2020

2121
## Object type
2222

@@ -39,7 +39,7 @@ Inject content into the top of each CSS output file.
3939
- **Type:** `string`
4040
- **Default:** `undefined`
4141

42-
Inject content into the top of each DTS output file.
42+
Inject content into the top of each declaration output file.
4343

4444
## Notice
4545

@@ -73,6 +73,6 @@ export default {
7373

7474
:::warning
7575

76-
The banner content in DTS files is handled differently from JavaScript and CSS output files. It is written directly using the file system API, so setting `BannerPlugin` will not affect it.
76+
The banner content in declaration files is handled differently from JavaScript and CSS output files. It is written directly using the file system API, so setting `BannerPlugin` will not affect it.
7777

7878
:::

website/docs/en/config/lib/bundle.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default {
7575

7676
::: note
7777

78-
If [DTS generation](/config/lib/dts) is enabled, remember to set [exclude](https://www.typescriptlang.org/tsconfig/#exclude) field in `tsconfig.json` to avoid generating TypeScript declaration files for the corresponding files.
78+
If [declaration files generation](/config/lib/dts) is enabled, remember to set [exclude](https://www.typescriptlang.org/tsconfig/#exclude) field in `tsconfig.json` to avoid generating TypeScript declaration files for the corresponding files.
7979

8080
For example, exclude test files within the `src` folder:
8181

0 commit comments

Comments
 (0)