Requires a pre-processed chain file with browser and rails browser

I am using browserify-rails and I am trying to get stars to pre-process a file containing the stars directive, so when I require() using a browser, it will contain the generated JavaScript.

The sprockets directive tries to enable js-routes pearl output to allow me access to Rails routes from clients.


This is my setup (within app/assets/javascripts ):

 system/ rails_routes.js application.js 

application.js is the main file and it launches the rest of the application. I would like to be able to do something like

 var rr = require("./system/rails_routes.js"); 

and access the route object.


Inside system/react_routes.js , I have the following:

 //= require js-routes console.log("Does this work?"); 

(as an aside, I configured js-routes to put the output in an object called module.exports , so to match the CommonJS model, as described in railsware / JS routes # 121 )

The only problem is that when I look at the generated package, the sprockets directive still exists and has not been extended.

The console.log call also exists and is executed when I require() module.

Is there any way to make this work? What is the correct way for stars to pre-process a file before linking it using browser rails?

+6
source share
1 answer

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?

+8
source

Source: https://habr.com/ru/post/978470/


All Articles