Upgrading from Webpacker v5 to Shakapacker v6
There are several substantial changes in Shakapacker v6 that you need to manually account for when coming from Webpacker 5. This guide will help you through it.
ShakaCode offers support for upgrading from webpacker or using Shakapacker. If interested, contact justin@shakacode.com.
Webpacker/Shakapacker has become a slimmer wrapper around Webpack
By default, Webpacker 6 is focused on compiling and bundling JavaScript. This pairs with the existing asset pipeline in Rails that's setup to transpile CSS and static images using Sprockets. For most developers, that's the recommended combination. But if you'd like to use Webpacker for CSS and static assets as well, please see integrations for more information.
Webpacker used to configure Webpack indirectly, which lead to a complicated secondary configuration process. This was done in order to provide default configurations for the most popular frameworks, but ended up creating more complexity than it cured. So now Webpacker delegates all configuration directly to Webpack's default configuration setup. Additionally, all major dependencies, like webpack and babel are now peer dependencies, so you are free to upgrade those.
While you have to configure integration with frameworks yourself, webpack-merge helps with this. See this example for Vue and scroll to the bottom for more examples.
webpacker v6.0.0.rc.6 to shakapacker v6.0.0
See an example migration here: PR 27.
Update to v6.5.2
- Remove setting the NODE_ENV in your
bin/webpackerandbin/webpacker-dev-serverbin stubs as these are not set in the webpack runner file.
Update Steps to v6.0.0 from v6.0.0.rc.6
If you're on webpacker v5, follow how to upgrade to webpacker v6.0.0.rc.6 from v5 to get to v6.0.0.rc.6 first.
-
Change the gem name from
webpackertoshakapackerand the NPM package from@rails/webpackertoshakapacker. -
Install the peer dependencies. Run
yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader compression-webpack-plugin terser-webpack-plugin webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server. You may have old versions of libraries. Runyarn installand check for warnings likewarning " > shakapacker@6.1.1" has incorrect peer dependency "compression-webpack-plugin@^9.0.0"andfile-loader@1.1.11" has incorrect peer dependency "webpack@^2.0.0 || ^3.0.0 || ^4.0.0". In other words, warnings like these are serious and will cause considerable confusion if not respected. -
Update any scripts that called
bin/webpackorbin/webpack-dev-servertobin/webpackerorbin/webpacker-dev-server -
Update your webpack config for a single config file,
config/webpack/webpack.config.js. If you want to use the prior style of having a separate file for each NODE_ENV, you can use this shim forconfig/webpack/webpack.config.js. WARNING, previously, if you did not setNODE_ENV,NODE_ENVdefaulted todevelopment. Thus, you might expectconfig/webpack/development.jsto run, but you'll instead be using the the configuration file that corresponds to yourRAILS_ENV. If yourRAILS_ENVistest, you'd be runningconfig/webpack/test.js.// name this file config/webpack/webpack.config.js
const { env, webpackConfig } = require("shakapacker")
const { existsSync } = require("fs")
const { resolve } = require("path")
const envSpecificConfig = () => {
const path = resolve(__dirname, `${env.nodeEnv}.js`)
if (existsSync(path)) {
console.log(`Loading ENV specific webpack configuration file ${path}`)
return require(path)
} else {
// Probably an error if the file for the NODE_ENV does not exist
throw new Error(`Got Error with NODE_ENV = ${env.nodeEnv}`)
}
}
module.exports = envSpecificConfig() -
Update
babel.config.jsif you need JSX support. See Customizing Babel Config -
If you are using the view helper called
asset_pack_path, change "media/" in path to "static/" or consider using theimage_pack_path.
How to upgrade to Webpacker v6.0.0.rc.6 from v5
-
Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want.
-
Ensure no nested directories in your
source_entry_path. Check if you had any entry point files in child directories of yoursource_entry_path. Files for entry points in child directories are not supported by shakacode/shakapacker v6. Move those files to the top level, adjusting any imports in those files.The v6 configuration does not allow nesting, so as to allow placing the entry points at in the root directory of JavaScript. You can find this change here.
-
Upgrade the Webpacker Ruby gem and the NPM package
Note: Check the gem page to verify the latest version, and make sure to install identical version numbers of
webpackergem and package.Example going to a specific version:
# Gemfile
gem 'webpacker', '6.0.0.rc.6'bundle installyarn add @rails/webpacker@6.0.0-rc.6 --exactbundle exec rake webpacker:installOverwrite all files and check what changed.
Note, the webpacker:install will install the peer dependencies:
yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader compression-webpack-plugin terser-webpack-plugin webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server -
Review the new default's changes to
webpacker.yml. Consider each suggested change carefully, especially the change to have yoursource_entry_pathbe at the top level of yoursource_path. The v5 default usedpacksforsource_entry_path:source_path: app/javascript
source_entry_path: packsThe v6 default uses the top level directory.
source_path: app/javascript
source_entry_path: /If you prefer this configuration, then you will move your
app/javascript/packs/*(includingapplication.js) toapp/javascript/and update the configuration file.Note, moving your files is optional, as you can still keep your entries in a separate directory, called something like
packs, orentries. This directory is defined with thesource_path. -
Update
webpack-dev-serverto the current version, greater than 4.2, updatingpackage.json.Important: If you encounter the error
[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schemawith an unknown property_assetEmittingPreviousFiles, this indicates your webpack-dev-server configuration contains deprecated options.To resolve this issue:
- Ensure you're using webpack-cli >= 4.7.0 with webpack-dev-server 5.x (webpack-cli 4.7+ is compatible with webpack-dev-server v5)
- Check your current versions:
npm list webpack-cli webpack-dev-server - Remove any legacy options like
_assetEmittingPreviousFilesfrom your dev-server configuration - Review the webpack-dev-server migration guide for proper v4 to v5 migration steps
See issue #526 for more details.
-
Update API usage of the view helpers by changing
javascript_packs_with_chunks_tagandstylesheet_packs_with_chunks_tagtojavascript_pack_tagandstylesheet_pack_tag. Ensure that your layouts and views will only have at most one call tojavascript_pack_tagand at most one call tostylesheet_pack_tag. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like issue 2932. If you expose jquery globally withexpose-loaderby usingimport $ from "expose-loader?exposes=$,jQuery!jquery"in yourapp/javascript/application.js, pass the optiondefer: falseto yourjavascript_pack_tag. -
If you are using any integrations like
css,postcss,ReactorTypeScript. Please see https://github.com/shakacode/shakapacker#integrations section on how they work in v6. -
config/webpack/environment.jswas changed toconfig/webpack/base.jsand exports a native webpack config so no need to calltoWebpackConfig. Usemergeto make changes:// config/webpack/base.js
const { webpackConfig, merge } = require("@rails/webpacker")
const customConfig = {
module: {
rules: [
{
test: require.resolve("jquery"),
loader: "expose-loader",
options: {
exposes: ["$", "jQuery"]
}
}
]
}
}
module.exports = merge(webpackConfig, customConfig) -
Copy over custom browserlist config from
.browserslistrcif it exists into the"browserslist"key inpackage.jsonand remove.browserslistrc. -
Remove
babel.config.jsif you never changed it. Configure yourpackage.jsonto use the default:"babel": {
"presets": [
"./node_modules/@rails/webpacker/package/babel/preset.js"
]
}See customization example the Customizing Babel Config for React configuration.
-
extensionswas removed from thewebpacker.ymlfile. Move custom extensions to your configuration by merging an object like this. For more details, see docs for Webpack Configuration{
resolve: {
extensions: [".ts", ".tsx", ".vue", ".css"]
}
} -
In
webpacker.yml, check if you hadwatched_paths. That is nowadditional_paths. -
Some dependencies were removed in PR 3056. If you see the error:
Error: Cannot find module 'babel-plugin-macros', or similar, then you need toyarn add <dependency>wheremight include: babel-plugin-macros,case-sensitive-paths-webpack-plugin,core-js,regenerator-runtime. Or you might want to remove your dependency on those. -
Review the new default's changes to
webpacker.ymlandconfig/webpack. Consider each suggested change carefully, especially the change to have yoursource_entry_pathbe at the top level of yoursource_path. -
Make sure that you can run
bin/webpackwithout errors. -
Try running
RAILS_ENV=production rake assets:precompile. If all goes well, don't forget to clean the generated assets withrake assets:clobber. -
Run
yarn add webpack-dev-serverif those are not already in your dev dependencies. Make sure you're using v4+. -
In
bin/webpackandbin/webpack-dev-server, The default NODE_ENV, if not set, will be the RAILS_ENV. Previously, NODE_ENV would default to development if not set. Thus, the old bin stubs would use the webpack config corresponding toconfig/webpack/development.js. After the change, ifRAILS_ENVistest, thenNODE_ENVistest. The final 6.0 release changes to using a singlewebpack.config.js. -
Now, follow the steps above to get to shakapacker v6 from webpacker v6.0.0.rc.6