Stephan Wehner
2006-Aug-13 17:06 UTC
[Rails] Javascript compression / How to hook in to rails development
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/.
Mohit Sindhwani
2006-Aug-13 18:16 UTC
[Rails] Javascript compression / How to hook in to rails development
Stephan Wehner wrote:> 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 >Sorry, this doesn''t relate to your question, but it''s worth pointing out that if people are using JS code created by others, they should be aware of ''license'' concerns. Many JS coders (as others) *require* that the license and acknowledgments remain in the Javascript file. It''s worth remembering that when using tools that strip comments :) Cheers, Mohit.
Michael Schuerig
2006-Aug-13 19:38 UTC
[Rails] Re: Javascript compression / How to hook in to rails development
On Sunday 13 August 2006 20:02, Mohit Sindhwani wrote:> Stephan Wehner wrote: > > Hi there, > > > > I thought it''s rather neat to use the javascript compression from > > > > http://dojotoolkit.org/docs/compressor_system.html[snip]> Sorry, this doesn''t relate to your question, but it''s worth pointing > out that if people are using JS code created by others, they should > be aware of ''license'' concerns. Many JS coders (as others) *require* > that the license and acknowledgments remain in the Javascript file. > It''s worth remembering that when using tools that strip comments :)I''m not really sure if this applies to compressed versions of a file, possibly bundled in a single file with other code. I tend to view this the same as the relationship between plain text source code and compiled binary code. While a programmer expects their copyright/license notice to be retained in the source code, in the binary it has vanished. Of course, combined and compressed JavaScript is not binary code, but it involves an inordinate amount of work to extract the original pieces in a fashion that they are individually recognizable and reusable. If you want to be super prudent, include all the licenses at the top of the compressed JavaScript code. For my taste it would be good enough -- and required -- to include them in the license notice of the enclosing package. Michael -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/
Stephan Wehner
2006-Aug-14 17:46 UTC
[Rails] Re: Javascript compression / How to hook in to rails develop
Mohit wrote:> Sorry, this doesn''t relate to your question, but it''s worth pointing out > that if people are using JS code created by others, they should be aware > of ''license'' concerns. Many JS coders (as others) *require* that the > license and acknowledgments remain in the Javascript file. It''s worth > remembering that when using tools that strip comments :)> Cheers, > Mohit.I addressed the license issues in the rake task, see the condition if copyrightlength>0 %x{ echo "// Adapted for stephansmap.org and compressed with custom_rhino from #{to_compress.gsub(/public/,'''')}" > #{compress_out}; head -#{copyrightlength} #{ to_compress } >> #{compress_out}}; The "head ..." instruction puts the original copyright notice at the top of the compressed file. I counted manually to see how many lines to copy over. Plus the top of the compressed file says how to obtain a copy of the file with my modifications ( append .txt to the filename ) I think that way the copyrights and licenses are respected! Stephan Stephan wrote:> > > 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/.
Mohit Sindhwani
2006-Aug-14 18:27 UTC
[Rails] Re: Javascript compression / How to hook in to rails develop
Stephan Wehner wrote:> Mohit wrote: > >> Sorry, this doesn''t relate to your question, but it''s worth pointing out >> that if people are using JS code created by others, they should be aware >> of ''license'' concerns. Many JS coders (as others) *require* that the >> license and acknowledgments remain in the Javascript file. It''s worth >> remembering that when using tools that strip comments :) >> > > >> Cheers, >> Mohit. >> > > I addressed the license issues in the rake task, see the condition > > if copyrightlength>0 > %x{ echo "// Adapted for stephansmap.org and compressed > with custom_rhino from #{to_compress.gsub(/public/,'''')}" > > #{compress_out}; head -#{copyrightlength} #{ to_compress } >> > #{compress_out}}; > > > The "head ..." instruction puts the original copyright notice at the top > of the compressed file. I counted manually to see how many lines to copy > over. Plus the top of the compressed file says how to obtain a copy of > the file with my modifications ( append .txt to the filename ) > > > I think that way the copyrights and licenses are respected! > > Stephan >Excellent!! Cheers, mohit.