The good news is that there is a webpack gem that will do all the work for us…
And after rails 5.1 you can just
rails new myapp --webpacker
but lets assume you have an existing app, the changes are a little bigger, but we can use both the old asset pipeline and the new webpacker.
Lets start adding the webpacker gem to the Gemfile
Then just run:
bundle install rails webpacker:install
var mylib = require('myjslibrary');
To add a library requirement use:
yarn add myjslibraryname
And do no forget to run in your deploy server:
After commiting the yarn.lock file of course, that file will make sure you have the same library versions in all the machines your project is running.
And last, but not least, do not forget to add the the script tag to call that file to your layout using the code:
rails webpacker:install:angular # Install everything needed for Angular rails webpacker:install:coffee # Install everything needed for Coffee rails webpacker:install:elm # Install everything needed for Elm rails webpacker:install:erb # Install everything needed for Erb rails webpacker:install:react # Install everything needed for React rails webpacker:install:stimulus # Install everything needed for Stimulus rails webpacker:install:typescript # Install everything needed for Typescript rails webpacker:install:vue # Install everything needed for Vue
Any of these shortcuts will install the required files to use the specified library in your existing rails app.
This is it for now, it is a good start I think.
Please comment any questions you have and I’ll answer it the fastest I can!