A good chunk splitting strategy is important for improving application loading performance. It can leverage browser caching to reduce requests and improve loading speed.
Several chunk splitting strategies are built into Rsbuild to meet the needs of most applications. You can also customize the chunk splitting configuration for specific use cases.
See Rspack - Code Splitting for more details.
The chunk splitting configuration of Rsbuild is in performance.chunkSplit.
Rsbuild supports the following chunk splitting strategies:
split-by-experience
: an empirical splitting strategy, automatically splits some commonly used npm packages into chunks of moderate size.split-by-module
: split by NPM package granularity, each NPM package corresponds to a chunk.split-by-size
: automatically split according to module size.all-in-one
: bundle all codes into one chunk.single-vendor
: bundle all NPM packages into a single chunk.custom
: custom chunk splitting strategy.
When using strategies other than all-in-one
, Rspack's default splitting rules will also take effect. For more details, please refer to Rspack - SplitChunksPlugin.
Rsbuild uses the split-by-experience
strategy by default, an optimization strategy based on practical experience. When the following npm packages are used in your application, they are automatically split into separate chunks:
lib-polyfill.js
: Contains core-js
, @swc/helpers
, tslib
lib-axios.js
: Contains axios
lib-react.js
: Provided by @rsbuild/plugin-reactlib-vue.js
: Provided by @rsbuild/plugin-vueThis approach groups commonly used packages and splits them into individual chunks, which helps improve browser caching efficiency.
If the npm packages mentioned above are not installed or used, the corresponding chunks will not be generated.
Split each npm package into a separate chunk.
This strategy splits node_modules with the finest granularity. Under HTTP/2, multiplexing can speed up resource loading. However, in non-HTTP/2 environments, use this strategy with caution due to HTTP head-of-line blocking.
This strategy puts all source code and third-party dependencies in a single chunk.
To also bundle dynamically imported chunks into a single file, set the output.asyncChunks option in Rspack to false
:
This strategy puts third-party dependencies in one chunk and source code in another.
The single vendor file may be very large, potentially reducing page loading performance.
With this strategy, after setting minSize
and maxSize
to fixed values, Rsbuild will automatically split chunks without extra configuration.
In addition to built-in strategies, you can also customize the splitting strategy for more specific needs. Custom strategies have two parts:
splitChunks
configNote that these custom capabilities can be used together with built-in strategies; you can use built-in strategies to split common packages and custom functions to split other packages.
Rsbuild supports custom groups, which are more flexible than built-in strategies and simpler than writing Rspack's splitChunks
config.
For example, split the axios
library under node_modules into axios.js
:
Through forceSplitting
config, you can easily split packages into chunks.
Chunks split using forceSplitting
are inserted into the HTML file as initial resources using <script>
tags. Split them appropriately based on your scenario to avoid excessive initial bundle size.
In addition to custom grouping, you can also customize Rspack's splitChunks
config through override
. For example:
minSize
to 30,000 so modules smaller than 30,000 bytes will not be split.styles.css
.The override
config will be merged with Rspack's splitChunks
config. For specific config details, please refer to Rspack - splitChunks.
In addition to the chunkSplit
configuration, using dynamic import is also an important optimization technique that can effectively reduce initial bundle size.
Dynamic import is a feature introduced in ECMAScript 2020 that allows you to dynamically load JavaScript modules. Rspack supports dynamic import by default, so you can use it directly in your code.
When the bundler encounters import()
syntax, it automatically splits the relevant code into a new chunk and loads it on-demand at runtime.
For example, if your project has a large module called bigModule.ts
(which can also be a third-party dependency), you can use dynamic import to load it on-demand:
When you run the build command, bigModule.ts
will be automatically split into a new chunk and loaded on-demand at runtime.