output.externals
- Type:
- Default:
undefined
Use this option to specify which modules should not be bundled by Rsbuild, and instead use implementations provided by the external environment.
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.
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.
For more details, see the Rspack Externals documentation.
Examples
Basic usage
For example, you can exclude react-dom from the output bundle and access the module at runtime through the global ReactDOM variable:
Note that string matching for module names is exact. That means you need to explicitly declare subpath imports such as react-dom/client.
If you need to match a group of similar import patterns, use regular expressions or a function for more flexible matching logic.
Array format
Use an array to define multiple external configurations:
Using with CDN
A common use case is to load libraries from CDN and exclude them from your bundle, then use html.tags to include them in your HTML.
Then you can use the external libraries in your source code:
Regular expressions
Use regular expressions to match multiple modules with a pattern:
Web workers
When building a worker with output.target set to web-worker, external dependencies must be available in the worker itself. Workers cannot access global variables from the host page.
For module worker output (output.module is true), external dependencies are loaded through ES module imports by default. Make sure the worker runtime can resolve the external module paths.
For classic worker output (output.module: false), you can map dependencies to global variables in the worker. For example, map worker-sdk to self.WorkerSDK:
Before loading the worker bundle, load a script that defines self.WorkerSDK. For example, use a separate bootstrap script that calls importScripts() to load the dependency script first, then the worker bundle.
If you build for both web and web-worker targets, configure externals that rely on browser page globals in the environment with target: 'web'. Avoid placing them in the top-level output.externals, because that configuration also applies to workers, which cannot access the page's global variables.

