Course contentsShow
← JavaScript
Lesson 2457 of 2,72350. JavaScript Build ToolingFree lesson

Rollup's ES module-first approach

Why Rollup treats ES modules as first-class and how it enables better tree shaking.

Rollup's ES Module-First Approach

You'll learn why Rollup's commitment to ES modules makes it exceptional at eliminating unused code.

What Makes Rollup Different

While Webpack was designed to bundle everything (CommonJS, AMD, ES modules, assets), Rollup was built from the ground up with ES modules as its native format. This isn't just a preference—it's architectural. Rollup assumes your code uses import and export, and this assumption unlocks powerful optimizations.

Why ES Modules Enable Better Tree Shaking

ES modules have a critical advantage: their structure is statically analyzable. This means Rollup can determine exactly what's imported and exported without running the code.

Think of it like a restaurant menu versus a surprise tasting menu. With ES modules (the regular menu), Rollup knows upfront: "Module A imports functions X and Y from Module B." It can trace the entire dependency graph at build time, identifying which exports are never imported anywhere.

CommonJS, by contrast, uses require() calls that can happen conditionally or dynamically—like a chef deciding dishes on the fly. Rollup can't reliably determine what's truly unused without executing the code.

The Tree-Shaking Advantage

Because Rollup speaks ES modules natively, it performs more aggressive dead code elimination. It doesn't just remove unused files—it removes unused exports within files. If you import one function from a utility library with 50 functions, Rollup includes only that one function in your bundle (assuming no side effects).

This makes Rollup ideal for libraries where bundle size is critical and consumers only use portions of your API.

Key Takeaway: Rollup's ES module-first design enables superior tree shaking because ES modules' static structure allows Rollup to precisely identify and eliminate unused code at the export level, not just the file level.