# Better bundles with Rollup

DevFeed: [Better bundles with Rollup](<https://devfeed.tech/articles/better-bundles-with-rollup-52446.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/better-bundles-rollup/>)

Published: 2016-02-05T00:00:00Z

Content type: article

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [ECMAScript](<https://devfeed.tech/topics/ecmascript.md>), [Webpack](<https://devfeed.tech/topics/webpack.md>), [browserify](<https://devfeed.tech/topics/browserify.md>), [import](<https://devfeed.tech/topics/import.md>), [export](<https://devfeed.tech/topics/export.md>)

Tags: [babel](<https://devfeed.tech/tags/babel.md>), [browserify](<https://devfeed.tech/tags/browserify.md>), [cli](<https://devfeed.tech/tags/cli.md>), [command-line](<https://devfeed.tech/tags/command-line.md>), [commonjs](<https://devfeed.tech/tags/commonjs.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [es2015](<https://devfeed.tech/tags/es2015.md>), [es6](<https://devfeed.tech/tags/es6.md>), [export](<https://devfeed.tech/tags/export.md>), [exports](<https://devfeed.tech/tags/exports.md>), [import](<https://devfeed.tech/tags/import.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [live-code](<https://devfeed.tech/tags/live-code.md>), [modules](<https://devfeed.tech/tags/modules.md>), [static](<https://devfeed.tech/tags/static.md>)

## AI overview

Rollup reduced the example TodoMVC bundle from 15.8KB with Babel, Browserify, and Uglifyify to 11.3KB, a reported saving of about 4.5KB (over 33%). The article explains that Rollup can include only used code by analyzing static ES2015 imports and exports; this requires preserving ES2015 modules during transpilation. Its setup uses Babel's ES2015 Rollup preset, Rollup's Babel and Uglify plugins, and emits an IIFE bundle. The author notes that Rollup without a plugin cannot parse CommonJS modules, and leaves comparison on larger projects with npm dependencies for future work.

## Source excerpt

Recently I've been hearing a lot about Rollup, a new JavaScript bundling tool that aims to produce smaller bundling sizes through tree shaking, which is the process of taking an application and figuring out which code is actually used. Sam Saccone's cost of transpiling ES2015 repository also peaked my interest by comparing bundle sizes of popular bundling solutions. Rollup performed well in it and I was keen to give it a go! What is Tree Shaking? A bundler that supports tree shaking will "shake" your application when it bundles it to see which code is actually used. Think of this like shaking a tree branch and seeing which leaves stay on it. This is most effective when you're depending on a huge library, Lodash for example, but only use one or two of the methods in it. A bundling tool that can calculate which parts of the library are used and only include them will cut out the vast majority of the library, which is unused. With a large application that includes many 3rd party dependencies we can dramatically reduce the size of our bundle we ship to users. Isn't this dead code elimination? There's a subtle difference that the rollup README notes: Rather than excluding dead code, we should be including live code (aka 'tree-shaking'). That's only possible with ES6 modules. ## ES2015 Modules required We need our application to be written in ES2015 modules because they allow us to only import parts of a module. That is, rather than: var each = require('lodash').each; Which requires the entire module to be loaded, we can instead in ES2015 say: import { each } from 'lodash'; ES2015 modules are static, which means that their imports and exports are known without having to run the application. For example, the following isn't allowed in an ES2015 module: if (something) { export const foo = 2; } Rollup is able to parse your ES2015 application and its dependencies and eliminate any code that isn't used. ES2015 TodoMVC To demonstrate this in action I took the vanilla ES6 TodoMV