[Rails] Javascript compression / How to hook in to rails development

Stephan Wehner stephan at buckmaster.ca
Sun Aug 13 16:50:29 GMT 2006


Hi there,

I thought it's rather neat to use the javascript compression from

http://dojotoolkit.org/docs/compressor_system.html

This removes javascript comments and renames variables and seems pretty 
reliable and effective.

For example, I keep the original htmlarea.js file from 
http://xinha.gogo.co.nz/
 in a file htmlarea.js.txt and it compresses from 200K to 100K. With 
gzip it compresses further down to 27K as opposed to 50K without the 
intermediate custom_rhino compression (hopefully I can make use of 
Apache 2.0 mod_deflate soon.)

So I set up a Rake task for all my javascript files

FILE lib/tasks/compress_javascript.rake

task :javascript do
  {
    'public/stb.js' => 0,
    'public/tabs.js' => 23,
    'public/xinha/htmlarea.js' => 33,
    'public/javascripts/prototype.js' => 11,
    'public/javascripts/controls.js' => 21,
    'public/javascripts/effects.js' => 23,
    'public/calendar-dateparse.js' => 19,
  }.each do |filename, copyrightlength|
           to_compress = filename + '.txt'
           compress_out = filename
           unless uptodate?(compress_out, to_compress)
             puts to_compress.to_s + " -> " + compress_out
             if copyrightlength>0
               %x{ echo "// Adapted for stephansmap.org and compressed 
with custom_rhino from #{to_compress.gsub(/public/,'')}" > 
#{compress_out}; head -#{copyrightleng
th} #{ to_compress } >> #{compress_out}};
             else
               %x{ echo '' > #{ compress_out } }
             end
             %x{ java -jar extra/custom_rhino.jar -c #{ to_compress } 
>> #{ compress_out } 2>&1  }

           end
         end
end

END FILE

Now, how can I have this task executed during development? When I change 
one of the js.txt files and refresh a page, it would be nice to have 
this compression executed so I am looking at the site based on new 
javascript code the same way I can change controller ruby code and the 
changes are reflected on each access.


Stephan

-- 
Posted via http://www.ruby-forum.com/.


More information about the Rails mailing list