Skip to content

Commit 1270675

Browse files
authored
docs: clarify output.externals usage (#7497)
1 parent 6739057 commit 1270675

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

packages/core/src/types/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,8 +1308,8 @@ export interface OutputConfig {
13081308
*/
13091309
target?: RsbuildTarget;
13101310
/**
1311-
* At build time, prevent some `import` dependencies from being packed into bundles in
1312-
* your code, and instead fetch them externally at runtime.
1311+
* Use this option to specify which modules should not be bundled by Rsbuild, and instead
1312+
* use implementations provided by the external environment.
13131313
* For more information, please see: [Rspack Externals](https://rspack.rs/config/externals)
13141314
* @default undefined
13151315
*/

website/docs/en/config/output/externals.mdx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: 'At build time, prevent some import dependencies from being packed into bundles in your code, and instead fetch them externally at runtime.'
2+
description: 'Use output.externals to specify which modules should not be bundled by Rsbuild, and instead use implementations provided by the external environment.'
33
---
44

55
# output.externals
@@ -17,26 +17,35 @@ type Externals =
1717

1818
- **Default:** `undefined`
1919

20-
At build time, prevent some `import` dependencies from being packed into bundles in your code, and instead fetch them externally at runtime.
20+
Use this option to specify which modules should not be bundled by Rsbuild, and instead use implementations provided by the external environment.
2121

22-
> For more information, please see: [Rspack Externals](https://rspack.rs/config/externals).
22+
For example, if your page already loads React from a CDN, or if you're building a library and want consumers to install `react` themselves, you can declare it as an external. This reduces bundle size and avoids including the same dependency twice.
23+
24+
This is commonly used in library development, and is also useful in app scenarios such as loading dependencies from a CDN or relying on dependencies injected by the host environment.
25+
26+
> For more details, see the Rspack [Externals](https://rspack.rs/config/externals) documentation.
2327
2428
## Examples
2529

2630
### Basic usage
2731

28-
Exclude the `react-dom` dependency from the output bundle. To get this module at runtime, the value of `react-dom` will globally retrieve the `ReactDOM` variable.
32+
For example, you can exclude `react-dom` from the output bundle and access the module at runtime through the global `ReactDOM` variable:
2933

3034
```ts title="rsbuild.config.ts"
3135
export default {
3236
output: {
3337
externals: {
3438
'react-dom': 'ReactDOM',
39+
'react-dom/client': 'ReactDOM',
3540
},
3641
},
3742
};
3843
```
3944

45+
Note that string matching for module names is exact. That means you need to explicitly declare subpath imports such as `react-dom/client`.
46+
47+
If you need to match a group of similar import patterns, use [regular expressions](#regular-expressions) or a function for more flexible matching logic.
48+
4049
### Array format
4150

4251
Use an array to define multiple external configurations:
@@ -88,7 +97,7 @@ Then you can use the external libraries in your source code:
8897
const response = await window.axios.get('/api/users');
8998
```
9099

91-
### RegExp patterns
100+
### Regular expressions
92101

93102
Use regular expressions to match multiple modules with a pattern:
94103

website/docs/zh/config/output/externals.mdx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: '在构建时,防止将代码中某些 import 的依赖包打包到 bundle 中,而是在运行时再去从外部获取这些依赖'
2+
description: 'output.externals 用于指定哪些模块不需要被 Rsbuild 打包,而是直接使用外部环境中提供的实现'
33
---
44

55
# output.externals
@@ -17,26 +17,35 @@ type Externals =
1717

1818
- **默认值:** `undefined`
1919

20-
在构建时,防止将代码中某些 `import` 的依赖包打包到 bundle 中,而是在运行时再去从外部获取这些依赖
20+
用于指定哪些模块不需要被 Rsbuild 打包,而是直接使用外部环境中提供的实现
2121

22-
> 更多用法供参考:[Rspack Externals](https://rspack.rs/zh/config/externals)
22+
例如,当页面已经通过 CDN 引入了 React,或你开发的库希望由使用方自行安装 `react` 时,可以将其声明为 external。这可以减少打包产物的体积,同时避免重复引入相同依赖。
23+
24+
该能力常用于库开发场景,同时在应用侧接入 CDN、使用宿主环境注入依赖等场景中也同样适用。
25+
26+
> 更多用法请参考 Rspack 的 [Externals](https://rspack.rs/zh/config/externals) 文档。
2327
2428
## 示例
2529

2630
### 基础用法
2731

28-
`react-dom` 依赖从构建产物中剔除。为了在运行时获取这个模块, `react-dom` 的值将全局检索 `ReactDOM` 变量。
32+
例如,`react-dom` 从构建产物中排除,并在运行时通过全局变量 `ReactDOM` 来获取该模块:
2933

3034
```ts title="rsbuild.config.ts"
3135
export default {
3236
output: {
3337
externals: {
3438
'react-dom': 'ReactDOM',
39+
'react-dom/client': 'ReactDOM',
3540
},
3641
},
3742
};
3843
```
3944

45+
值得注意的是,当在 `externals` 中以字符串形式指定模块名时,匹配规则为精确匹配。因此需要显式声明 `react-dom/client` 等子路径导入。
46+
47+
如果你需要匹配一组相似的导入形式,可以改用[正则表达式](#正则表达式),或者使用函数来实现更灵活的匹配逻辑。
48+
4049
### 数组格式
4150

4251
使用数组来定义多个 `externals` 配置:
@@ -88,7 +97,7 @@ export default {
8897
const response = await window.axios.get('/api/users');
8998
```
9099

91-
### 正则表达式模式
100+
### 正则表达式
92101

93102
使用正则表达式来匹配具有特定模式的多个模块:
94103

0 commit comments

Comments
 (0)