Skip to content

Commit 17f7c6e

Browse files
authoredNov 19, 2024
docs: DTS (#448)
1 parent 5732401 commit 17f7c6e

File tree

15 files changed

+427
-20
lines changed

15 files changed

+427
-20
lines changed
 

‎packages/plugin-dts/README.md

Lines changed: 158 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,171 @@
22
<img alt="Rslib Banner" src="https://assets.rspack.dev/rslib/rslib-banner.png">
33
</picture>
44

5-
# Rslib
5+
# rsbuild-plugin-dts
66

7-
Rslib is a library development tool powered by [Rsbuild](https://rsbuild.dev). It allows library developers to leverage the knowledge and ecosystem of webpack and Rspack.
7+
An [Rsbuild plugin](https://www.npmjs.com/package/rsbuild-plugin-dts) to emit declaration files for TypeScript which is built-in in Rslib.
88

9-
## Documentation
9+
## Using in Rslib
1010

11-
https://lib.rsbuild.dev/
11+
Read [DTS](https://lib.rsbuild.dev/guide/advanced/dts) and [lib.dts](https://lib.rsbuild.dev/config/lib/dts) for more details.
12+
13+
## Using in Rsbuild
14+
15+
Install:
16+
17+
```bash
18+
npm add rsbuild-plugin-dts -D
19+
```
20+
21+
Add plugin to `rsbuild.config.ts`:
22+
23+
```ts
24+
// rsbuild.config.ts
25+
import { pluginDts } from 'rsbuild-plugin-dts';
26+
27+
export default {
28+
plugins: [pluginDts()],
29+
};
30+
```
31+
32+
## Options
33+
34+
### bundle
35+
36+
- **Type:** `boolean`
37+
- **Default:** `false`
38+
39+
Whether to bundle the DTS files.
40+
41+
If you want to [bundle DTS](https://lib.rsbuild.dev/guide/advanced/dts#bundle-dts) files, you should:
42+
43+
1. Install `@microsoft/api-extractor` as a development dependency, which is the underlying tool used for bundling DTS files.
44+
45+
```bash
46+
npm add @microsoft/api-extractor -D
47+
```
48+
49+
2. Set `bundle` to `true`.
50+
51+
```js
52+
pluginDts({
53+
bundle: true,
54+
});
55+
```
56+
57+
### distPath
58+
59+
- **Type:** `string`
60+
61+
The output directory of DTS files. The default value follows the priority below:
62+
63+
1. The `distPath` value of the plugin options.
64+
2. The `declarationDir` value in the `tsconfig.json` file.
65+
3. The [output.distPath.root](https://rsbuild.dev/config/output/dist-path) value of Rsbuild configuration.
66+
67+
```js
68+
pluginDts({
69+
distPath: './dist-types',
70+
});
71+
```
72+
73+
### build
74+
75+
- **Type:** `boolean`
76+
- **Default:** `false`
77+
78+
Determines whether to generate DTS files while 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.
79+
80+
When this option is enabled, you must explicitly set `declarationDir` or `outDir` in `tsconfig.json` in order to meet the build requirements.
81+
82+
### abortOnError
83+
84+
- **Type:** `boolean`
85+
- **Default:** `true`
86+
87+
Whether to abort the build process when an error occurs during DTS generation.
88+
89+
By default, type errors will cause the build to fail.
90+
91+
When `abortOnError` is set to `false`, the build will still succeed even if there are type issues in the code.
92+
93+
```js
94+
pluginDts({
95+
abortOnError: false,
96+
});
97+
```
98+
99+
### dtsExtension
100+
101+
- **Type:** `string`
102+
- **Default:** `'.d.ts'`
103+
104+
The extension of the DTS file.
105+
106+
```js
107+
pluginDts({
108+
dtsExtension: '.d.mts',
109+
});
110+
```
111+
112+
### autoExternal
113+
114+
- **Type:** `boolean`
115+
- **Default:** `true`
116+
117+
Whether to automatically externalize dependencies of different dependency types and do not bundle them into the DTS file.
118+
119+
The default value of `autoExternal` is `true`, which means the following dependency types will not be bundled:
120+
121+
- `dependencies`
122+
- `optionalDependencies`
123+
- `peerDependencies`
124+
125+
And the following dependency types will be bundled:
126+
127+
- `devDependencies`
128+
129+
```js
130+
pluginDts({
131+
autoExternal: {
132+
dependencies: true,
133+
optionalDependencies: true,
134+
peerDependencies: true,
135+
devDependencies: false,
136+
},
137+
});
138+
```
139+
140+
### banner
141+
142+
- **Type:** `string`
143+
- **Default:** `undefined`
144+
145+
Inject content into the top of each DTS file.
146+
147+
```js
148+
pluginDts({
149+
banner: '/** @banner */',
150+
});
151+
```
152+
153+
### footer
154+
155+
- **Type:** `string`
156+
- **Default:** `undefined`
157+
158+
Inject content into the bottom of each DTS file.
159+
160+
```js
161+
pluginDts({
162+
footer: '/** @footer */',
163+
});
164+
```
12165

13166
## Contributing
14167

15168
Please read the [Contributing Guide](https://github.com/web-infra-dev/rslib/blob/main/CONTRIBUTING.md).
16169

17170
## License
18171

19-
Rslib is [MIT licensed](https://github.com/web-infra-dev/rslib/blob/main/LICENSE).
172+
[MIT licensed](https://github.com/web-infra-dev/rslib/blob/main/LICENSE).

‎packages/plugin-dts/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts';
5050
// use ts compiler API to generate bundleless dts
5151
// use ts compiler API and api-extractor to generate dts bundle
5252
// TODO: deal alias in dts
53-
export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
53+
export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
5454
name: PLUGIN_DTS_NAME,
5555

5656
setup(api) {

‎website/docs/en/_meta.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,5 @@
88
"text": "Config",
99
"link": "/config/",
1010
"activeMatch": "/config/"
11-
},
12-
{
13-
"text": "API",
14-
"link": "/api/javascript-api/core",
15-
"activeMatch": "/api/"
1611
}
1712
]

‎website/docs/en/config/lib/auto-external.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
overviewHeaders: [2, 3]
3+
---
4+
15
# lib.autoExternal
26

37
- **Type:**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
overviewHeaders: [2, 3]
3+
---
4+
15
# lib.banner
26

37
- **Type:**

0 commit comments

Comments
 (0)