Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Webpacker in Ruby on Rails?

In this blog post, we will dive into the world of Ruby on Rails and learn about a popular tool called Webpacker. If you are new to programming or just starting with Ruby on Rails, don't worry! We will try our best to avoid jargon and explain everything in simple terms. We will also provide actual code examples and use intuitions and analogies to make it easier to understand. So, let's get started!

Introduction to Webpacker

In the world of web development, we often use many different files and types of assets like JavaScript, CSS, and images. These assets help us build beautiful and interactive web applications. However, managing and organizing these assets can sometimes become a challenge.

This is where tools like Webpacker come into play. In simple terms, Webpacker is a wrapper around the popular JavaScript module bundler called Webpack. It helps us easily manage and bundle our assets in a Ruby on Rails application. With Webpacker, we can write modern JavaScript using the latest features, as well as use popular libraries and frameworks, like React, Vue, or Angular.

To understand why Webpacker is essential, let's first understand the problem it solves.

The Problem: Managing Assets in Rails

Before Webpacker, the default way to manage assets in Rails was using the Rails Asset Pipeline. The Asset Pipeline is a robust tool to concatenate, minify, and compress assets like JavaScript, CSS, and images. It also has support for preprocessors like CoffeeScript or SASS.

While the Asset Pipeline works well for simple applications, it has some limitations when it comes to handling modern JavaScript development practices. For instance, it does not handle JavaScript modules or support advanced features like code splitting, hot module replacement, and tree shaking.

Due to these limitations, Rails developers started integrating Webpack into their applications to better manage their JavaScript assets. This led to the creation of Webpacker, which makes it easy to use Webpack in Rails applications.

Installing Webpacker in Rails

Now that we have a basic understanding of Webpacker, let's see how we can install and use it in a Rails application.

Starting from Rails 6, Webpacker is included by default in new Rails applications. If you are using Rails 5.1 or later, you can add Webpacker by running the following command:

rails webpacker:install

This command will do the following:

  1. Add the webpacker gem to the Gemfile and run bundle install.
  2. Install Webpack and its dependencies using Yarn (a popular JavaScript package manager).
  3. Generate the necessary configuration files.

After running this command, you will see a new folder named app/javascript in your Rails application. This is where you will put all your JavaScript assets.

How Webpacker Works

To understand how Webpacker works, let's take a look at the main components involved:

Webpack: Webpack is the core JavaScript module bundler that Webpacker uses. It goes through your JavaScript files, starting from an entry point, and bundles all the required modules into a single file (or multiple files if code splitting is used). It also handles other assets like CSS and images.

Loaders and Plugins: Webpack uses loaders and plugins to process and transform files. Loaders are functions that tell Webpack how to interpret and transform a file before bundling. Plugins, on the other hand, perform various tasks like minification, code splitting, and hot module replacement.

Configuration: Webpacker provides a set of default configurations that work well for most Rails applications. However, you can customize these configurations to suit your needs. The main configuration file is config/webpacker.yml, which contains settings like the output directory, cache, and development server.

Development Server: Webpacker uses the Webpack Dev Server to serve your assets during development. It watches your files for changes and automatically rebuilds the bundles when needed. It also supports hot module replacement, which updates your code without requiring a full page refresh.

Now that we know the main components, let's see how we can use Webpacker in a Rails application.

Using Webpacker in Rails

Adding JavaScript Files

With Webpacker, you can organize your JavaScript code into multiple files and folders inside the app/javascript directory. Webpack will automatically bundle these files into a single output file (or multiple files if code splitting is used).

To include the bundled JavaScript file in your Rails views, you can use the javascript_pack_tag helper method. For example, if you have a file called app/javascript/packs/application.js, you can include it in your layout file like this:

<%= javascript_pack_tag 'application' %>

Adding CSS Files

Webpacker can also handle CSS files using the style-loader and css-loader Webpack loaders. To add a CSS file, create a new file inside the app/javascript directory (e.g., app/javascript/styles/application.css). Then, import the CSS file in your JavaScript pack file like this:

import '../styles/application.css';

To include the bundled CSS file in your Rails views, you can use the stylesheet_pack_tag helper method:

<%= stylesheet_pack_tag 'application' %>

Adding Images

To add images to your Rails application, you can use the file-loader or url-loader Webpack loaders. First, create an images folder inside the app/javascript directory and add your images there. Then, import the images in your JavaScript or CSS files like this:

import logo from '../images/logo.png';
.background {
  background-image: url('~images/background.jpg');
}

Webpacker will automatically process and optimize the images during bundling.

Using JavaScript Libraries and Frameworks

With Webpacker, you can easily use popular JavaScript libraries and frameworks like React, Vue, or Angular. To install a library, use the yarn add command:

yarn add react react-dom

Then, import and use the library in your JavaScript code:

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(<h1>Hello, world!</h1>, document.getElementById('root'));

Webpacker also provides built-in support for some popular frameworks. For example, to install and configure Vue.js, you can run the following command:

rails webpacker:install:vue

This command will install the necessary dependencies and generate a sample Vue component that you can use as a starting point.

Conclusion

In this blog post, we learned about Webpacker and how it helps us manage and bundle assets in a Ruby on Rails application. We discussed the limitations of the Rails Asset Pipeline and how Webpacker solves those problems. We also covered how to install and use Webpacker, including adding JavaScript, CSS, images, and using popular libraries and frameworks.

With Webpacker, you can now build modern, interactive, and scalable web applications using the latest JavaScript features and frameworks while still enjoying the simplicity and productivity of Ruby on Rails. Happy coding!