Top Related Projects
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
The zero configuration build tool for the web. 📦🚀
Next generation frontend tooling. It's fast!
The React Framework
Cybernetically enhanced web apps
Quick Overview
Webpacker is a Ruby gem that integrates Webpack, a popular JavaScript module bundler, with Rails applications. It provides a convenient way to manage and compile JavaScript, CSS, and other assets in Rails projects, allowing developers to use modern front-end tools and workflows within the Rails ecosystem.
Pros
- Seamless integration of modern JavaScript tooling with Rails
- Simplified asset management and compilation process
- Support for ES6+ features, CSS preprocessing, and code splitting
- Easy configuration and customization options
Cons
- Learning curve for developers unfamiliar with Webpack
- Potential complexity in larger projects with advanced configurations
- Occasional version compatibility issues between Webpacker and Rails
- Performance overhead compared to simpler asset pipelines
Code Examples
- Installing Webpacker in a Rails application:
# Gemfile
gem 'webpacker', '~> 5.0'
# Then run
bundle install
rails webpacker:install
- Adding a new JavaScript pack:
// app/javascript/packs/application.js
import 'core-js/stable'
import 'regenerator-runtime/runtime'
// Your custom JavaScript code
console.log('Hello from Webpacker!')
- Using a Webpacker-managed JavaScript pack in a view:
<%# app/views/layouts/application.html.erb %>
<%= javascript_pack_tag 'application' %>
- Configuring Webpacker:
# config/webpacker.yml
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
webpack_compile_output: true
development:
<<: *default
compile: true
# Other environment-specific settings
Getting Started
-
Add Webpacker to your Rails project:
# Gemfile gem 'webpacker', '~> 5.0'
-
Install Webpacker:
bundle install rails webpacker:install
-
Create a new JavaScript pack:
touch app/javascript/packs/hello.js
-
Add content to the pack:
// app/javascript/packs/hello.js console.log('Hello from Webpacker!')
-
Include the pack in your view:
<%= javascript_pack_tag 'hello' %>
-
Start your Rails server and visit a page that includes the pack to see it in action.
Competitor Comparisons
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Pros of webpack
- More flexible and customizable for various project types
- Larger ecosystem with extensive plugins and loaders
- Better performance for complex builds and large applications
Cons of webpack
- Steeper learning curve and more complex configuration
- Requires more manual setup for Rails integration
- May be overkill for simpler Rails applications
Code Comparison
webpack configuration:
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
// ... more configuration options
};
Webpacker configuration:
# config/webpacker.yml
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
# ... more configuration options
Summary
webpack offers more flexibility and power for complex projects, while Webpacker provides a simpler, Rails-specific integration. webpack excels in customization and performance for large applications, but may be more challenging to set up. Webpacker offers easier integration with Rails and a gentler learning curve, making it suitable for simpler Rails projects. The choice between the two depends on project requirements, team expertise, and desired level of control over the asset pipeline.
The zero configuration build tool for the web. 📦🚀
Pros of Parcel
- Zero configuration required out of the box
- Faster build times due to multicore processing
- Supports a wider range of file types and assets natively
Cons of Parcel
- Less integrated with Rails ecosystem
- May require additional setup for some Rails-specific features
- Smaller community and fewer Rails-specific resources
Code Comparison
Webpacker configuration:
# config/webpacker.yml
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
Parcel configuration:
// package.json
{
"source": "src/index.html",
"scripts": {
"start": "parcel",
"build": "parcel build"
}
}
Webpacker focuses on Rails integration and conventions, while Parcel aims for simplicity and zero-config setup. Webpacker requires more configuration but offers tighter Rails integration. Parcel provides a more straightforward approach but may need additional setup for Rails-specific features. The choice between them depends on project requirements and developer preferences.
Next generation frontend tooling. It's fast!
Pros of Vite
- Significantly faster build times and hot module replacement (HMR)
- Native ES modules support, reducing bundle size and improving performance
- Simpler configuration and setup process
Cons of Vite
- Less mature ecosystem compared to Webpacker
- May require additional setup for certain Rails-specific features
- Limited built-in support for older browsers
Code Comparison
Vite configuration (vite.config.js):
export default {
plugins: [],
build: {
outDir: 'dist',
rollupOptions: {}
}
}
Webpacker configuration (webpacker.yml):
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
Summary
Vite offers superior performance and a more modern approach to asset bundling, making it an attractive option for new projects or those looking to improve build times. However, Webpacker's tight integration with Rails and its mature ecosystem may still be preferable for some developers, especially those working on existing Rails applications or requiring extensive customization.
The React Framework
Pros of Next.js
- Built-in server-side rendering and static site generation
- Automatic code splitting for faster page loads
- Extensive ecosystem with plugins and community support
Cons of Next.js
- Steeper learning curve for developers new to React
- Less flexibility in project structure compared to Webpacker
- Potential overhead for simpler applications
Code Comparison
Next.js:
// pages/index.js
export default function Home() {
return <h1>Welcome to Next.js!</h1>
}
Webpacker:
# app/javascript/packs/application.js
import React from 'react'
import ReactDOM from 'react-dom'
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(<h1>Welcome to Webpacker!</h1>, document.body.appendChild(document.createElement('div')))
})
Key Differences
- Next.js is a full-fledged React framework, while Webpacker is a Rails asset pipeline replacement
- Next.js provides a more opinionated structure for React applications
- Webpacker offers greater flexibility for integrating with existing Rails applications
Use Cases
- Choose Next.js for standalone React applications with server-side rendering requirements
- Opt for Webpacker when integrating React components into existing Rails projects or when more control over asset management is needed
Cybernetically enhanced web apps
Pros of Svelte
- Smaller bundle sizes due to compile-time optimization
- Simpler, more intuitive syntax with less boilerplate code
- Better performance with minimal runtime overhead
Cons of Svelte
- Smaller ecosystem and community compared to Rails/Webpack
- Less mature tooling and integration options
- Limited server-side rendering capabilities out of the box
Code Comparison
Svelte component:
<script>
let count = 0;
const increment = () => count += 1;
</script>
<button on:click={increment}>
Clicks: {count}
</button>
Webpacker with React:
import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicks: {count}
</button>
);
};
Svelte offers a more concise syntax and doesn't require explicit imports for reactivity. Webpacker with React needs additional setup and imports but provides a familiar ecosystem for Rails developers. Svelte's approach results in smaller bundle sizes and potentially better performance, while Webpacker offers tighter integration with Rails and a wider range of available tools and libraries.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Webpacker has been retired ð
Webpacker has served the Rails community for over five years as a bridge to compiled and bundled JavaScript. This bridge is no longer needed for most people in most situations following the release of Rails 7. We now have three great default answers to JavaScript in 2021+, and thus we will no longer be evolving Webpacker in an official Rails capacity.
For applications currently using Webpacker, the first recommendation is to switch to jsbundling-rails with Webpack (or another bundler). You can follow the switching guide, if you choose this option.
Secondly, you may want to try making the jump all the way to import maps. That's the default setup for new Rails 7 applications, but depending on your JavaScript use, it may be a substantial jump.
Finally, you can continue to use Webpacker as-is. We will continue to address security issues on the Ruby side of the gem according to the normal maintenance schedule of Rails. But we will not be updating the gem to include newer versions of the JavaScript libraries. This pertains to the v5 edition of this gem that was included by default with previous versions of Rails.
The development of v6 will not result in an official gem released by the Rails team nor see any future support. But Justin Gordon is continuing that line of development â including a focus on hot-module reloading features etc â under a new gem called Shakapacker that is based on the unreleased v6 work from this repository.
Thank you to everyone who has contributed to Webpacker over the last five-plus years!
Please refer to the 5-x-stable branch for 5.x documentation.
Top Related Projects
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
The zero configuration build tool for the web. 📦🚀
Next generation frontend tooling. It's fast!
The React Framework
Cybernetically enhanced web apps
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot