<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Involution &#187; Capistrano</title>
	<atom:link href="http://involution.com/category/capistrano/feed/" rel="self" type="application/rss+xml" />
	<link>http://involution.com</link>
	<description>Tony Perrie&#039;s Weblog</description>
	<lastBuildDate>Tue, 15 May 2012 20:48:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using YUICompressor with Capistrano and Rails 2.3+ on Combined Javascript and CSS</title>
		<link>http://involution.com/2009/06/18/using-yuicompressor-with-capistrano-and-rails-2-3-on-combined-javascript-and-css/</link>
		<comments>http://involution.com/2009/06/18/using-yuicompressor-with-capistrano-and-rails-2-3-on-combined-javascript-and-css/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 15:20:49 +0000</pubDate>
		<dc:creator>Tony Perrie</dc:creator>
				<category><![CDATA[Capistrano]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[YUICompressor]]></category>

		<guid isPermaLink="false">http://involution.com/?p=1225</guid>
		<description><![CDATA[YUICompressor is a standalone Javascript and CSS minifier from the YUI folks. It&#8217;s fairly awesome in that it does deep analysis of Javascript using Rhino (a standalone Javascript interpreter). Most minifiers take the low road and merely remove spaces and newlines, and, if you&#8217;re lucky maybe shorten the variable names. YUICompressor one-ups them all because [...]]]></description>
			<content:encoded><![CDATA[<p>YUICompressor is a standalone Javascript and CSS minifier from the YUI folks.  It&#8217;s fairly awesome in that it does deep analysis of Javascript using Rhino (a standalone Javascript interpreter).  Most minifiers take the low road and merely remove spaces and newlines, and, if you&#8217;re lucky maybe shorten the variable names.  YUICompressor one-ups them all because it actually parses all your JS and aggressively optimizes it for download speed.  </p>
<h3>CSS and Javascript Conflation</h3>
<p>The problem is most of the guides out there for integrating YUICompressor with Capistrano and Rails are woefully out-of-date.  The main issue is that Rails now has built-in support for combining all JS files into a single file.  So, there&#8217;s no need to manually do this in Capistrano these days.  </p>
<p>You can make Rails combine automatically combine your Javascript by adding a <code>:cache => "cache/all"</code> to your <code>javascript_include_tag</code> and <code>stylesheet_link_tag</code> in your layout. </p>
<pre><code><%= stylesheet_link_tag 'one', 'two', :cache => "cache/all" %>
<%= javascript_include_tag 'jquery', 'ninja',  :cache => "cache/all" %>
</code></pre>
<h3>Priming the JS and CSS Caches</h3>
<p>The Capistrano stuff is a bit tricky because Rails doesn&#8217;t generate the Javascript and CSS until after the app has been visited for the first time.  So, you have to visit each of your web servers after restart during your deploy.</p>
<pre><code>task :after_restart, :roles => :web do
  desc "Visiting each web server"
  sleep(5)  # Wait for passenger to fully spin-up
  run "/usr/bin/wget -O- http://127.0.0.1 >/dev/null 2>&#038;1"
end
</code></pre>
<h3>Invoking YUICompressor</h3>
<p>Now, we can invoke YUICompressor after <code>after_restart</code>.  The previous <code>after_restart</code> task automatically gets invoked after the web servers are restarted, however, the compression needs to happen after that.  So, you have to manually chain a <code>minify</code> just after <code>after_restart</code>.</p>
<pre><code>after "deploy:after_restart", "deploy:minify"</code></pre>
<p>The actual task is quite simple for running the compressor itself.  It&#8217;s just a normal Capistrano task. </p>
<pre><code>task :minify, :roles => :web do
  desc "Minify JS and CSS Using YUICompressor"
  javascript = "#{current_path}/public/javascripts/cache/all.js"
  stylesheet = "#{current_path}/public/stylesheets/cache/all.css"
  compressor = "java -jar /usr/lib/java/yuicompressor-2.4.2.jar"
  run "#{compressor} --type js #{javascript} -o #{javascript}"
  run "#{compressor} --type css #{stylesheet} -o #{stylesheet}"
end
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://involution.com/2009/06/18/using-yuicompressor-with-capistrano-and-rails-2-3-on-combined-javascript-and-css/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

