As for webpacker
we need to add the js files separately from the gem, it may seem like kind of regression, as with sprockets we have added the files from the gem folder.
Webpack
brings us a lot of great features as well as some headaches.
And since webpacker
is a relatively new framework, many gems have not switched to a new format.
If the gem added support for webpack packed their js inside es6 modules (easy way)
we just install the js with Yarn, and require it as you mentioned above.
Let's see an example based on i18n-js gem.
in previous version (sprockets) we were needed to:
1 # add gem to the gemfile
=> gem "i18n-js"
2 # run gem install
=> gem install "i18n-js"
3 # require gem js inside aplication.js
=> //= require i18n.js
With webpacker
the same process will look like this:
1 # install the js with Yarn #
=> yarn add i18n-js
Three comments on this, first, as you may see i18n-js
is fully js gem, so there no need to add this to the gemfile
(rails ujs is additional fully js files that was previously inside the gemfile
), second, gems may content js, CSS, and ruby, with webpacker
we mostly getting rid of js gems types, and depend on how you handle the CSS, we may get rid of CSS types gems (like bootstrap gem
that is js and CSS gem) counter-intuitive, but we can handle and install modules\packges with Yarn that include js and CSS files, and webpack
is works well with both.
Although by convention it still suggested using sprockets for CSS, so we just set the routs to the node_modules when needed.
In the last third point, its always good practice to look at the yarn yarnpkg.com/package to see if the gem has a package we can add.
2 # added installed package to the pack\page you need the package
import I18n from "i18n-js"
3 # now you can set the language for the package
I18n.defaultLocal = 'he'
OK, but what if the gem has no module\packege?
it's only a little complicated here, depends on the case, sometimes you can just take the code from gem and add it through import inside your javascript folder, or if the code has no module you can use exports-loader
package to pack old js code to modules.
package,json
and require in yourapp/javascript/packs/application.js
. Also, you can explain your question with details: what gem you try to use? – cnnr