Ive spent endless hours integrating browser rails in my project and creating JS routes as part of this setup ...
The solution I came to and described below is the result of not being able to get Sprockets to pre-process the route file before Browserify appears. I spent quite a bit of time both on the source code of the browser rails and on the stars, but could not find a way to turn around and make each component act in the correct order for this to work.
So, I decided to use the Rails hook to create the complete JS file βmanuallyβ in the development environment, so the routes are always updated with the latest Rails route files. Then I assume that the JS file routes will be updated when I click on production.
When doing this download in the environment, make sure that the JS file is ready before Sprockets / Browsify chime in appears: for them it is just another simple JS file.
Here is the code to include in development.rb
:
ActionDispatch::Reloader.to_prepare do Rails.application.reload_routes! if JsRoutes.assert_usable_configuration! JsRoutes.generate!(Rails.root.join('app/assets/javascripts/routes.js')) end end
You always need to reload the routes, otherwise the generated file will always be the state of the last of the last Rails file. I never understood why ...
In my application.js
I simply deleted all the //=
directives, but jQuery (to keep the global jQuery available), and used the require
method for all other modules so that browsers would select the files I want to include.
So this is a bit hacky, but it works.
I would be interested to know if anyone with the best knowledge of the Sprockets pipeline can get a better solution?
source share