ENHANCEMENT: allow overridable JS/CSS minification

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.
This commit is contained in:
Jeremy Thomerson 2013-06-04 12:47:44 +00:00
parent 358988e9c6
commit 4c0b452c0f

View File

@ -408,6 +408,14 @@ class Requirements_Backend {
*/ */
public $combine_js_with_jsmin = true; 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. * @var string By default, combined files are stored in assets/_combinedfiles.
* Set this by calling Requirements::set_combined_files_folder() * Set this by calling Requirements::set_combined_files_folder()
@ -1052,17 +1060,15 @@ class Requirements_Backend {
$combinedData = ""; $combinedData = "";
foreach(array_diff($fileList, $this->blocked) as $file) { foreach(array_diff($fileList, $this->blocked) as $file) {
$fileContent = file_get_contents($base . $file); $fileContent = file_get_contents($base . $file);
// if we have a javascript file and jsmin is enabled, minify the content $fileContent = $this->minifyFile($file, $fileContent);
$isJS = stripos($file, '.js');
if($isJS && $this->combine_js_with_jsmin) {
require_once('thirdparty/jsmin/jsmin.php');
increase_time_limit_to(); if ($this->write_header_comment) {
$fileContent = JSMin::minify($fileContent); // 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 .= $fileContent . "\n";
$combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n".($isJS ? ';' : '')."\n";
} }
$successfulWrite = false; $successfulWrite = false;
@ -1087,6 +1093,19 @@ class Requirements_Backend {
$this->css = $newCSSRequirements; $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() { public function get_custom_scripts() {
$requirements = ""; $requirements = "";