[core] DIY CSS/JS minifier

This commit is contained in:
Shish 2024-01-01 15:00:54 +00:00
parent 47dfc2c36e
commit 8815b702c5
3 changed files with 258 additions and 584 deletions

View file

@ -27,10 +27,6 @@
"reference" : "fd4ff50eb577457c1b7b887401663e91e77625ae" "reference" : "fd4ff50eb577457c1b7b887401663e91e77625ae"
} }
} }
},
{
"type" : "vcs",
"url" : "https://github.com/shish/php-css.git"
} }
], ],
@ -39,24 +35,22 @@
"ext-pdo": "*", "ext-pdo": "*",
"ext-json": "*", "ext-json": "*",
"ext-fileinfo": "*", "ext-fileinfo": "*",
"flexihash/flexihash": "^2.0",
"flexihash/flexihash" : "^2.0", "ifixit/php-akismet": "^1.0",
"ifixit/php-akismet" : "^1.0", "google/recaptcha": "^1.1",
"google/recaptcha" : "^1.1", "shish/eventtracer-php": "^2.0",
"shish/eventtracer-php" : "^2.0", "shish/ffsphp": "^1.3",
"shish/ffsphp" : "^1.3", "shish/microbundler": "^1.0",
"shish/microcrud" : "^2.0", "shish/microcrud": "^2.0",
"shish/microhtml" : "^2.2", "shish/microhtml": "^2.2",
"shish/gqla" : "dev-main", "shish/gqla": "dev-main",
"enshrined/svg-sanitize" : "^0.16", "enshrined/svg-sanitize": "^0.16",
"bower-asset/jquery": "^1.12",
"bower-asset/jquery" : "^1.12", "bower-asset/jquery-timeago": "^1.5",
"bower-asset/jquery-timeago" : "^1.5", "bower-asset/js-cookie": "^2.1",
"bower-asset/js-cookie" : "^2.1", "psr/simple-cache": "^1.0",
"psr/simple-cache" : "^1.0", "sabre/cache": "^2.0.1",
"sabre/cache" : "^2.0.1",
"naroga/redis-cache": "dev-master", "naroga/redis-cache": "dev-master",
"tbela99/css": "dev-master",
"aws/aws-sdk-php": "^3.294" "aws/aws-sdk-php": "^3.294"
}, },

773
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,8 +5,6 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use MicroHTML\HTMLElement; use MicroHTML\HTMLElement;
use TBela\CSS\Parser;
use TBela\CSS\Renderer;
require_once "core/event.php"; require_once "core/event.php";
@ -399,26 +397,11 @@ class BasePage
$css_md5 = md5(serialize($css_files)); $css_md5 = md5(serialize($css_files));
$css_cache_file = data_path("cache/style/{$theme_name}.{$css_latest}.{$css_md5}.css"); $css_cache_file = data_path("cache/style/{$theme_name}.{$css_latest}.{$css_md5}.css");
if (!file_exists($css_cache_file)) { if (!file_exists($css_cache_file)) {
// the CSS minifier causes a bunch of deprecation warnings, $mcss = new \MicroBundler\MicroBundler();
// so we turn off error reporting while it runs foreach($css_files as $css) {
$old_error_level = error_reporting(error_reporting(null) & ~E_DEPRECATED); $mcss->addSource($css, file_get_contents($css));
$parser = new Parser();
foreach($css_files as $file) {
$parser->append($file);
} }
$element = $parser->parse(); $mcss->save($css_cache_file);
// minified output
$renderer = new Renderer([
'compress' => true,
'convert_color' => 'hex',
'css_level' => 3,
'sourcemap' => true,
'allow_duplicate_declarations' => false,
'legacy_rendering' => true, // turn nested CSS into regular
]);
$renderer->save($element, $css_cache_file);
error_reporting($old_error_level);
} }
return $css_cache_file; return $css_cache_file;
@ -442,11 +425,11 @@ class BasePage
$js_md5 = md5(serialize($js_files)); $js_md5 = md5(serialize($js_files));
$js_cache_file = data_path("cache/script/{$theme_name}.{$js_latest}.{$js_md5}.js"); $js_cache_file = data_path("cache/script/{$theme_name}.{$js_latest}.{$js_md5}.js");
if (!file_exists($js_cache_file)) { if (!file_exists($js_cache_file)) {
$js_data = ""; $mcss = new \MicroBundler\MicroBundler();
foreach ($js_files as $file) { foreach($js_files as $js) {
$js_data .= file_get_contents($file) . "\n"; $mcss->addSource($js, file_get_contents($js));
} }
file_put_contents($js_cache_file, $js_data); $mcss->save($js_cache_file);
} }
return $js_cache_file; return $js_cache_file;