Minify CSS and JS
This commit is contained in:
parent
c07b714a0d
commit
f168cf6863
73
combine.php
73
combine.php
|
@ -1,11 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
$cache = true;
|
$cache = true;
|
||||||
$cachedir = dirname(__FILE__) . '/assets/cache';
|
$cachedir = dirname(__FILE__) . '/assets/cache';
|
||||||
$cssdir = dirname(__FILE__) . '/assets/css';
|
$cssdir = dirname(__FILE__) . '/assets/css';
|
||||||
$jsdir = dirname(__FILE__) . '/assets/js';
|
$jsdir = dirname(__FILE__) . '/assets/js';
|
||||||
|
|
||||||
// Determine the directory and type we should use
|
// Determine the directory and type we should use
|
||||||
switch ($_GET['type']) {
|
switch ($_GET['type']) {
|
||||||
case 'css':
|
case 'css':
|
||||||
$base = realpath($cssdir);
|
$base = realpath($cssdir);
|
||||||
break;
|
break;
|
||||||
|
@ -15,14 +15,14 @@ switch ($_GET['type']) {
|
||||||
default:
|
default:
|
||||||
header("HTTP/1.0 503 Not Implemented");
|
header("HTTP/1.0 503 Not Implemented");
|
||||||
exit;
|
exit;
|
||||||
};
|
};
|
||||||
|
|
||||||
$type = $_GET['type'];
|
$type = $_GET['type'];
|
||||||
$elements = explode(',', $_GET['files']);
|
$elements = explode(',', $_GET['files']);
|
||||||
|
|
||||||
// Determine last modification date of the files
|
// Determine last modification date of the files
|
||||||
$lastmodified = 0;
|
$lastmodified = 0;
|
||||||
while (list(, $element) = each($elements)) {
|
while (list(, $element) = each($elements)) {
|
||||||
$path = realpath($base . '/' . $element);
|
$path = realpath($base . '/' . $element);
|
||||||
|
|
||||||
if (($type == 'javascript' && substr($path, -3) != '.js') ||
|
if (($type == 'javascript' && substr($path, -3) != '.js') ||
|
||||||
|
@ -37,18 +37,18 @@ while (list(, $element) = each($elements)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
$lastmodified = max($lastmodified, filemtime($path));
|
$lastmodified = max($lastmodified, filemtime($path));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send Etag hash
|
// Send Etag hash
|
||||||
$hash = $lastmodified . '-' . md5($_GET['files']);
|
$hash = $lastmodified . '-' . md5($_GET['files']);
|
||||||
header("Etag: \"" . $hash . "\"");
|
header("Etag: \"" . $hash . "\"");
|
||||||
|
|
||||||
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
||||||
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') {
|
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') {
|
||||||
// Return visit and no modifications, so do not send anything
|
// Return visit and no modifications, so do not send anything
|
||||||
header("HTTP/1.0 304 Not Modified");
|
header("HTTP/1.0 304 Not Modified");
|
||||||
header('Content-Length: 0');
|
header('Content-Length: 0');
|
||||||
} else {
|
} else {
|
||||||
// First time visit or files were modified
|
// First time visit or files were modified
|
||||||
if ($cache) {
|
if ($cache) {
|
||||||
// Determine supported compression method
|
// Determine supported compression method
|
||||||
|
@ -96,6 +96,9 @@ if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
||||||
while (list(, $element) = each($elements)) {
|
while (list(, $element) = each($elements)) {
|
||||||
$path = realpath($base . '/' . $element);
|
$path = realpath($base . '/' . $element);
|
||||||
$contents .= "\n\n" . file_get_contents($path);
|
$contents .= "\n\n" . file_get_contents($path);
|
||||||
|
$contents = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $contents);
|
||||||
|
$contents = str_replace(': ', ':', $contents);
|
||||||
|
$contents = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $contents);
|
||||||
// $contents = fn_minify_css($contents);
|
// $contents = fn_minify_css($contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,33 +125,33 @@ if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
define('MINIFY_STRING', '"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\'');
|
define('MINIFY_STRING', '"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\'');
|
||||||
define('MINIFY_COMMENT_CSS', '/\*[\s\S]*?\*/');
|
define('MINIFY_COMMENT_CSS', '/\*[\s\S]*?\*/');
|
||||||
define('MINIFY_COMMENT_HTML', '<!\-{2}[\s\S]*?\-{2}>');
|
define('MINIFY_COMMENT_HTML', '<!\-{2}[\s\S]*?\-{2}>');
|
||||||
define('X', "\x1A");
|
define('X', "\x1A");
|
||||||
|
|
||||||
function n($s)
|
function n($s)
|
||||||
{
|
{
|
||||||
return str_replace(["\r\n", "\r"], "\n", $s);
|
return str_replace(["\r\n", "\r"], "\n", $s);
|
||||||
}
|
}
|
||||||
|
|
||||||
function t($a, $b)
|
function t($a, $b)
|
||||||
{
|
{
|
||||||
if ($a && strpos($a, $b) === 0 && substr($a, -strlen($b)) === $b) {
|
if ($a && strpos($a, $b) === 0 && substr($a, -strlen($b)) === $b) {
|
||||||
return substr(substr($a, strlen($b)), 0, -strlen($b));
|
return substr(substr($a, strlen($b)), 0, -strlen($b));
|
||||||
}
|
}
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fn_minify($pattern, $input)
|
function fn_minify($pattern, $input)
|
||||||
{
|
{
|
||||||
return preg_split('#(' . implode('|', $pattern) . ')#', $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
return preg_split('#(' . implode('|', $pattern) . ')#', $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fn_minify_css($input, $comment = 2, $quote = 2)
|
function fn_minify_css($input, $comment = 2, $quote = 2)
|
||||||
{
|
{
|
||||||
if (!is_string($input) || !$input = n(trim($input))) return $input;
|
if (!is_string($input) || !$input = n(trim($input))) return $input;
|
||||||
$output = $prev = "";
|
$output = $prev = "";
|
||||||
foreach (fn_minify([MINIFY_COMMENT_CSS, MINIFY_STRING], $input) as $part) {
|
foreach (fn_minify([MINIFY_COMMENT_CSS, MINIFY_STRING], $input) as $part) {
|
||||||
|
@ -188,10 +191,10 @@ function fn_minify_css($input, $comment = 2, $quote = 2)
|
||||||
$prev = $part;
|
$prev = $part;
|
||||||
}
|
}
|
||||||
return trim($output);
|
return trim($output);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fn_minify_css_union($input)
|
function fn_minify_css_union($input)
|
||||||
{
|
{
|
||||||
if (stripos($input, 'calc(') !== false) {
|
if (stripos($input, 'calc(') !== false) {
|
||||||
// Keep important white–space(s) in `calc()`
|
// Keep important white–space(s) in `calc()`
|
||||||
$input = preg_replace_callback('#\b(calc\()\s*(.*?)\s*\)#i', function ($m) {
|
$input = preg_replace_callback('#\b(calc\()\s*(.*?)\s*\)#i', function ($m) {
|
||||||
|
@ -250,4 +253,4 @@ function fn_minify_css_union($input)
|
||||||
' '
|
' '
|
||||||
], $input);
|
], $input);
|
||||||
return trim(str_replace(X, ' ', $input));
|
return trim(str_replace(X, ' ', $input));
|
||||||
}
|
}
|
Reference in New Issue
Block a user