Laravel Idea for PhpStorm - Full-featured IDE for productive artisans!

Introduction to Rollup.js

Published on by

Introduction to Rollup.js image

Rollup is a next-generation JavaScript module bundler. Using it you can build your app or library using ES2015 modules, then efficiently bundle them up into a single file.

This helps combat the problem of the ever-growing size and complexity of your scripts. It gives us the ability to divide our programs into modules, so we can organize them into different files and directories.

These self-contained chunks of code that provides distinct functionality that we can use, swap and delete easily which means increasing in the readability, maintainability, and reusability of our code.

Rollup.js Overview

Rollup is a JavaScript module bundler that enables you to code your apps using ES2015 modules, then it will concatenate all the modules into a single file in order to reduce the number of HTTP requests and improve pages load time. Rollup aims to be fast and generates a cleaner and more efficient code than any other module bundlers.

By using Rollup, you will benefit from ES2015 static analysis which means your imported modules will be resolved at compile time (before executing) and any unused exports will be removed before the program runs so it will save the size and decreases the overhead of your scripts.

Another great feature is Tree-shaking, which leads to exclude unused exports from bundles. So, instead of importing the whole module, Tree-shaking allows you to import only the bits you need which will make your bundles smaller.

Take a look for the following example, assume that we have two functions at greetings.js file:

export function sayHello () {
return "Hello!";
}
 
export function sayHi () {
return "Hi!";
}

And we imported all of these functions in app.js, then we use only sayHello by logging it to the console:

import * as Greetings from './greetings.js';
console.log( Greetings.sayHello() ); // Hello!

Notice how the output will be, by utilizing Tree-shaking, Rollup will exclude sayHi function because it’s not used:

function sayHello () {
return "Hello!";
}
 
console.log( sayHello() ); // Hello!

Be aware that Rollup is a module bundler, it’s not, however, an ES2015 to ES5 converter, So it will not take your ES2015 class syntax and compile it to ES5 syntax. For compiling ES2015 to ES5, you might use bable or buble which is another ES2015 compiling tool from Rich Harris, the creator of Rollup.

Rollup with Laravel Elixir

Laravel 5.3 is using Webpack by default, but switching to Rollup is very easy. First, install Elixir Rollup extension:

npm install laravel-elixir-rollup-official --save-dev

Then, use the Rollup task in your gulp file like this:

elixir(mix => {
mix.rollup('app.js');
});

The Rollup task accepts an array of files relative to the resources/assets/js directory and generates a single file in the public/js directory. If you want to customize the paths of input and output files, pass the path of the input file as the first argument and the output path as the second argument, Be aware that you should begin the path with ./ to instructs Elixir to omit any default base directories:

elixir(function(mix) {
mix.rollup(
'./resources/assets/js/app.js',
'./public/dist'
);
});

Note that Elixir Rollup extension uses buble as a dependency. So Elixir will comfort you by compiling ES2015 to ES5 using buble automatically!

Lastly, if you want to override the default Rollup configuration, you have two choices, one is creating a rollup.config.js file in the root of the project, another option is by passing a Rollup config object as the fourth argument to Rollup Elixir task.

That’s it, if you are looking for a fast, clean, and efficient module bundler, give Rollup a try on your next project.

Diaa Fares photo

Laravel Artisan, Web Developer, Tech Geek

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Laravel Idea for PhpStorm logo

Laravel Idea for PhpStorm

Ultimate PhpStorm plugin for Laravel developers, delivering lightning-fast code completion, intelligent navigation, and powerful generation tools to supercharge productivity.

Laravel Idea for PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
JetShip - Laravel Starter Kit logo

JetShip - Laravel Starter Kit

A Laravel SaaS Boilerplate and a starter kit built on the TALL stack. It includes authentication, payments, admin panels, and more. Launch scalable apps fast with clean code, seamless deployment, and custom branding.

JetShip - Laravel Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Handling Geospatial Data with Laravel Magellan image

Handling Geospatial Data with Laravel Magellan

Read article
Managing Large Datasets in Laravel with LazyCollection image

Managing Large Datasets in Laravel with LazyCollection

Read article
Collect and Monitor Everything About Sent Emails in Your Laravel App image

Collect and Monitor Everything About Sent Emails in Your Laravel App

Read article
Packagist.org is ending support for Composer 1.x image

Packagist.org is ending support for Composer 1.x

Read article
Mastering Dynamic String Manipulation with Laravel's Str::replaceArray() image

Mastering Dynamic String Manipulation with Laravel's Str::replaceArray()

Read article
Laravel Jobs - December image

Laravel Jobs - December

Read article