From d5634cfe49dafa96d6f587909a7eccb02d20d624 Mon Sep 17 00:00:00 2001
From: Austin King <shout@ozten.com>
Date: Tue, 27 Mar 2012 05:18:05 -0700
Subject: [PATCH] A slight tweak on https://gist.github.com/1849619 cachify CSS
 while compressing it. Fixes Issue 620

---
 scripts/compress-worker.js | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/scripts/compress-worker.js b/scripts/compress-worker.js
index 0cf43ce21..3bcb9084e 100644
--- a/scripts/compress-worker.js
+++ b/scripts/compress-worker.js
@@ -1,6 +1,8 @@
 const
+cachify = require('connect-cachify'),
 fs = require('fs'),
 jsp = require("uglify-js").parser,
+logger = require('../lib/logging.js').logger,
 pro = require("uglify-js").uglify,
 uglifycss = require('uglifycss'),
 mkdirp = require('mkdirp'),
@@ -31,7 +33,8 @@ function compressResource(staticPath, name, files, cb) {
         final_code = pro.split_lines(pro.gen_code(ast), 32 * 1024); // compressed code here
       } else if (/\.css$/.test(name)) {
         // compress css
-        final_code = uglifycss.processString(orig_code);
+        var cach_code = cachify_embedded(orig_code);
+        final_code = uglifycss.processString(cach_code);
       } else {
         return cb("can't determine content type: " + name);
       }
@@ -78,6 +81,20 @@ function compressResource(staticPath, name, files, cb) {
   isBuildNeeded();
 }
 
+var static_root = path.join(__dirname, '..', 'resources/static/');
+logger.info("cachify will look in " + static_root);
+// Cachify only used in compress for CSS Images, so no asserts needed
+cachify.setup({}, { root: static_root});
+
+function cachify_embedded (css_src) {
+  return css_src.replace(/url\s*\(['"](.*)\s*['"]\s*\)/g, function (str, url) {
+    // This will throw an error if url doesn't exist. This is good as we will
+    // catch typos during build.
+    logger.info("For " + str + " making " + url + " into " + cachify.cachify(url));
+     return "url('" + cachify.cachify(url) + "')";
+  });
+}
+
 process.on('message', function(m) {
   var startTime = new Date;
 
-- 
GitLab