You are here

function _javascript_aggregator_minify in Javascript Aggregator 6

Helper function to minify and gzip files.

2 calls to _javascript_aggregator_minify()
javascript_aggregator_preprocess_page in ./javascript_aggregator.module
Implementation of hook_preprocess_hook().
phptemplate_closure in ./javascript_aggregator.module
Implementation of theme_closure().

File

./javascript_aggregator.module, line 98

Code

function _javascript_aggregator_minify($scripts) {

  // Only process it is JavaScript Optimization is enabled.
  if (variable_get('preprocess_js', 0)) {

    // Strip out the aggregated JavaScript file.
    $path_to_files_directory = base_path() . file_directory_path();
    $pattern = "!(<script type=\"text\\/javascript\" src=\"(.*?){$path_to_files_directory})(.*?)(\"(.*?)><\\/script>)!";
    if (preg_match_all($pattern, $scripts, $matches) > 0) {
      $aggregated_file_name = $matches[3][0];
      $jsmin_file_name = $aggregated_file_name . 'min.js';

      // Construct the final JSMin file path.
      $jsmin_file_path = file_directory_path() . $jsmin_file_name;

      // Create the JSMinified file if it doesn't exist yet.
      if (!file_exists($jsmin_file_path)) {
        if (variable_get('javascript_aggregator_jsminplus', FALSE)) {

          // JSMin+ the contents of the aggregated file.
          require_once drupal_get_path('module', 'javascript_aggregator') . '/jsminplus.php';

          // Strip Byte Order Marks (BOM's) from the file, JSMin+ cannot parse these.
          $file = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", file_get_contents(file_directory_path() . $aggregated_file_name));
          $contents = JSMinPlus::minify($file);
        }
        else {

          // JSMin the contents of the aggregated file.
          require_once drupal_get_path('module', 'javascript_aggregator') . '/jsmin.php';
          $contents = JSMin::minify(file_get_contents(file_directory_path() . $aggregated_file_name));
        }

        // Code comments containing copyright notices and licensing information
        // are stripped when the file is minified. GPL and most other open
        // source licenses require the license text to be included whenever the
        // file is distributed, so include a reference to the un-minified file.
        $contents = '// Minified using Javascript Aggregator - see ' . $path_to_files_directory . $aggregated_file_name . " for original source including licensing information.\n" . $contents;

        // GZip the JavaScript if required.
        $htaccess = file_directory_path() . '/js/.htaccess';
        if (variable_get('javascript_aggregator_gzip', FALSE)) {

          // Create the GZip file if it doesn't already exist.
          if (!file_exists($jsmin_file_path . '.gz')) {
            file_save_data(gzencode($contents, 9), $jsmin_file_path . '.gz', FILE_EXISTS_REPLACE);
          }

          // Make sure the .htaccess file is active to handle GZipped JavaScript files.
          if (!variable_get('javascript_aggregator_no_htaccess', FALSE) && !file_exists($htaccess)) {
            $rewrite_base = base_path() . file_directory_path() . '/js/';
            $htaccess_contents = <<<EOT
<Files *.js.gz>
AddEncoding x-gzip .gz
ForceType text/javascript
</Files>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase {<span class="php-variable">$rewrite_base</span>}
RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)\\.js \$1.js.gz [L,QSA]
</IfModule>
EOT;
            file_save_data($htaccess_contents, $htaccess, FILE_EXISTS_REPLACE);
          }
        }
        else {

          // Delete .htaccess file so *.gz files do not get served.
          if (file_exists($htaccess)) {
            file_delete($htaccess);
          }
        }

        // Save the contents to the JavaScript file.
        file_save_data($contents, $jsmin_file_path, FILE_EXISTS_REPLACE);
      }

      // Replace the aggregated file with the minified JavaScript file.
      $scripts = str_replace($aggregated_file_name, $jsmin_file_name, $scripts);
    }
  }
  return $scripts;
}