Skip to content

Commit c18a3be

Browse files
authored
feat: default exclude @types/react from dts bundle (#1365)
1 parent 85d4b54 commit c18a3be

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

packages/plugin-dts/src/dts.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
const isObject = (obj: unknown): obj is Record<string, any> =>
2121
Object.prototype.toString.call(obj) === '[object Object]';
2222

23+
export const DEFAULT_EXCLUDED_PACKAGES: string[] = ['@types/react'];
24+
2325
// use !externals
2426
export const calcBundledPackages = (options: {
2527
cwd: string;
@@ -110,7 +112,11 @@ export const calcBundledPackages = (options: {
110112
!externals.some((e) => (typeof e === 'string' ? d === e : e.test(d))),
111113
);
112114

113-
return Array.from(new Set(bundledPackages));
115+
const filteredBundledPackages = Array.from(new Set(bundledPackages)).filter(
116+
(pkg) => !DEFAULT_EXCLUDED_PACKAGES.includes(pkg),
117+
);
118+
119+
return filteredBundledPackages;
114120
};
115121

116122
export async function generateDts(data: DtsGenOptions): Promise<void> {

packages/plugin-dts/tests/external.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import fs from 'node:fs';
22
import { logger } from '@rsbuild/core';
33
import { describe, expect, it, rs } from '@rstest/core';
4-
import { calcBundledPackages } from '../src/dts';
4+
import { calcBundledPackages, DEFAULT_EXCLUDED_PACKAGES } from '../src/dts';
5+
6+
const defaultExcludePackages = DEFAULT_EXCLUDED_PACKAGES.reduce(
7+
(acc: Record<string, string>, cur: string) => {
8+
acc[cur] = '1.0.0';
9+
return acc;
10+
},
11+
{},
12+
);
513

614
const commonPkgJson = {
715
dependencies: {
@@ -13,6 +21,7 @@ const commonPkgJson = {
1321
devDependencies: {
1422
baz: '1.0.0',
1523
bar: '1.0.0',
24+
...defaultExcludePackages,
1625
},
1726
};
1827

@@ -65,6 +74,7 @@ describe('should calcBundledPackages correctly', () => {
6574
baz: '1.0.0',
6675
bar: '1.0.0',
6776
react: '1.0.0',
77+
...defaultExcludePackages,
6878
},
6979
}),
7080
);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,23 @@ export default {
199199

200200
### How to additionally exclude specified dependencies when `dts.bundle` is `true`?
201201

202-
Rslib uses [rsbuild-plugin-dts](https://github.com/web-infra-dev/rslib/blob/main/packages/plugin-dts/README.md) to generate declaration files, which supports configuration via [output.externals](/config/rsbuild/output#outputtarget) for excluding certain dependencies from bundled declaration files.
202+
Rslib uses [rsbuild-plugin-dts](https://github.com/web-infra-dev/rslib/blob/main/packages/plugin-dts/README.md) to generate declaration files, which supports configuration via [output.externals](/config/rsbuild/output#outputexternals) for excluding certain dependencies from bundled declaration files.
203203

204-
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 declaration output files during the build. However, in practice, a component library should not bundle `@types/react`.
205-
206-
In this scenario, you can configure [output.externals](/config/rsbuild/output#outputtarget) to exclude `@types/react`.
204+
For example, if `@types/foo` is only declared in `devDependencies`, according to the dependency handling logic of [autoExternal](/config/lib/auto-external), Rslib will try to bundle `@types/foo` into the declaration output files during the build. In this case, you can exclude `@types/foo` by configuring [output.externals](/config/rsbuild/output#outputexternals).
207205

208206
```ts title="rslib.config.ts"
209207
export default {
210208
lib: [
211209
// ...
212210
],
213211
output: {
214-
externals: ['@types/react'],
212+
externals: ['@types/foo'],
215213
},
216214
};
217215
```
218216

217+
In addition, if you only want to specify a few dependencies to be bundled into the declaration output files, you can configure [dts.bundle.bundledPackages](/config/lib/dts#dtsbundlebundledpackages) to achieve this. All other dependencies not in this configuration will be excluded.
218+
219219
## Rsbuild plugin
220220

221221
### Why does using `modifyRsbuildConfig` to modify the configuration does not take effect?

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,23 @@ export default {
199199

200200
### 如何在 `dts.bundle``true` 时额外排除指定的依赖?
201201

202-
Rslib 通过 [rsbuild-plugin-dts](https://github.com/web-infra-dev/rslib/blob/main/packages/plugin-dts/README.md) 完成对类型声明文件的生成,该插件支持通过 [output.externals](/config/rsbuild/output#outputtarget) 进行配置,用于从打包后的类型声明文件中排除指定的依赖
202+
Rslib 通过 [rsbuild-plugin-dts](https://github.com/web-infra-dev/rslib/blob/main/packages/plugin-dts/README.md) 生成类型声明文件,该插件支持通过 [output.externals](/config/rsbuild/output#outputexternals) 配置,从打包后的类型声明文件中排除指定的依赖
203203

204-
举个例子:常见的 React 组件库通常不会将 `@types/react` 声明在 `peerDependencies` 中,而是仅声明在 `devDependencies`,按照 [autoExternal](/config/lib/auto-external) 处理依赖的逻辑,在打包时 Rslib 会尝试将 `@types/react` 一同打包进类型声明文件产物中,但在实践中组件库并不应该打包 `@types/react`
205-
206-
此时可以通过配置 [output.externals](/config/rsbuild/output#outputtarget) 来排除 `@types/react`
204+
举个例子,若 `@types/foo` 仅声明在 `devDependencies` 中,按照 [autoExternal](/config/lib/auto-external) 处理依赖的逻辑,在打包时 Rslib 会尝试将 `@types/foo` 一同打包进类型声明文件产物中,此时可以通过配置 [output.externals](/config/rsbuild/output#outputexternals) 来排除 `@types/foo`
207205

208206
```ts title="rslib.config.ts"
209207
export default {
210208
lib: [
211209
// ...
212210
],
213211
output: {
214-
externals: ['@types/react'],
212+
externals: ['@types/foo'],
215213
},
216214
};
217215
```
218216

217+
此外,如果只想指定某几个依赖项打包进类型声明文件产物中,可以通过配置 [dts.bundle.bundledPackages](/config/lib/dts#dtsbundlebundledpackages) 来实现,不在该配置中的所有其他依赖项都会被排除。
218+
219219
## Rsbuild 插件
220220

221221
### 为什么使用 `modifyRsbuildConfig` 修改配置不生效?

0 commit comments

Comments
 (0)