1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-12 09:05:08 +02:00

minify in chunks for JS

This commit is contained in:
Steve Clay
2011-10-13 22:58:50 -04:00
parent 3c5543c3ea
commit f67da8e359
3 changed files with 58 additions and 28 deletions

View File

@@ -487,10 +487,21 @@ class Minify {
? self::$_options['minifiers'][$type]
: false;
// minify each source with its own options and minifier, then combine.
// Here we used to combine all first but this was probably
// bad for PCRE performance, esp. in CSS.
foreach (self::$_controller->sources as $source) {
// process groups of sources with identical minifiers/options
$content = array();
$i = 0;
$l = count(self::$_controller->sources);
$groupToProcessTogether = array();
$lastMinifier = null;
$lastOptions = null;
do {
// get next source
$source = null;
if ($i < $l) {
$source = self::$_controller->sources[$i];
/* @var Minify_Source $source */
$sourceContent = $source->getContent();
// allow the source to override our minifier and options
$minifier = (null !== $source->minifier)
? $source->minifier
@@ -498,20 +509,40 @@ class Minify {
$options = (null !== $source->minifyOptions)
? array_merge($defaultOptions, $source->minifyOptions)
: $defaultOptions;
if ($minifier) {
self::$_controller->loadMinifier($minifier);
// get source content and minify it
}
// do we need to process our group right now?
if ($i > 0 // no, the first group doesn't exist yet
&& (
! $source // yes, we ran out of sources
|| $type === self::TYPE_CSS // yes, to process CSS individually (avoiding PCRE bugs/limits)
|| $minifier !== $lastMinifier // yes, minifier changed
|| $options !== $lastOptions) // yes, options changed
)
{
// minify previous sources with last settings
$imploded = implode($implodeSeparator, $groupToProcessTogether);
$groupToProcessTogether = array();
if ($lastMinifier) {
self::$_controller->loadMinifier($lastMinifier);
try {
$pieces[] = call_user_func($minifier, $source->getContent(), $options);
$content[] = call_user_func($lastMinifier, $imploded, $lastOptions);
} catch (Exception $e) {
throw new Exception("Exception in " . $source->getId() .
": " . $e->getMessage());
throw new Exception("Exception in minifier: " . $e->getMessage());
}
} else {
$pieces[] = $source->getContent();
$content[] = $imploded;
}
}
$content = implode($implodeSeparator, $pieces);
// add content to the group
if ($source) {
$groupToProcessTogether[] = $sourceContent;
$lastMinifier = $minifier;
$lastOptions = $options;
}
$i++;
} while ($source);
$content = implode($implodeSeparator, $content);
if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) {
$content = self::_handleCssImports($content);

View File

@@ -1,6 +1,5 @@
(function(){var
reMailto=/^mailto:my_name_is_(\S+)_and_the_domain_is_(\S+)$/,reRemoveTitleIf=/^my name is/,oo=window.onload,fixHrefs=function(){var i=0,l,m;while(l=document.links[i++]){if(m=l.href.match(reMailto)){l.href='mailto:'+m[1]+'@'+m[2];if(reRemoveTitleIf.test(l.title)){l.title='';}}}};window.onload=function(){oo&&oo();fixHrefs();};})();
;var MrClay=window.MrClay||{};MrClay.QueryString=function(){var parse=function(str){var assignments=str.split('&'),obj={},propValue;for(var i=0,l=assignments.length;i<l;++i){propValue=assignments[i].split('=');if(propValue.length>2||-1!=propValue[0].indexOf('+')||propValue[0]==''){continue;}
reMailto=/^mailto:my_name_is_(\S+)_and_the_domain_is_(\S+)$/,reRemoveTitleIf=/^my name is/,oo=window.onload,fixHrefs=function(){var i=0,l,m;while(l=document.links[i++]){if(m=l.href.match(reMailto)){l.href='mailto:'+m[1]+'@'+m[2];if(reRemoveTitleIf.test(l.title)){l.title='';}}}};window.onload=function(){oo&&oo();fixHrefs();};})();;var MrClay=window.MrClay||{};MrClay.QueryString=function(){var parse=function(str){var assignments=str.split('&'),obj={},propValue;for(var i=0,l=assignments.length;i<l;++i){propValue=assignments[i].split('=');if(propValue.length>2||-1!=propValue[0].indexOf('+')||propValue[0]==''){continue;}
if(propValue.length==1){propValue[1]=propValue[0];}
obj[unescape(propValue[0])]=unescape(propValue[1].replace(/\+/g,' '));}
return obj;};function construct_(spec){spec=spec||window;if(typeof spec=='object'){this.window=spec;spec=spec.location.search.substr(1);}else{this.window=window;}

View File

@@ -98,7 +98,7 @@ function test_Minify()
// test for Issue 73
Minify::setCache(null);
$expected = "\n;function h(){}";
$expected = ";function h(){}";
$output = Minify::serve('Files', array(
'files' => array(
$minifyTestPath . '/issue73_1.js'