Index: javascript_aggregator.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/javascript_aggregator/javascript_aggregator.module,v
retrieving revision 1.15
diff -u -r1.15 javascript_aggregator.module
--- javascript_aggregator.module	19 Apr 2008 15:58:43 -0000	1.15
+++ javascript_aggregator.module	22 Aug 2008 19:49:08 -0000
@@ -10,6 +10,12 @@
  */
 
 /**
+ * Define the target file name for the JSMIN php implementation
+ * relative to the current module path.
+ */
+define('JSMIN_INCLUDE_FILE_NAME', 'jsmin.php');
+
+/**
  * Main function that finds .js files in $scripts.
  */
 function javascript_aggregator_cache($scripts) {
@@ -51,6 +57,7 @@
     }
 
     // Generate a unique filename for any set of js files
+    // TODO: should take into account last time settings were changed.
     $filename = md5(serialize(array($scripts_js_files, filemtime(__FILE__)))).'.js';
 
     // create files/js similar to drupal_build_css_cache (common.inc)
@@ -62,18 +69,28 @@
     array_unshift($scripts_js_links, base_path().$jsfile);
 
     if (!file_exists($jsfile)) {
+      // pull in jsmin if possible.
+      $jsmin = _javascript_aggregator_jsmin_exists();
+      if ($jsmin) {
+        require_once(_javascript_aggregator_jsmin_path());
+      }
+
       $contents = '';
 
       foreach ($scripts_js_files as $scripts_js_file => $filemtime) {
         $data = file_get_contents($scripts_js_file);
 
         if (variable_get('javascript_aggregator_optimize_js', 0)) {
-          // Perform some safe JS optimizations.
-          $data = preg_replace('<
+          if ($jsmin) {
+            $data = JSMin::minify($data);
+          } else {
+            // Perform some safe JS optimizations.
+            $data = preg_replace('<
             /\*.*?\*/
             >x', '\1', $data);
+          }
         }
-        $contents .= ";\n/* AGGREGATED JS FILE: $scripts_js_file */\n".$data."\n";
+        $contents .= ";\n/* AGGREGATED JS FILE: $scripts_js_file (JSMIN = $jsmin) */\n".$data."\n";
       }
       // Create the js file.
       file_save_data($contents, $jsfile, FILE_EXISTS_REPLACE);
@@ -100,6 +117,7 @@
     drupal_set_message(t('Javascript cache cleared.'), $type = 'status');
   }
   else {
+    drupal_set_message(file_create_path('js'));
     drupal_set_message(t('Javascript cache could not be cleared. Or already empty.'), $type = 'error');
   }
   drupal_goto('admin/settings/performance');
@@ -137,11 +155,11 @@
 
     $form['javascript_aggregation']['javascript_aggregator_optimize_js'] = array(
       '#type' => 'radios',
-      '#title' => t('Optimize JavaScript files'),
+      '#title' => t('Optimize JavaScript files using (JSMIN module if possible)'),
       '#default_value' => variable_get('javascript_aggregator_optimize_js', 0),
       '#disabled' => !$is_writable,
       '#options' => array(t('Disabled'), t('Enabled')),
-      '#description' => t("This option removes /* */ comments to reduce file size. If you encounter js errors disable this option."),
+      '#description' => t('This option removes /* */ comments to reduce file size. If the <a  target="_blank" href="http://code.google.com/p/jsmin-php/">JSMIN</a> module can be loaded from %jsmin, it will perform more <a href="http://www.crockford.com/javascript/jsmin.html" target="_blank">aggressive optimizations</a>.  If you encounter js errors disable this option.', array('%jsmin' =>_javascript_aggregator_jsmin_path())),
     );
     
     $form['javascript_aggregation']['javascript_aggregator_exclude_js'] = array(
@@ -183,3 +201,19 @@
   }
   return $items;
 }
+
+/**
+ * Return an absolute path to the jsmin php file
+ */
+
+function _javascript_aggregator_jsmin_path() {
+  return drupal_get_path('module', 'javascript_aggregator') . '/' . JSMIN_INCLUDE_FILE_NAME;
+}
+
+/**
+ * Return true if the specified jsmin include file exists
+ */
+
+function _javascript_aggregator_jsmin_exists() {
+  return file_exists(_javascript_aggregator_jsmin_path());
+}

