简体   繁体   中英

uglifier gem with rails 3.0

The rails 3.1 has as default a great gem named uglifier for js minification.

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. I searched already some info, but i couldn't not find any direction. Something that i found, in the rdoc is

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?

update:

i'm using barista and coffeescript, so maybe there is something that i can add to barista to generate the js already minified

Barista has hooks. If you look at your config/initializers/barista_config.rb, it tells you what all the hooks are. So, for example, I decided not to go with Jammit but instead minify using the JSMin gem. Inside my barista_config.rb I put this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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