Skip to content

Commit d213f43

Browse files
docs(QA): add how to external module in DTS (#706)
Co-authored-by: Timeless0911 <[email protected]>
1 parent f033669 commit d213f43

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

website/docs/en/guide/faq/features.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,24 @@ export default {
9292
},
9393
};
9494
```
95+
96+
## d.ts generation
97+
98+
### How to additionally exclude specified dependencies when `dts.bundle` is `true`?
99+
100+
Rslib uses [rsbuild-plugin-dts](https://github.com/web-infra-dev/rslib/blob/main/packages/plugin-dts/README.md) to generate d.ts files, which supports configuration via [output.externals](/config/rsbuild/output#outputtarget) for excluding certain dependencies from bundled d.ts files.
101+
102+
For example, a typical React component library often does not declare @types/react in peerDependencies but only in devDependencies. Following the [autoExternal](/config/lib/auto-external) logic for dependency handling, Rslib will attempt to bundle @types/react into the d.ts artifact during the build. However, in practice, a component library should not bundle @types/react.
103+
104+
In this scenario, you can configure [output.externals](/config/rsbuild/output#outputtarget) to exclude @types/react.
105+
106+
```ts title="rslib.config.ts"
107+
export default {
108+
lib: [
109+
// ...
110+
],
111+
output: {
112+
externals: ['@types/react'],
113+
},
114+
};
115+
```

website/docs/zh/guide/faq/features.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,24 @@ export default {
9292
},
9393
};
9494
```
95+
96+
## d.ts 生成
97+
98+
### 如何在 `dts.bundle``true` 时额外排除指定的依赖?
99+
100+
Rslib 通过 [rsbuild-plugin-dts](https://github.com/web-infra-dev/rslib/blob/main/packages/plugin-dts/README.md) 完成对 d.ts 文件的生成,该插件支持通过 [output.externals](/config/rsbuild/output#outputtarget) 进行配置,用于从打包后的 d.ts 文件中排除指定的依赖。
101+
102+
举个例子:常见的 React 组件库通常不会将 @types/react 声明在 peerDependencies 中,而是仅声明在 devDependencies,按照 [autoExternal](/config/lib/auto-external) 处理依赖的逻辑,在打包时 Rslib 会尝试将 @types/react 一同打包进 d.ts 产物中,但在实践中组件库并不应该打包 @types/react
103+
104+
此时可以通过配置 [output.externals](/config/rsbuild/output#outputtarget) 来排除 @types/react
105+
106+
```ts title="rslib.config.ts"
107+
export default {
108+
lib: [
109+
// ...
110+
],
111+
output: {
112+
externals: ['@types/react'],
113+
},
114+
};
115+
```

0 commit comments

Comments
 (0)