Rails Javascript压缩/缩小对respond_to javascript响应?

时间:2021-02-16 01:19:24

Greetings Rails and Javascript Gurus!

问候路由和Javascript大师!

I have a project where I am returning a large javascript file in a

我有一个项目,我将返回一个大的javascript文件

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.

我试图找出如何缩小或压缩.js响应,因为.js.erb视图充满了注释,并且根据控制器的结果大小不一。

Anyone have any ideas?

有人有主意吗?

3 个解决方案

#1


5  

well, maybe I have a solution:

好吧,也许我有一个解决方案:

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

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.

这非常有效。当然,关键是minify方法。你会发现很多JS minifiers。例如,你可以使用这个(如果许可证允许):http://github.com/thumblemonks/smurf/raw/master/lib/smurf/javascript.rb-它基于Crockford的jsmin.c。

If you put this file into your lib, require it, your minify method can look like this:

如果你把这个文件放到lib中,需要它,你的minify方法可能如下所示:

def minify(content)
  min = Smurf::Javascript.new(content)
  min.minified
end

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.

如果你计划自动缩小,那么你可能应该选择一个中间件。令人惊讶的是我找不到任何东西(有很多针对CSS / JS但它是关于静态资产而不是动态内容),但编写它并不是一个问题。

#2


12  

For Rails 4:

对于Rails 4:

render js: Uglifier.new.compile(render_to_string)

#3


3  

For rails 3 using the built in Uglifier method (the default for the assets pipeline)

对于使用内置Uglifier方法的rails 3(资产管道的默认方法)

See Radek's code above and just swap this in.

请参阅上面的Radek代码,然后将其交换。

  def minify(content)
    Uglifier.new.compile(content)
  end

#1


5  

well, maybe I have a solution:

好吧,也许我有一个解决方案:

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

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.

这非常有效。当然,关键是minify方法。你会发现很多JS minifiers。例如,你可以使用这个(如果许可证允许):http://github.com/thumblemonks/smurf/raw/master/lib/smurf/javascript.rb-它基于Crockford的jsmin.c。

If you put this file into your lib, require it, your minify method can look like this:

如果你把这个文件放到lib中,需要它,你的minify方法可能如下所示:

def minify(content)
  min = Smurf::Javascript.new(content)
  min.minified
end

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.

如果你计划自动缩小,那么你可能应该选择一个中间件。令人惊讶的是我找不到任何东西(有很多针对CSS / JS但它是关于静态资产而不是动态内容),但编写它并不是一个问题。

#2


12  

For Rails 4:

对于Rails 4:

render js: Uglifier.new.compile(render_to_string)

#3


3  

For rails 3 using the built in Uglifier method (the default for the assets pipeline)

对于使用内置Uglifier方法的rails 3(资产管道的默认方法)

See Radek's code above and just swap this in.

请参阅上面的Radek代码,然后将其交换。

  def minify(content)
    Uglifier.new.compile(content)
  end