You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/en/config/output/externals.mdx
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
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.'
3
3
---
4
4
5
5
# output.externals
@@ -17,26 +17,35 @@ type Externals =
17
17
18
18
-**Default:**`undefined`
19
19
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.
21
21
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.
23
27
24
28
## Examples
25
29
26
30
### Basic usage
27
31
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:
29
33
30
34
```ts title="rsbuild.config.ts"
31
35
exportdefault {
32
36
output: {
33
37
externals: {
34
38
'react-dom': 'ReactDOM',
39
+
'react-dom/client': 'ReactDOM',
35
40
},
36
41
},
37
42
};
38
43
```
39
44
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
+
40
49
### Array format
41
50
42
51
Use an array to define multiple external configurations:
@@ -88,7 +97,7 @@ Then you can use the external libraries in your source code:
0 commit comments