# Shakapacker
_Official, actively maintained fork of [rails/webpacker](https://github.com/rails/webpacker). For pre v6, see [rails/webpacker 5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable). Be sure to see the [CHANGELOG](./CHANGELOG.md)._
* Note, internal naming will continue to use `webpacker` where possible.
* See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
[![Ruby specs](https://github.com/shakacode/shakapacker/workflows/Ruby%20specs/badge.svg)](https://github.com/shakacode/shakapacker/actions)
[![Jest specs](https://github.com/shakacode/shakapacker/workflows/Jest%20specs/badge.svg)](https://github.com/shakacode/shakapacker/actions)
[![Rubocop](https://github.com/shakacode/shakapacker/workflows/Rubocop/badge.svg)](https://github.com/shakacode/shakapacker/actions)
[![JS lint](https://github.com/shakacode/shakapacker/workflows/JS%20lint/badge.svg)](https://github.com/shakacode/shakapacker/actions)
[![node.js](https://img.shields.io/badge/node-%3E%3D%2012.0.0-brightgreen.svg)](https://www.npmjs.com/package/shakapacker)
[![Gem](https://img.shields.io/gem/v/shakapacker.svg)](https://rubygems.org/gems/shakapacker)
Webpacker makes it easy to use the JavaScript pre-processor and bundler
[Webpack v5](https://webpack.js.org/)
to manage application-like JavaScript in Rails. It can coexist with the asset pipeline,
leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fonts, and CSS.
See a comparison of [webpacker with jsbundling-rails](https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md).
Discussion forums to discuss debugging and troubleshooting tips. Please open issues for bugs and feature requests:
1. [rails/webpacker discussion forum](https://discuss.rubyonrails.org/c/webpacker/10)
2. [Discussions tab](https://github.com/shakacode/shakapacker/discussions)
---
- [Prerequisites](#prerequisites)
- [Features](#features)
- [Optional support](#optional-support)
- [Installation](#installation)
- [Rails v6](#rails-v6)
- [Rails v7](#rails-v7)
- [Manual Installation Steps](#manual-installation-steps)
- [Usage](#usage)
- [Defer for `javascript_pack_tag`](#defer-for-javascript_pack_tag)
- [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
- [Development](#development)
- [Webpack Configuration](#webpack-configuration)
- [Babel configuration](#babel-configuration)
- [Integrations](#integrations)
- [React](#react)
- [Typescript](#typescript)
- [CoffeeScript](#coffeescript)
- [TypeScript](#typescript)
- [CSS](#css)
- [Postcss](#postcss)
- [Sass](#sass)
- [Less](#less)
- [Stylus](#stylus)
- [Other frameworks](#other-frameworks)
- [Custom Rails environments](#custom-rails-environments)
- [Upgrading](#upgrading)
- [Paths](#paths)
- [Additional paths](#additional-paths)
- [Deployment](#deployment)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [License](#license)
## Prerequisites
- Ruby 2.7+
- Rails 5.2+
- Node.js 12.13.0+ || 14+
- Yarn
## Features
- [Webpack v5](https://webpack.js.org/)
- ES6 with [babel](https://babeljs.io/)
- Automatic code splitting using multiple entry points
- Asset compression, source-maps, and minification
- CDN support
- Rails view helpers
- Extensible and configurable
### Optional support
_requires extra packages to be installed_
- Stylesheets - Sass, Less, Stylus and Css, PostCSS
- CoffeeScript
- TypeScript
- React
## Installation
### Rails v6
With Rails v6, webpacker is installed by default:
```bash
rails new myapp
```
### Rails v7
With Rails v7, skip JavaScript for a new app and follow below Manual Installation Steps to manually add the `webpacker` gem to your Gemfile.
```bash
rails new myapp --skip-javascript
```
### Manual Installation Steps
Update your `Gemfile`:
```ruby
# Gemfile
gem 'shakapacker', '~> 6.0'
# OR if you prefer to use master
gem 'shakapacker', git: 'https://github.com/shakacode/shakapacker.git'
yarn add https://github.com/shakacode/shakapacker.git
```
Then running the following to install Webpacker:
```bash
./bin/bundle install
./bin/rails webpacker:install
```
When `package.json` and/or `yarn.lock` changes, such as when pulling down changes to your local environment in a team settings, be sure to keep your NPM packages up-to-date:
```bash
yarn
```
Note, in v6, most JS packages are peer dependencies. Thus, the installer will add the packages:
```bash
yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader \
compression-webpack-plugin pnp-webpack-plugin terser-webpack-plugin \
webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server
```
Previously, these "webpack" and "babel" packages were direct dependencies for `webpacker`. By
making these peer dependencies, you have control over the versions used in your webpack and babel configs.
## Usage
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
```yml
app/javascript:
# Only Webpack entry files here
└── application.js
└── application.css
└── src:
│ └── my_component.js
└── stylesheets:
│ └── my_styles.css
└── images:
└── logo.svg
```
You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper. If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
```erb
<%= javascript_pack_tag 'application' %>
<%= stylesheet_pack_tag 'application' %>
```
The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
packs with the chunks in your view, which creates html tags for all the chunks.
The result looks like this:
```erb
<%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
```
**Important:** Pass all your pack names as multiple arguments, not multiple calls, when using `javascript_pack_tag` and the **`stylesheet_pack_tag`. Otherwise, you will get duplicated chunks on the page. Be especially careful if you might be calling these view helpers from your view, partials, and the layout for a page. You will need some logic to ensure you call the helpers only once with multiple arguments.
```erb
<%# DO %>
<%= javascript_pack_tag 'calendar', 'map' %>
<%= stylesheet_pack_tag 'calendar', 'map' %>
<%# DON'T %>
<%= javascript_pack_tag 'calendar' %>
<%= javascript_pack_tag 'map' %>
<%= stylesheet_pack_tag 'calendar' %>
<%= stylesheet_pack_tag 'map' %>
```
If you want to link a static asset for `` tag, you can use the `asset_pack_path` helper:
```erb
```
Or use the dedicated helper:
```erb
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
```
If you want to create a favicon:
```erb
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
```
If you want to preload a static asset in your `
`, you can use the `preload_pack_asset` helper:
```erb
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
```
If you want to use images in your stylesheets:
```css
.foo {
background-image: url('../images/logo.svg')
}
```
##### Defer for `javascript_pack_tag`
Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
#### Server-Side Rendering (SSR)
Note, if you are using server-side rendering of JavaScript with dynamic code-splitting, as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails), your JavaScript should create the link prefetch HTML tags that you will use, so you won't need to use to `asset_pack_path` in those circumstances.
**Note:** In order for your styles or static assets files to be available in your view, you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know to package up those files.
### Development
Webpacker ships with two binstubs: `./bin/webpacker` and `./bin/webpacker-dev-server`. Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js` executables to ensure that the right configuration files and environmental variables are loaded based on your environment.
In development, Webpacker compiles on demand rather than upfront by default. This happens when you refer to any of the pack assets using the Webpacker helper methods. This means that you don't have to run any separate processes. Compilation errors are logged to the standard Rails log. However, this auto-compilation happens when a web request is made that requires an updated webpack build, not when files change. Thus, that can be painfully slow for front-end development in this default way. Instead, you should either run the `bin/webpacker --watch` or run `./bin/webpacker-dev-server`
If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/webpacker-dev-server` or `ruby ./bin/webpacker-dev-server`. Windows users will need to run these commands in a terminal separate from `bundle exec rails s`. This process will watch for changes in the relevant files, defined by `webpacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
```bash
# webpack dev server
./bin/webpacker-dev-server
# watcher
./bin/webpacker --watch --progress
# standalone build
./bin/webpacker --progress
# Help
./bin/webpacker help
# Version
./bin/webpacker version
# Info
./bin/webpacker info
```
Once you start this webpack development server, Webpacker will automatically start proxying all webpack asset requests to this server. When you stop this server, Rails will detect that it's not running and Rails will revert back to on-demand compilation _if_ you have the `compile` option set to true in your `config/webpacker.yml`
You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `WEBPACKER_DEV_SERVER_