mirror of
https://github.com/mrclay/minify.git
synced 2025-08-13 09:34:54 +02:00
minify in chunks for JS
This commit is contained in:
@@ -487,10 +487,21 @@ class Minify {
|
|||||||
? self::$_options['minifiers'][$type]
|
? self::$_options['minifiers'][$type]
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// minify each source with its own options and minifier, then combine.
|
// process groups of sources with identical minifiers/options
|
||||||
// Here we used to combine all first but this was probably
|
$content = array();
|
||||||
// bad for PCRE performance, esp. in CSS.
|
$i = 0;
|
||||||
foreach (self::$_controller->sources as $source) {
|
$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
|
// allow the source to override our minifier and options
|
||||||
$minifier = (null !== $source->minifier)
|
$minifier = (null !== $source->minifier)
|
||||||
? $source->minifier
|
? $source->minifier
|
||||||
@@ -498,20 +509,40 @@ class Minify {
|
|||||||
$options = (null !== $source->minifyOptions)
|
$options = (null !== $source->minifyOptions)
|
||||||
? array_merge($defaultOptions, $source->minifyOptions)
|
? array_merge($defaultOptions, $source->minifyOptions)
|
||||||
: $defaultOptions;
|
: $defaultOptions;
|
||||||
if ($minifier) {
|
}
|
||||||
self::$_controller->loadMinifier($minifier);
|
// do we need to process our group right now?
|
||||||
// get source content and minify it
|
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 {
|
try {
|
||||||
$pieces[] = call_user_func($minifier, $source->getContent(), $options);
|
$content[] = call_user_func($lastMinifier, $imploded, $lastOptions);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new Exception("Exception in " . $source->getId() .
|
throw new Exception("Exception in minifier: " . $e->getMessage());
|
||||||
": " . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
} else {
|
} 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')) {
|
if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) {
|
||||||
$content = self::_handleCssImports($content);
|
$content = self::_handleCssImports($content);
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
(function(){var
|
(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();};})();
|
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;}
|
||||||
;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];}
|
if(propValue.length==1){propValue[1]=propValue[0];}
|
||||||
obj[unescape(propValue[0])]=unescape(propValue[1].replace(/\+/g,' '));}
|
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;}
|
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;}
|
||||||
|
@@ -98,7 +98,7 @@ function test_Minify()
|
|||||||
// test for Issue 73
|
// test for Issue 73
|
||||||
Minify::setCache(null);
|
Minify::setCache(null);
|
||||||
|
|
||||||
$expected = "\n;function h(){}";
|
$expected = ";function h(){}";
|
||||||
$output = Minify::serve('Files', array(
|
$output = Minify::serve('Files', array(
|
||||||
'files' => array(
|
'files' => array(
|
||||||
$minifyTestPath . '/issue73_1.js'
|
$minifyTestPath . '/issue73_1.js'
|
||||||
|
Reference in New Issue
Block a user