Is it possible to display a js file using Rails and minimize output?

I am generating some js output for each user using a template called example.js.erb . This is basically a javascript file in which I must specify the unique details of the user. I use this inside the widget for the user.

Is there a way to minimize js when rendering it? Currently javascript is processing correctly, but it has a long form, and I would like to minimize the output in order to reduce the file size and increase the download speed.

+7
source share
3 answers

Rails Javascript compression / minimization to answer answer_to javascript?

 respond_to do |format| format.js { self.response_body = minify(render_to_string) } end 
+4
source

Try something like this in the controller:

 def action_name js_code = render_to_string 'your_js_code_view' js_code.gsub!(/\r\n|\r|\n/, '') #and/or any other minimization you like render 'the_view_wich_will_show_the_minimized_js_code' end 
0
source

For Rails 4:

 render js: Uglifier.new.compile(render_to_string) 

For Rails 5:

 render js: Uglifier.new(harmony: true).compile(render_to_string) 
0
source

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


All Articles