Rollup is a JavaScript module bundler, which compiles large pieces of code and converts it to a bundle, which can load on Application load cycle.
Rollup is based on ES2015 modules, which are more efficient thaN CommonJS, which is also used by Webpack and Browserify.
There is a concept in Rollup called Tree-Shaking, which has the main purpose of eliminating unused code from the files and as the name suggests, it can remove unused code like we shake the tree and the leaves or branches shed. After removing the unused code from the files, we have included only the final code to be moved to the production.
The best benefit of Tree Shaking is when we have third party plugins and tools used in our Application, which have plenty of functions which we selectively used and also integrates with production to increase the code and the performance overload.
This is all about the basic introduction of Rollup and Tree Shaking. Now, let’s proceed by following step by step integration of Rollup and make a bundle from it.
Before proceeding further, you have to setup the environment for Angular 2. If you already have setup, proceed with this article by using step wise implementation and if you still haven't set up an environment to point to for this article, set the development environment for Angular 2 by clicking here.
Step 1
Install Rollup plugin by using the command given below.
- npm install rollup --save-dev
Step 2
- npm install rollup rollup-plugin-node-resolve rollup-plugin-commonjs rollup-plugin-uglify --save-dev
Step 3
Next, create a configuration file (rollup-config.js) in the project root directory to tell Rollup, how to process the Application.
Copy line of code to the file.
- import rollup from 'rollup'
- import nodeResolve from 'rollup-plugin-node-resolve'
- import commonjs from 'rollup-plugin-commonjs';
- import uglify from 'rollup-plugin-uglify'
- export default {
- entry: 'src/main.js',
- dest: 'src/build.js', // output a single application bundle
- sourceMap: false,
- format: 'iife',
- onwarn: function(warning) {
- // Skip certain warnings
- // should intercept ... but doesn't in some rollup versions
- if (warning.code === 'THIS_IS_UNDEFINED') {
- return;
- }
- // intercepts in some rollup versions
- if (warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1) {
- return;
- }
- // console.warn everything else
- console.warn(warning.message);
- },
- plugins: [
- nodeResolve({
- jsnext: true,
- module: true
- }),
- commonjs({
- include: 'node_modules/rxjs/**',
- }),
- uglify()
- ]
- }

Mohsin ArifPosted Apr 17, 2019, 1:39 PM
Very knowledgeable article
Brian O'NeilPosted Sep 29, 2017, 3:06 PM
Very good; now enhance your process with Angular 2 Ahead Of Time Compilation (AOT). Thanks