Shakapacker is a modern JavaScript and asset bundler for Ruby on Rails applications. It leverages Webpack to manage and compile JavaScript, CSS, and other assets, making it easier to integrate modern front-end tools and frameworks into Rails projects.
What are shakapacker's main functionalities?
JavaScript Bundling
Shakapacker allows you to bundle JavaScript files using Webpack. The code sample demonstrates a basic Webpack configuration for bundling JavaScript files in a Rails application.
Shakapacker can manage CSS and other assets like images. The code sample shows how to configure Webpack to handle CSS files and image assets using appropriate loaders.
Shakapacker supports Hot Module Replacement (HMR) for a better development experience. The code sample configures Webpack's dev server to enable HMR, allowing modules to be updated in the browser without a full reload.
Webpacker is another asset bundler for Rails applications, similar to Shakapacker. It also uses Webpack to manage JavaScript, CSS, and other assets. Webpacker is the predecessor to Shakapacker and offers similar functionalities but may not be as up-to-date with the latest Webpack features.
Vite Rails is a modern alternative to Webpacker and Shakapacker, using Vite as the asset bundler. Vite offers faster build times and a more modern development experience compared to Webpack-based solutions. It is a good option for developers looking for a cutting-edge toolchain.
Shakapacker
Official, actively maintained successor to rails/webpacker. Internal naming for shakapacker will continue to use webpacker where possible for v6. ShakaCode stands behind the long-term maintainence and development of this project for the Rails community.
See V6 Upgrade for upgrading from v5 or prior v6 releases.
Webpacker makes it easy to use the JavaScript pre-processor and bundler Webpack v5+
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.
Check out 6.1.1+ for SWC and esbuild-loader support! They are faster than Babel!
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:
yarn
Note, in v6+, most JS packages are peer dependencies. Thus, the installer will add the packages:
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.
Note for Yarn v2 usage
If you are using Yarn v2 (berry), please note that PnP modules are not supported.
In order to use Shakapacker with Yarn v2, make sure you set nodeLinker: node-modules in your .yarnrc.yml file as per the Yarn docs to opt out of Plug'n'Play behaviour.
Concepts
At it's core, Shakapacker's essential functionality is to:
Provide configuration by a single file used by both Rails view helpers and JavaScript webpack compilation code.
Provide Rails view helpers, utilizing this configuration file, so that a webpage can load JavaScript, CSS, and other static assets compiled by webpack, supporting bundle splitting, fingerprinting, and HMR.
Provide a community supported, default webpack compilation that generates the necessary bundles and manifest, using the same configuration file. This compilation can be extended for any needs.
Usage
Configuration and Code
You will need your file system to correspond to the setup of your webpacker.yml file.
Suppose you have the following configuration:
webpacker.yml
default:&defaultsource_path:app/javascriptsource_entry_path:packspublic_root_path:publicpublic_output_path:packsnested_entries:false# And more
And that maps to a directory structure like this:
app/javascript:
└── packs: # sets up webpack entries
│ └── application.js # references ../src/my_component.js
│ └── application.css
└── src: # any directory name is fine. Referenced files need to be under source_path
│ └── my_component.js
└── stylesheets:
│ └── my_styles.css
└── images:
└── logo.svg
public/packs # webpack output
Webpack intelligently includes only necessary files. In this example, the file packs/application.js would reference ../src/my_component.js
nested_entries allows you to have webpack entry points nested in subdirectories. This defaults to false so you don't accidentally create entry points for an entire tree of files. In other words, with nested_entries: false, you can have your entire source_path used for your source (using the source_entry_path: /) and you place files at the top level that you want as entry points. nested_entries: true allows you to have entries that are in subdirectories. This is useful if you have entries that are generated, so you can have a generated subdirectory and easily separate generated files from the rest of your codebase.
View Helpers
The Shakapacker view helpers generate the script and link tags to get the webpack output onto your views.
Be sure to consult the API documentation in the source code of helper.rb.
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.
View Helpers javascript_pack_tag and stylesheet_pack_tag
These view helpers take your webpacker.yml configuration file, along with the resulting webpack compilation manifest.json and generates the HTML to load the assets.
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:
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.
You can provide multiple packs and other attributes. Note, defer defaults to showing.
In this output, both the calendar and map codes might refer to other common libraries. Those get placed something like the vendor bundle. The view helper removes any duplication.
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/javascript/application.js, pass the option defer: false to your javascript_pack_tag.
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.
While this also generally applies to stylesheet_pack_tag, you may use multiple calls to stylesheet_pack_tag if, say, you require multiple tags for different output media:
View Helper append_javascript_pack_tag and append_stylesheet_pack_tag
If you need configure your script pack names or stylesheet pack names from the view for a route or partials, then you will need some logic to ensure you call the helpers only once with multiple arguments. The new view helpers, append_javascript_pack_tag and append_stylesheet_pack_tag can solve this problem. The helper append_javascript_pack_tag will queue up script packs when the javascript_pack_tag is finally used. Similarly,append_stylesheet_pack_tag will queue up style packs when the stylesheet_pack_tag is finally used.
However, you typically can't do that in the main layout, as the view and partial codes will depend on the route.
Thus, you can distribute the logic of what packs are needed for any route. All the magic of splitting up the code and CSS was automatic!
Important: Both append_(javascript/stylesheet)_pack_tag helpers can be used anywhere in your application as long as they are executed BEFORE (javascript/stylesheet)_pack_tag respectively. If you attempt to call one of the append_(javascript/stylesheet)_pack_tag helpers after the respective (javascript/stylesheet)_pack_tag, an error will be raised.
The typical issue is that your layout might reference some partials that need to configure packs. A good way to solve this problem is to use content_for to ensure that the code to render your partial comes before the call to javascript_pack_tag.
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, 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.
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.
Note: older Shakapacker installations had set a missing NODE_ENV in the binstubs. Please remove this for versions 6.5.2 and newer.
Automatic Webpack Code Building
Shakapacker can be configured to automatically compile on demand when needed using the webpacker.ymlcompile option. This happens when you refer to any of the pack assets using the Shakapacker 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 during development.
The compile: true option can be more useful for test and production builds.
Compiler strategies
Shakapacker ships with two different strategies that are used to determine whether assets need recompilation per the compile: true option:
digest - This strategy calculates SHA1 digest of files in your watched paths (see below). The calculated digest is then stored in a temp file. To check whether the assets need to be recompiled, Shakapacker calculates the SHA1 of the watched files and compares it with the one stored. If the digests are equal, no recompilation occurs. If the digests are different or the temp file is missing, files are recompiled.
mtime - This strategy looks at last modified at timestamps of both files AND directories in your watched paths. The timestamp of the most recent file or directory is then compared with the timestamp of manifest.json file generated. If the manifest file timestamp is newer than one of the most recently modified file or directory in the watched paths, no recompilation occurs. If the manifest file is order, files are recompiled.
The mtime strategy is generally faster than the digest one, but it requires stable timestamps, this makes it perfect for a development environment, such as needing to rebuild bundles for tests, or if you're not changing frontend assets much.
In production or CI environments, the digest strategy is more suitable, unless you are using incremental builds or caching and can guarantee that the timestamps will not change after e.g. cache restore. However, many production or CI environments will explicitly compile assets, so compile: false is more appropriate. Otherwise, you'll waste time either checking file timestamps or computing digests.
You can control what strategy is used by compiler_strategy option in webpacker.yml config file. By default mtime strategy is used in development environment, digest is used elsewhere.
Note: If you are not using the webpack-dev-server, your packs will be served by Rails public file server. If you've enabled caching (Rails application config.action_controller.perform_caching setting), your changes will likely not be picked up due to Cache-Control header being set and assets being cached in browser memory. For more details see the issue #88.
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. 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.
Common Development Commands
# webpack dev server
./bin/webpacker-dev-server
# watcher
./bin/webpacker --watch --progress
# standalone build
./bin/webpacker --progress
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 in the form WEBPACKER_DEV_SERVER_<OPTION>. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the same environmental variables must be available to the rails server process.
By default, the webpack dev server listens on localhost:3035 in development for security purposes. However, if you want your app to be available on port 4035 over local LAN IP or a VM instance like vagrant, you can set the port and host when running ./bin/webpacker-dev-server binstub:
Note: You need to allow webpack-dev-server host as an allowed origin for connect-src if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer config/initializers/content_security_policy.rb with a snippet like this:
Rails.application.config.content_security_policy do |policy|
policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035'ifRails.env.development?
end
Note: Don't forget to prefix ruby when running these binstubs on Windows
Webpack Configuration
First, you don't need to use Shakapacker's webpack configuration. However, the shakapacker NPM package provides convenient access to configuration code that reads the config/webpacker.yml file which the view helpers also use. If you have your own customized webpack configuration, at the minimum, you must ensure:
Your output files go the right directory
Your output includes a manifest, via package webpack-assets-manifest that maps output names (your 'packs') to the fingerprinted versions, including bundle-splitting dependencies. That's the main secret sauce of webpacker!
The most practical webpack configuration is to take the default from Shakapacker and then use webpack-merge to merge your customizations with the default. For example, suppose you want to add some resolve.extensions:
// use the new NPM package name, `shakapacker`.// merge is webpack-merge from https://github.com/survivejs/webpack-mergeconst { webpackConfig: baseWebpackConfig, merge } = require('shakapacker')
const options = {
resolve: {
extensions: ['.css', '.ts', '.tsx']
}
}
// Copy the object using merge b/c the baseClientWebpackConfig is a mutable global// If you want to use this object for client and server rendering configurations,// having a new object is essential.module.exports = merge({}, baseWebpackConfig, options)
Shakapacker gives you a default configuration file config/webpack/webpack.config.js, which, by default, you don't need to make any changes to config/webpack/webpack.config.js since it's a standard production-ready configuration. However, you will probably want to customize or add a new loader by modifying the webpack configuration, as shown above.
You might add separate files to keep your code more organized.
Then require this file in your config/webpack/webpack.config.js:
// config/webpack/webpack.config.js// use the new NPM package name, `shakapacker`.const { webpackConfig, merge } = require('shakapacker')
const customConfig = require('./custom')
module.exports = merge(webpackConfig, customConfig)
If you need access to configs within Shakapacker's configuration, you can import them like so:
// config/webpack/webpack.config.jsconst { webpackConfig } = require('shakapacker')
console.log(webpackConfig.output_path)
console.log(webpackConfig.source_path)
// Or to print out your whole webpack configurationconsole.log(JSON.stringify(webpackConfig, undefined, 2))
You may want to modify rules in the default configuration. For instance, if you are using a custom svg loader, you may want to remove .svg from the default file loader rules. You can search and filter the default rules like so:
Optionally, you can change your Babel configuration by removing these lines in your package.json and add a Babel configuration file in your project. For an example customization based on the original, see Customizing Babel Config.
SWC configuration
You can try out experimental integration with the SWC loader. You can read more at SWC usage docs.
Please note that if you want opt-in to use SWC, you can skip React integration instructions as it is supported out of the box.
esbuild loader configuration
You can try out experimental integration with the esbuild-loader. You can read more at esbuild-loader usage docs.
Please note that if you want opt-in to use esbuild-loader, you can skip React integration instructions as it is supported out of the box.
Integrations
Shakapacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
Out of the box Shakapacker ships with - development, test and production environments in config/webpacker.yml however, in most production apps extra environments are needed as part of deployment workflow. Shakapacker supports this out of the box from version 3.4.0+ onwards.
You can choose to define additional environment configurations in webpacker.yml,
staging:<<:*default# Production depends on precompilation of packs prior to booting for performance.compile:false# Cache manifest.json for performancecache_manifest:true# Compile staging packs to a separate directorypublic_output_path:packs-staging
Otherwise Shakapacker will use production environment as a fallback environment for loading configurations. Please note, NODE_ENV can either be set to production, development or test. This means you don't need to create additional environment files inside config/webpacker/* and instead use webpacker.yml to load different configurations using RAILS_ENV.
For example, the below command will compile assets in production mode but will use staging configurations from config/webpacker.yml if available or use fallback production environment configuration:
And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
./bin/webpacker
./bin/webpacker-dev-server
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/webpacker/compile.rake`
bundle exec rails assets:precompile
bundle exec rails webpacker:compile
Upgrading
You can run following commands to upgrade Shakapacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
# check your Gemfile for version restrictions
bundle update shakapacker
# overwrite your changes to the default install files and revert any unwanted changes from the install
rails webpacker:install
# yarn 1 instructions
yarn upgrade shakapacker --latest
yarn upgrade webpack-dev-server --latest
# yarn 2 instructions
yarn up shakapacker@latest
yarn up webpack-dev-server@latest
# Or to install the latest release (including pre-releases)
yarn add shakapacker@next
Also, consult the CHANGELOG for additional upgrade links.
Paths
By default, Shakapacker ships with simple conventions for where the JavaScript app files and compiled webpack bundles will go in your Rails app. All these options are configurable from config/webpacker.yml file.
The configuration for what webpack is supposed to compile by default rests on the convention that every file in app/javascript/(default) or whatever path you set for source_entry_path in the webpacker.yml configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put any file inside app/javascript directory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "app/javascript/" directory and keep everything else under subdirectories like app/javascript/controllers.
Suppose you want to change the source directory from app/javascript to frontend and output to assets/packs. This is how you would do it:
# config/webpacker.ymlsource_path:frontend# packs are the files in frontend/public_output_path:assets/packs# outputs to => public/assets/packs
Similarly you can also control and configure webpack-dev-server settings from config/webpacker.yml file:
If you have hmr turned to true and inline_css is not false, then the stylesheet_pack_tag generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the stylesheet_pack_tag will create the appropriate HTML tags.
If you want to have HMR and separate link tags, set hmr: true and inline_css: false. This will cause styles to be extracted and reloaded with the mini-css-extract-plugin loader. Note that in this scenario, you do not need to include style-loader in your project dependencies.
Additional paths
If you are adding Shakapacker to an existing app that has most of the assets inside app/assets or inside an engine, and you want to share that with webpack modules, you can use the additional_paths option available in config/webpacker.yml. This lets you
add additional paths that webpack should look up when resolving modules:
additional_paths: ['app/assets', 'vendor/assets']
You can then import these items inside your modules like so:
// Note it's relative to parent directory i.e. app/assetsimport'stylesheets/main'import'images/rails.png'
Note: Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of whole parent directory if you just need to reference one or two modules
Also note: While importing assets living outside your source_path defined in webpacker.yml (like, for instance, assets under app/assets) from within your packs using relative paths like import '../../assets/javascripts/file.js' will work in development, Shakapacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out Webpacker::MtimeStrategy#latest_modified_timestamp or Webpacker::DigestStrategy#watched_files_digest depending on strategy configured by compiler_strategy option in webpacker.yml). That's why you'd need to add app/assets to the additional_paths as stated above and use import 'javascripts/file.js' instead.
Deployment
Shakapacker hooks up a new webpacker:compile task to assets:precompile, which gets run whenever you run assets:precompile. If you are not using Sprockets, webpacker:compile is automatically aliased to assets:precompile. Similar to sprockets both rake tasks will compile packs in production mode but will use RAILS_ENV to load configuration from config/webpacker.yml (if available).
This behavior is optional & can be disabled by either setting an WEBPACKER_PRECOMPILE environment variable to false, no, n, or f, or by setting a webpacker_precompile key in your webpacker.yml to false. (source code)
When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use yarn install --frozen-lockfile to install NPM packages on the remote host to ensure that the installed packages match the yarn.lock file.
If you are using a CDN setup, webpacker will use the configured asset host value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the WEBPACKER_ASSET_HOST environment variable.
The following companies support our open source projects, and ShakaCode uses their products!
[v6.5.4] - November 4, 2022
Fixed
Fixed regression caused by 6.5.3. PR #192 introduce extra split() call. PR 202 by Eric-Guo.
FAQs
Use webpack to manage app-like JavaScript modules in Rails
The npm package shakapacker receives a total of 101,088 weekly downloads. As such, shakapacker popularity was classified as popular.
We found that shakapacker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 0 open source maintainers collaborating on the project.
Package last updated on 04 Nov 2022
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.
Socket researchers found a malicious Maven package impersonating the legitimate ‘XZ for Java’ library, introducing a backdoor for remote code execution.