April 1, 2019
Using Webpack with Ruby on Rails
Webpack, although a little hard to configure, remains the leading edge build tool for modern web development. This post/video guides you through adding Webpack to an older Ruby on Rails 5.2 project as well as the latest Ruby on Rails 6 beta release.
The demos here are meant to be a high-level overview of how Webpack can be used inside Ruby on Rails no matter the version.
With Webpacker you gain these features and more:
- webpack 4.x.x
- ES6 with babel
- Automatic code splitting using multiple entry points
- Stylesheets - Sass and CSS
- Images and fonts
- PostCSS - Auto-Prefixer
- Asset compression, source-maps, and minification
- CDN support
- React, Angular, Elm and Vue support out-of-the-box
- Rails view helpers
Why do I need webpack with Rails if the asset pipeline is already present?
Realistically, in most cases, you don't but it does make it easier to reach for modern front-end frameworks like Vue.js or React.js with minimal setup time. The asset pipeline still works as it should alongside the new webpack workflow. All assets associated with webpack now live inside an
Does webpack depend on the asset pipeline? Do they conflict?
The webpacker gem doesn't depend on the asset pipeline directly though it does hook into a few rake commands bundled with each Rails installation
app/assets/ for any
css, images, or other assets the project I'm working on might require.
Do I even need the asset pipeline with the webpack addition?
Realistically, no but the asset pipeline has a bunch of handy features for rendering assets in views you or your team may be accustomed to. Luckily you can do the same with assets inside
Shameless plug time
I have a new course called Hello Rails. Hello Rails is modern course designed to help you start using and understanding Ruby on Rails fast. If you're a novice when it comes to Ruby or Ruby on Rails I invite you to check out the site. The course will be much like these builds but a super more in-depth version with more realistic goals and deliverables. View the course!