简体   繁体   English

带有rails 3.0的丑陋宝石

[英]uglifier gem with rails 3.0

The rails 3.1 has as default a great gem named uglifier for js minification. rails 3.1 默认有一个很棒的 gem,名为 uglifier 用于 js 缩小。

To enable it, we just have to call要启用它,我们只需要调用

config.assets.js_compressor  = :uglifier

I'm working in another project, rails 3.0, and I want to use as well the same gem.我正在另一个项目中工作,rails 3.0,我也想使用同一个 gem。 I searched already some info, but i couldn't not find any direction.我已经搜索了一些信息,但我找不到任何方向。 Something that i found, in the rdoc is我在 rdoc 中发现的东西是

require 'uglifier'
Uglifier.new.compile(File.read("source.js"))
# => js file minified

I can write a rake task maybe to execute it, it's fine, but there is any other way?我可以写一个 rake 任务来执行它,很好,但是还有其他方法吗?

update:更新:

i'm using barista and coffeescript, so maybe there is something that i can add to barista to generate the js already minified我正在使用咖啡师和 coffeescript,所以也许我可以向咖啡师添加一些东西来生成已经缩小的 js

Barista has hooks.咖啡师有钩子。 If you look at your config/initializers/barista_config.rb, it tells you what all the hooks are.如果你查看你的 config/initializers/barista_config.rb,它会告诉你所有的钩子是什么。 So, for example, I decided not to go with Jammit but instead minify using the JSMin gem.因此,例如,我决定不使用 Jammit 使用 go,而是使用 JSMin gem 进行缩小。 Inside my barista_config.rb I put this:在我的 barista_config.rb 里面我放了这个:

Barista.configure do |c|
  c.on_compilation do |path|
    if Rails.env.production?
      puts "+++ Barista: Compressing #{path} for production environment +++"
      compressible = File.read(path)
      File.open(path, 'w'){|f| f.write(JSMin.minify(compressible))}
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM