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