diff --git a/website/docs/en/guide/faq/features.mdx b/website/docs/en/guide/faq/features.mdx index fd456060f..ea99c7af9 100644 --- a/website/docs/en/guide/faq/features.mdx +++ b/website/docs/en/guide/faq/features.mdx @@ -92,3 +92,24 @@ export default { }, }; ``` + +## d.ts generation + +### How to additionally exclude specified dependencies when `dts.bundle` is `true`? + +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. + +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. + +In this scenario, you can configure [output.externals](/config/rsbuild/output#outputtarget) to exclude @types/react. + +```ts title="rslib.config.ts" +export default { + lib: [ + // ... + ], + output: { + externals: ['@types/react'], + }, +}; +``` diff --git a/website/docs/zh/guide/faq/features.mdx b/website/docs/zh/guide/faq/features.mdx index e71a4195f..cdce1d73d 100644 --- a/website/docs/zh/guide/faq/features.mdx +++ b/website/docs/zh/guide/faq/features.mdx @@ -92,3 +92,24 @@ export default { }, }; ``` + +## d.ts 生成 + +### 如何在 `dts.bundle` 为 `true` 时额外排除指定的依赖? + +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 文件中排除指定的依赖。 + +举个例子:常见的 React 组件库通常不会将 @types/react 声明在 peerDependencies 中,而是仅声明在 devDependencies,按照 [autoExternal](/config/lib/auto-external) 处理依赖的逻辑,在打包时 Rslib 会尝试将 @types/react 一同打包进 d.ts 产物中,但在实践中组件库并不应该打包 @types/react。 + +此时可以通过配置 [output.externals](/config/rsbuild/output#outputtarget) 来排除 @types/react。 + +```ts title="rslib.config.ts" +export default { + lib: [ + // ... + ], + output: { + externals: ['@types/react'], + }, +}; +```