Lazy barrel
Lazy barrel is a stable optimization feature in Rspack that improves build performance by skipping the building of unused re-export modules in side-effect-free barrel files.
What is a barrel file
A barrel file is a module that re-exports functionality from other modules, commonly used to create a cleaner public API for a package or directory:
This allows consumers to import from a single entry point:
However, barrel files can cause performance issues because bundlers traditionally need to build all re-exported modules, even if only a few are actually used.
How lazy barrel works
When lazy barrel optimization is enabled (which it is by default), Rspack will skip building unused re-export modules in side-effect-free barrel files until they're actually needed.
Example
With lazy barrel optimization:
- ✅ Only
Button.jsis built - ✅
Card.js,Modal.js, and other unused modules are not built - ✅ Faster build times, especially in large projects
Without lazy barrel optimization:
- ❌ All modules (
Button.js,Card.js,Modal.js, etc.) would be built - ❌ Slower build times, even though most modules are unused
Requirements
For lazy barrel optimization to work, barrel files must be side-effect-free. A module is considered side-effect-free when it meets one of the following conditions:
-
Package-level declaration: The
package.jsonfile declares"sideEffects": falsepackage.jsonSee Side effects analysis for more details.
-
Module-level declaration: Modules explicitly marked as side-effect-free through
rules[].sideEffectsrspack.config.mjs
Supported export patterns
Lazy barrel optimization works with the following export patterns:
Named re-exports
Namespace re-exports
Named exports from local declarations
Default exports
Star re-exports (export *)
Star re-exports (export * from "./module") are not fully optimized by lazy barrel and remain a performance concern.
When lazy barrel can still optimize
Lazy barrel will skip building star re-exports only when:
- The barrel file is side-effect-free, and
- The imported specifier is found in the barrel's named exports
Example scenario where optimization works:
In this case:
- ✅
API_VERSIONis a named export in the barrel file itself - ✅ Rspack can optimize this—no modules are built (
Button.js,Card.js,Modal.jsare all skipped)
Example scenario where optimization fails:
In this case:
- ❌
Buttonis not a named export in the barrel file—it's fromexport * from './Button' - ❌ Rspack must build
./Button.jsand potentially all other star re-exports to find which module exportsButton
FAQ
1. Does lazy barrel support CommonJS?
Currently, lazy barrel only supports ES modules (ESM). CommonJS support would require improvements to Rspack's CommonJS tree shaking, particularly the static analysis capabilities. Support for CommonJS may be added in a future release.
2. Can Rspack automatically detect side-effect-free modules?
Rspack can analyze whether a module has side effects (this capability is already used by optimization.sideEffects for tree shaking). However, this analysis requires checking the module's dependencies recursively—a module is only truly side-effect-free when all its dependencies are also side-effect-free.
During the make phase, dependencies must be built before their side effects can be analyzed. Lazy barrel is specifically designed to avoid building those dependencies. Therefore, it relies on explicit markers like "sideEffects": false in package.json or rules[].sideEffects, which don't require dependency checking since they declare the entire package or matched modules as side-effect-free.
Configuration
Lazy barrel is enabled by default since Rspack 1.6.0. No configuration is needed.
If you're using an older version and have experiments.lazyBarrel in your configuration, you can safely remove it:
The experiments.lazyBarrel configuration option has been deprecated and will be removed in Rspack v2.0.

