Export Patterns That Enable Tree Shaking
You'll learn how to structure your exports so bundlers can confidently remove unused code from your production builds.
Why Export Style Matters
Tree shaking works by analyzing your module's exports and imports at build time. The bundler traces which functions and variables are actually imported and used, then eliminates the rest. But this static analysis only works when the bundler can clearly see what you're exporting—and that clarity depends entirely on how you write your exports.
Named Exports: The Tree-Shakeable Pattern
Named exports are the gold standard for tree shaking. Each export is explicitly declared, making it trivial for bundlers to track which pieces are used:
// utils.js - GOOD for tree shaking
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
export function divide(a, b) {
return a / b;
}
When you import only add, the bundler sees that multiply and divide are never referenced and removes them completely.
Default Exports: The Tree-Shaking Obstacle
Default exports create ambiguity. When you export a single object or function as default, the bundler often can't determine which properties of that object are used:
// utils.js - BAD for tree shaking
export default {
add: (a, b) => a + b,
multiply: (a, b) => a * b,
divide: (a, b) => a / b
};
The bundler sees the entire object imported, even if you only call .add(). All three functions ship to production.
Key takeaway: Use named exports consistently to maximize tree shaking. Each function or constant should be its own export statement, not bundled into a default object. This gives bundlers the visibility they need to eliminate dead code effectively.