treeshake
- Type:
boolean| object with the properties below - Optional
Controls tree-shaking (dead code elimination).
See the In-depth Dead Code Elimination Guide for more details.
When false, tree-shaking will be disabled. When true, it is equivalent to setting each options to the default value.
Default
trueannotations?
- Type:
boolean - Optional
Whether to respect /*@__PURE__*/ annotations and other tree-shaking hints in the code.
See related Oxc documentation for more details.
Default
truecommonjs?
- Type:
boolean - Optional
Whether to enable tree-shaking for CommonJS modules. When true, unused exports from CommonJS modules can be eliminated from the bundle, similar to ES modules. When disabled, CommonJS modules will always be included in their entirety.
This option allows rolldown to analyze exports.property assignments in CommonJS modules and remove unused exports while preserving the module's side effects.
Example
// source.js (CommonJS)
exports.used = 'This will be kept';
exports.unused = 'This will be tree-shaken away';
// main.js
import { used } from './source.js';
// With commonjs: true, only the 'used' export is included in the bundle
// With commonjs: false, both exports are includedDefault
trueinvalidImportSideEffects?
- Type:
boolean - Optional
Whether to assume that invalid import statements might have side effects.
See related Oxc documentation for more details.
Default
falsemanualPureFunctions?
- Type: readonly
string[] - Optional
Array of function names that should be considered pure (no side effects) even if they can't be automatically detected as pure.
See related Oxc documentation for more details.
Example
treeshake: {
manualPureFunctions: ['console.log', 'debug.trace']
}Default
[]moduleSideEffects?
- Type:
ModuleSideEffectsOption - Optional
Values:
true: Uses packagesideEffectsmetadata for bundled modules when available and static analysis otherwise. Rolldown generally treats external modules as having side effects.false: Marks all modules as side-effect-free.string[]: Array of module IDs to treat as having side effects. All other modules are marked as side-effect-free.'no-external': Marks external modules as side-effect-free and bundled modules as having side effects, overriding packagesideEffectsmetadata and static analysis. To mark only external modules as side-effect-free and preserve the default behavior for bundled modules, use[{ external: true, sideEffects: false }].ModuleSideEffectsRule[]: Uses thesideEffectsvalue of the first matching rule. Each rule must settestorexternal. Unmatched modules use the same behavior astrue.function: Receives(id, external)and returns whether the module has side effects. Returningundefineduses the same behavior astrue.
Important: Mark a module as side-effect-free only if your application can skip evaluating it and any dependencies reached only through it.
NOTE
Performance: Prefer ModuleSideEffectsRule[] over functions
When possible, use rule-based configuration instead of functions. Rules are processed entirely in Rust, while JavaScript functions require runtime calls between Rust and JavaScript, which can hurt CPU utilization during builds.
Functions should be a last resort: Only use the function signature when your logic cannot be expressed with patterns or simple string matching.
Rule advantages: ModuleSideEffectsRule[] provides better performance by avoiding Rust-JavaScript runtime calls, clearer intent, and easier maintenance.
Example
// Assume no modules have side effects (aggressive tree-shaking)
treeshake: {
moduleSideEffects: false
}
// Only specific modules have side effects (string array)
treeshake: {
moduleSideEffects: [
'lodash',
'react-dom',
]
}
// Use rules for pattern matching and granular control
treeshake: {
moduleSideEffects: [
{ test: /^node:/, sideEffects: true },
{ test: /\.css$/, sideEffects: true },
{ test: /some-package/, sideEffects: false, external: false },
]
}
// Custom function to determine side effects
treeshake: {
moduleSideEffects: (id, external) => {
if (external) return false; // external modules have no side effects
return id.includes('/side-effects/') || id.endsWith('.css');
}
}
// Assume no external modules have side effects
treeshake: {
moduleSideEffects: 'no-external',
}Common Use Cases:
- CSS files:
{ test: /\.css$/, sideEffects: true }- preserve CSS imports - Polyfills: Add specific polyfill modules to the array
- Plugins: Modules that register themselves globally on import
- Library development: Set to
falsefor libraries where unused exports should be removed
Default
truepropertyReadSideEffects?
- Type:
false|"always" - Optional
Controls whether reading properties from objects is considered to have side effects.
Set to false for more aggressive tree-shaking behavior.
See related Oxc documentation for more details.
Default
'always'propertyWriteSideEffects?
- Type:
false|"always" - Optional
Controls whether writing properties to objects is considered to have side effects.
Set to false for more aggressive behavior.
Default
'always'unknownGlobalSideEffects?
- Type:
boolean - Optional
Whether to assume that accessing unknown global properties might have side effects.
See related Oxc documentation for more details.
Default
true