VP.
VP.

Reputation: 5141

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

Upvotes: 1

Views: 5560

Answers (1)

Steve Ross
Steve Ross

Reputation: 4144

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

Upvotes: 1

Related Questions