Skip to content

Commit

Permalink
ENHANCEMENT: allow overridable JS/CSS minification
Browse files Browse the repository at this point in the history
This allows someone to extends Requirements_Backend and plug in their own minification
of files, including CSS minification.  It also allows them to override whether or not
the header comment is written for each file.
  • Loading branch information
jthomerson committed Jun 4, 2013
1 parent 358988e commit 4c0b452
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions view/Requirements.php
Expand Up @@ -408,6 +408,14 @@ class Requirements_Backend {
*/
public $combine_js_with_jsmin = true;

/**
* Setting for whether or not a file header should be written when
* combining files.
*
* @var boolean
*/
public $write_header_comment = true;

/**
* @var string By default, combined files are stored in assets/_combinedfiles.
* Set this by calling Requirements::set_combined_files_folder()
Expand Down Expand Up @@ -1052,17 +1060,15 @@ public function process_combined_files() {
$combinedData = "";
foreach(array_diff($fileList, $this->blocked) as $file) {
$fileContent = file_get_contents($base . $file);
// if we have a javascript file and jsmin is enabled, minify the content
$isJS = stripos($file, '.js');
if($isJS && $this->combine_js_with_jsmin) {
require_once('thirdparty/jsmin/jsmin.php');
$fileContent = $this->minifyFile($file, $fileContent);

increase_time_limit_to();
$fileContent = JSMin::minify($fileContent);
if ($this->write_header_comment) {
// write a header comment for each file for easier identification and debugging
// also the semicolon between each file is required for jQuery to be combinable properly
$combinedData .= "/****** FILE: $file *****/\n";
}
// write a header comment for each file for easier identification and debugging
// also the semicolon between each file is required for jQuery to be combinable properly
$combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n".($isJS ? ';' : '')."\n";

$combinedData .= $fileContent . "\n";
}

$successfulWrite = false;
Expand All @@ -1087,6 +1093,19 @@ public function process_combined_files() {
$this->css = $newCSSRequirements;
}

protected function minifyFile($filename, $content) {
// if we have a javascript file and jsmin is enabled, minify the content
$isJS = stripos($filename, '.js');
if($isJS && $this->combine_js_with_jsmin) {
require_once('thirdparty/jsmin/jsmin.php');

increase_time_limit_to();
$content = JSMin::minify($content);
}
$content .= ($isJS ? ';' : '') . "\n";
return $content;
}

public function get_custom_scripts() {
$requirements = "";

Expand Down

0 comments on commit 4c0b452

Please sign in to comment.