<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -65,6 +65,7 @@ module Synthesis
       end
 
       def build_all
+        @@asset_host = Rails.configuration.action_controller.asset_host
         @@asset_packages_yml.keys.each do |asset_type|
           @@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).build }
         end
@@ -157,11 +158,53 @@ module Synthesis
         &quot;#{@asset_path}/#{source}.#{@extension}&quot;
       end
 
+      def replace_asset_urls(source, data)
+        if @asset_type == &quot;stylesheets&quot;
+          @@css_url_replacements ||= Hash.new
+
+          matches = data.scan(/url\([\s&quot;']*([^\)&quot;'\s]*)[\s&quot;']*\)/m).collect do |match|
+            match.first
+          end
+          
+          # build map of old to new, ensure if 2 files resolve to the same absolute path they get the same asset_host
+          matches.uniq.each do |match|
+            absolute_path = case match
+              when /^(http|https)\:\/\//
+                match
+              when /^\//
+                File.expand_path(&quot;#{RAILS_ROOT}/public#{match}&quot;)
+              else
+                File.expand_path(File.join(File.dirname(File.expand_path(source)), match))
+              end
+
+              @@css_url_replacements[absolute_path] = case absolute_path
+              when /^(http|https)\:\/\//
+                absolute_path
+              else
+                if @@asset_host.class == String
+                  host = (@@asset_host =~ /%d/) ? @@asset_host % (absolute_path.hash % 4) : @@asset_host
+                  host = host[0..-2] if host =~ /\/$/
+                else
+                  host = ''
+                end
+
+                absolute_path.sub(&quot;#{RAILS_ROOT}/public&quot;, host) + (File.exist?(absolute_path) ? '?' + File.mtime(absolute_path).to_i.to_s : '')
+              end unless @@css_url_replacements.has_key?(absolute_path)
+
+              unless absolute_path == @@css_url_replacements[absolute_path]
+                data.gsub!(match, @@css_url_replacements[absolute_path])
+              end
+          end
+        end
+
+        return data
+      end
+
       def merged_file
         merged_file = &quot;&quot;
         @sources.each {|s| 
           File.open(full_asset_path(s), &quot;r&quot;) { |f| 
-            merged_file += f.read + &quot;\n&quot; 
+            merged_file += replace_asset_urls(full_asset_path(s), f.read) + &quot;\n&quot; 
           }
         }
         merged_file</diff>
      <filename>lib/synthesis/asset_package.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,7 +5,7 @@ namespace :asset do
   namespace :packager do
 
     desc &quot;Merge and compress assets&quot;
-    task :build_all do
+    task :build_all =&gt; :environment do
       Synthesis::AssetPackage.build_all
     end
 </diff>
      <filename>tasks/asset_packager_tasks.rake</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>776957db704158d9f64dfcde328e14780724fb60</id>
    </parent>
  </parents>
  <author>
    <name>Erik St. Martin</name>
    <login>erikstmartin</login>
    <email>alakriti@gmail.com</email>
  </author>
  <url>http://github.com/erikstmartin/asset_packager/commit/3881654049a22770e51da9eb0380b61b27aee4c8</url>
  <id>3881654049a22770e51da9eb0380b61b27aee4c8</id>
  <committed-date>2009-06-16T14:07:28-07:00</committed-date>
  <authored-date>2009-06-16T14:07:28-07:00</authored-date>
  <message>added asset path fix so that relative paths are corrected to absolute paths during bundling. This will prevent broken image paths or the need to manually correct paths. Also makes use of asset_host if supplied by the rails environement performing the bundling</message>
  <tree>58febe1dda6920e216ae142b90d89df86ddae4a3</tree>
  <committer>
    <name>Erik St. Martin</name>
    <login>erikstmartin</login>
    <email>alakriti@gmail.com</email>
  </committer>
</commit>
