diff --git a/src/Admin.php b/src/Admin.php index 037bbab..9d1bc1a 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -78,11 +78,12 @@ public function run(?string $locale, string $path): void } if (($assetType = ($path === 'assets/core.js') ? 'js' : null) || ($assetType = ($path === 'assets/core.css') ? 'css' : null)) { // route static assets from template directory header('Content-Type: ' . Proxy::CONTENT_TYPES[$assetType]); + $assetContent = file_get_contents(__DIR__ . '/../template/assets/core.' . $assetType) + . (($customAssetPath = $this->context->getCustomAssetPath($assetType)) !== null ? "\n\n" . file_get_contents($customAssetPath) : ''); echo '/*' . "\n" . ' * This file is part of Baraja CMS.' . "\n" . ' */' . "\n\n" - . file_get_contents(__DIR__ . '/../template/assets/core.' . $assetType) - . (($customAssetPath = $this->context->getCustomAssetPath($assetType)) !== null ? "\n\n" . file_get_contents($customAssetPath) : ''); + . ($assetType === 'css' ? Helpers::minifyHtml($assetContent) : $assetContent); die; } if (strncmp($path, 'cms/', 4) !== 0 && $this->context->getUser()->isLoggedIn() === false) { // route login form diff --git a/src/Helpers.php b/src/Helpers.php index 273fb9d..75f2c5c 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -288,13 +288,17 @@ public static function escapeHtmlComment(string $s): string public static function minifyHtml(string $haystack): string { - return (string) preg_replace_callback( + $return = (string) preg_replace_callback( '#[ \t\r\n]+|<(/)?(textarea|pre)(?=\W)#i', - static function (array $match) { - return empty($match[2]) ? ' ' : $match[0]; - }, + fn (array $match): string => empty($match[2]) ? ' ' : $match[0], $haystack ); + $return = (string) preg_replace('/(\w|;)\s+({|})\s+(\w|\.|#)/', '$1$2$3', $return); + $return = str_replace(';}', '}', $return); + $return = (string) preg_replace('/(\w)\s*:\s+(\w|#|-|.)/', '$1:$2', $return); + $return = (string) preg_replace('/\s*\/\*+[^\*]+\*+\/\s*/', '', $return); + + return $return; }