Skip to content

Commit

Permalink
create-phar: added minification for JS & CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 1, 2014
1 parent eb9e779 commit 56a61a5
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion tools/create-phar/create-phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,43 @@
$phar->startBuffering();
foreach ($iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../src', RecursiveDirectoryIterator::SKIP_DOTS)) as $file) {
echo "adding: {$iterator->getSubPathname()}\n";
$phar[$iterator->getSubPathname()] = php_strip_whitespace($file);
$s = php_strip_whitespace($file);

if (in_array($file->getExtension(), array('js', 'css'))) {
continue;

} elseif ($file->getExtension() === 'phtml') {
$s = preg_replace_callback('#<\?php (require |readfile\(|.*file_get_contents\().*(/\w+\.(js|css))\'\)* \?>#', function($m) use ($file) {
return file_get_contents($file->getPath() . $m[2]);
}, $s);
$s = preg_replace_callback('#(<(script|style).*>)(.*)(</)#Uis', function($m) {
list(, $begin, $type, $s, $end) = $m;

if (strpos($s, '<?php') !== FALSE) {
return $m[0];

} elseif ($type === 'script' && function_exists('curl_init')) {
$curl = curl_init('http://closure-compiler.appspot.com/compile');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'output_info=compiled_code&js_code=' . urlencode($s));
$s = curl_exec($curl);
curl_close($curl);

} elseif ($type === 'style') {
$s = preg_replace('#/\*.*?\*/#s', '', $s); // remove comments
$s = preg_replace('#[ \t\r\n]+#', ' ', $s); // compress space, ignore hard space
$s = preg_replace('# ([^0-9a-z.\#*-])#i', '$1', $s);
$s = preg_replace('#([^0-9a-z%)]) #i', '$1', $s);
$s = str_replace(';}', '}', $s); // remove leading semicolon
$s = trim($s);
}

return $begin . $s . $end;
}, $s);
}

$phar[$iterator->getSubPathname()] = $s;
}

$phar->stopBuffering();
Expand Down

0 comments on commit 56a61a5

Please sign in to comment.