Greetings Rails and Javascript Gurus!
I have a project where I am returning a large javascript file in a
respond_to do |format|
format.js
end
block.
I am trying to figure out how I can minify or compress the .js response since the .js.erb view is full of comments and varies in size based on the results from the controller.
Anyone have any ideas?
well, maybe I have a solution:
This perfectly works. Of course that the key is the minify method. You will find a lot of JS minifiers around. For example you can use this one (well if license permits): http://github.com/thumblemonks/smurf/raw/master/lib/smurf/javascript.rb – it is based on Crockford’s jsmin.c.
If you put this file into your lib, require it, your minify method can look like this:
Hope that it helped you.
If you plan to do minifying automatically then you probably should go for a piece of middleware. Surprisingly I was not able to find any (there are many aimed to the CSS/JS but it’s about static assets not dynamic content) but it would not be such a problem to write it.