Webpacker: including javascript from gem (js-routes)

I am trying to use JS route gem with webpacker from Rails 5.1 but cannot figure out how to include js-routes.js.erb

in webpack app/javascript/packs/application.js

.

import 'js-routes'

leads to

Uncaught Error: Cannot find module "js-routes"

Which probably means the webpack cannot find the javascript in the included gem. This is probably due to this issue a problem with the github website .

What's the best solution for this problem now?

Thank!

+3


source to share


1 answer


Using the technique outlined in very advanced setup , part of the JsRoutes documentation:

// app/javascript/routes.js.erb

<%= JsRoutes.generate %>
export default this.Routes

      



And then in your app bundle:

// app/javascript/packs/application.js

import Routes from '../routes.js.erb'
// Note the .erb extension!

// If you want it to be available globally for some reason:
window.Routes = Routes

      

+2


source







All Articles