|
2 | 2 | <img alt="Rslib Banner" src="https://assets.rspack.dev/rslib/rslib-banner.png">
|
3 | 3 | </picture>
|
4 | 4 |
|
5 |
| -# Rslib |
| 5 | +# rsbuild-plugin-dts |
6 | 6 |
|
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. |
8 | 8 |
|
9 |
| -## Documentation |
| 9 | +## Using in Rslib |
10 | 10 |
|
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 | +``` |
12 | 165 |
|
13 | 166 | ## Contributing
|
14 | 167 |
|
15 | 168 | Please read the [Contributing Guide](https://github.com/web-infra-dev/rslib/blob/main/CONTRIBUTING.md).
|
16 | 169 |
|
17 | 170 | ## License
|
18 | 171 |
|
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). |
0 commit comments