17

How to migrate from webpacker to esbujsbundling-rails (esbuild)

 2 years ago
source link: https://dev.to/thomasvanholder/how-to-migrate-from-webpacker-to-jsbundling-rails-esbuild-5f2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1. Install jsbundling-rails

Add to gemfile:

gem 'jsbundling-rails'
Enter fullscreen modeExit fullscreen mode

In the terminal, run:

`bundle install`
`rails javascript:install:esbuild`
Enter fullscreen modeExit fullscreen mode

2. Swap pack_tag for include_tag

The jsbundling:install command inserts a javascript_include_tag tag above the tag in your application.html.erb file. This tag wil include the new javascript entrypoint javascript/application.js for your build script to be included in your application.

Remove the webpack's legacy stylesheet_pack_tag:

# old
<%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>

# new
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
Enter fullscreen modeExit fullscreen mode

If your app render multiple layouts, be sure to remove any javascript_pack_tag there too.


3. Import stimulus controllers

Even when you already installed stimulus, run the install command again in your terminal and overwrite any confliction changes. This will import all your existing stimulus controller correctly.

rails stimulus:install
Enter fullscreen modeExit fullscreen mode

Now, after adding or removing a new stimulus controller you can use a command which will auto-generate the controllers/index.js file.

rails stimulus:manifest:update
Enter fullscreen modeExit fullscreen mode

4. Migrate JS entrypoint

Move the content from javascript/packs/application.js to
javascript/application.js. After migrating the file, delete the javascript/packs folder which was used by Webpacker.

Make sure to import directories in the javascript folder with a relative path.

// old
require("channels")

//new
import "./channels"
import "./controllers"
Enter fullscreen modeExit fullscreen mode

5. Remove webpack

Webpack and its tentacles can finally be removed from the application.

A. Webpacker gem

gem 'webpacker', '~> 5.4'
Enter fullscreen modeExit fullscreen mode

B. Remove webpack packages

Webpack packages and plugins that accumulated over time can be removed too. For me, this included:

  • @rails/webpacker
  • webpack
  • webpack-cli
  • webpack-cli/serve
  • webpack-dev-server
  • clean-webpack-plugin
  • @hotwired/stimulus-webpack-helpers

C. Remove webpack files

Finally, remove all config and start-up files:

  • bin/webpack
  • bin/webpack-dev-server
  • config/webpacker.yml
  • config/webpack (directory)

6. Github Actions

If you use Github Actions as a CI/CD make sure to add in an additional build step to run yarn build. Yarn build will trigger the build step defined in your package.json file: "build": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds". All javascript files need to be bundled before running the tests in your workflow file.

- name: Build Esbuild
  run: yarn build
Enter fullscreen modeExit fullscreen mode

7. Heroku

If you use heroku to deploy your application, Heroku will NOT automatically install yarn as it does for Webpack!**

Therefore, we need to explicitly set a node buildpack before ruby. You can do this in the terminal or the Heroku Dashboard.

  • Terminal
heroku buildpacks:clear
heroku buildpacks:set heroku/nodejs
heroku buildpacks:add heroku/ruby
Enter fullscreen modeExit fullscreen mode
  • Dashboard Heroku Dashboard

Thanks for reading.

Feedback or question? Let me know :)

Resources:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK