Cory O'Daniel – These are just words Software development, thoughts, and randomness

10Feb/100

A Rails rake file for compressing your Javascript with YUI

Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-
edition).

This of course should be used with the Ruby YUI Compressor I posted about the other day.

This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'fileutils'
require 'pathname'
 
namespace :js do
  desc <<-INFO
Output Compressed JavaScript to STDOUT; 
  COMPRESS=ALL to compress all Javascript files that DONT contain ".min." 
INFO
 
  task :yui => :environment do
    targz()
    if ENV['COMPRESS'] == 'ALL'
      Dir[Rails.root.to_s / :public / :javascripts / '**/*.js'].each do |javascript|
        next if javascript =~ /\.min\./
 
        compress(javascript)
      end
    else
      if File.exist?(Rails.root + "config/yui.yml")
        javascripts = YAML.load(File.open("config/yui.yml").read)["javascripts"]
 
        if !javascripts.blank?
          javascripts.each {|script| compress(Rails.root + "public/javascripts" + script)}
        else
          raise Exception, "No javascript files in config/yui.yml" 
        end
      else
        raise Exception, "config/yui.yml Not Found; Do rake js:generate to create one or COMPRESS=ALL to run without a config file"
      end
    end
  end
 
  desc "Generate YUI Compressor Config file"
  task :generate => :environment do
    config_path = Rails.root + "config/yui.yml"
    File.open(config_path, "w+") do |f|
      f.puts <<-CONFIG
---
javascripts:
  - "application.js"
  - "jquery.js"
CONFIG
      puts config_path
    end
  end
 
  def targz()
    tgz_file = "#{Time.now.to_i}.javascripts.tgz"
    `cd #{Rails.root + "public"}; tar -zcf #{tgz_file} javascripts/`
    puts "Backed up assets to: #{tgz_file}"
  end
 
  def compress(path)
    puts "Compressing #{path}"
    file_handle = File.open(path)
    compressed_output = YUI.compress_safe file_handle
    file_handle.close
 
    #overwrite the file
    File.open(path, "w+") { |file| file.puts compressed_output }
  end
 
end

Wanna use it?
1. Drop it in your lib/tasks folder or wherever your Rakefile looks

You can compress 'everything' or specific files.

If you want to only compress specific Javascript files do:

1
2
3
rake js:generate 
# Edit your config/yui.yml file
rake js:yui

If you want to compress "everything" (It actually won't compress files that contain ".min.". You can remove the regexp's if you don't like it.)

1
rake js:yui COMPRESS=ALL

Yay, now your raking it in (Pun harhar) w/ your web2.0 yui compressed rails app. Woot woot.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tagged as: , , , No Comments