DEV Community

Cover image for How to Use Webpacker in Ruby on Rails in 2025?
Jordan Knightin
Jordan Knightin

Posted on

1

How to Use Webpacker in Ruby on Rails in 2025?

In 2025, managing JavaScript assets in a Ruby on Rails application remains crucial for creating dynamic, performant web applications. Webpacker has emerged as a vital tool in this regard, providing Rails developers with the capabilities to easily integrate modern JavaScript frameworks and libraries into their projects. This comprehensive guide will walk you through using Webpacker in Ruby on Rails, ensuring your app is ready for the next generation of web development.

What is Webpacker?

Webpacker is a modern JavaScript toolchain that helps manage JavaScript, CSS, and other assets in Rails applications. It acts as a wrapper around Webpack, a popular JavaScript bundler, allowing for efficient asset compilation and module bundling.

Setting Up Webpacker in Rails

Step 1: Install Webpacker

Firstly, ensure that you have Rails installed. If not, install the latest version of Rails:

gem install rails
Enter fullscreen mode Exit fullscreen mode

Next, create a new Rails application with Webpacker:

rails new my_app --webpack
Enter fullscreen mode Exit fullscreen mode

This command sets up a new Rails project with Webpacker integrated.

Step 2: Integrate Webpacker with Your App

If you’re adding Webpacker to an existing Rails application, include the Webpacker gem in your Gemfile:

gem 'webpacker', '~> 6.0'
Enter fullscreen mode Exit fullscreen mode

Then, run the bundle command to install it:

bundle install
Enter fullscreen mode Exit fullscreen mode

After installing the gem, initialize Webpacker:

rails webpacker:install
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Webpacker

Configure Webpacker by modifying the webpacker.yml file located in the config directory. This configuration file specifies settings for different environments (development, production, etc.).

Step 4: Adding JavaScript Frameworks

With Webpacker, you can easily integrate popular JavaScript frameworks such as React, Vue.js, or Angular:

  • React: To add React, run:
  rails webpacker:install:react
Enter fullscreen mode Exit fullscreen mode
  • Vue.js: To add Vue.js, run:
  rails webpacker:install:vue
Enter fullscreen mode Exit fullscreen mode
  • Angular: To add Angular, you might need custom setup instructions available via Webpacker plugins or guidance from the Webpack documentation, as direct support may evolve by 2025.

Step 5: Compiling Assets

Use the following command to compile assets in development mode:

bin/webpack-dev-server
Enter fullscreen mode Exit fullscreen mode

For production mode, precompile the assets:

rails assets:precompile
Enter fullscreen mode Exit fullscreen mode

Step 6: Managing JavaScript Components

Place your JavaScript files in the app/javascript folder. Webpacker uses this directory for processing and managing your application’s JavaScript.

Advanced Tips for 2025

  • Upgrading Webpacker: Ensure that you keep Webpacker and its dependencies up-to-date to take advantage of the latest features and security updates.
  • Optimizing Performance: Utilize Webpack’s plugins for code splitting and asset optimization to enhance your app’s performance.

Useful Resources

For further reading on enhancing your Ruby on Rails application, you might find these articles helpful:

By 2025, using Webpacker in Ruby on Rails is essential for developing feature-rich web applications. With these guidelines, you’re well-equipped to harness the power of Webpacker, making your Rails projects more dynamic and responsive.

For more updates on Webpacker’s evolution and other Rails-related content, stay connected with the Rails community and keep expanding your knowledge.

AWS Security LIVE! Stream

Streaming live from AWS re:Inforce

Tune into Security LIVE! at re:Inforce for expert takes on modern security challenges.

Learn More

Top comments (0)

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping