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.
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.
See an example migration here: PR 27.
If you're on webpacker v5, follow below steps 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 - 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 theconfig/webpack/RAILS_ENV.jsconst { 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
-
Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want.
-
Consider changing from the v5 default for
source_entry_pathinwebpacker.yml.source_path: app/javascript source_entry_path: packs
consider changing to the v6 default:
source_path: app/javascript source_entry_path: /
Then consider moving your
app/javascript/packs/*(includingapplication.js) toapp/javascript/and updating the configuration file.Note, moving your files is optional, as you can stil keep your entries in a separate directory, called something like
packs, orentries. This directory is defined within the source_path. -
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 new 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
shakapackergem and package. (Gems use a hyphen and packages use a dot between the main version number and the beta version.)Example going to a specific version:
# Gemfile gem 'shakapacker', '6.0.0.rc.13'
bundle install
yarn add shakapacker@6.0.0-rc.13 --exact
bundle exec rails 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: packs
The v6 default uses the top level directory.
source_path: app/javascript source_entry_path: /
If you prefer this configuratiom, 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 stil keep your entries in a separate directory, called something like
packs, orentries. This directory is defined with thesource_path. -
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 new 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.
-
Update
webpack-dev-serverto the current version, greater than 4.2, updatingpackage.json. -
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-loader,by 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>where might 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 bin/rails assets:precompile. If all goes well, don't forget to clean the generated assets withbin/rails 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